Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This input type allows the user to add and subtract from a number input type by pressing the buttons beside it.
- Loading branch information
1 parent
0c9f7fe
commit 202f3e8
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./stepper'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"homepage": "http://styleguide.pivotal.io/forms.html", | ||
"main": "index.js", | ||
"dependencies": { | ||
"@npmcorp/pui-css-bootstrap": "^2.0.0" | ||
}, | ||
"version": "2.0.1" | ||
"version": "2.1.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var FormStepper = function(el, input, decrementButton, incrementButton){ | ||
this.stepper = $(el); | ||
this.input = input || this.stepper.find("input[type=number]"); | ||
this.decrementButton = decrementButton || this.stepper.find(".btn-decrement"); | ||
this.incrementButton = incrementButton || this.stepper.find(".btn-increment"); | ||
|
||
this.min = parseInt(this.input.attr("min")) || 0; | ||
this.max = parseInt(this.input.attr("max")) || 99999; | ||
this.value = parseInt(this.input.val()) || parseInt(this.min); | ||
}; | ||
|
||
FormStepper.prototype.increment = function(){ | ||
var val = parseInt(this.value) || this.min; | ||
this.set(val + 1); | ||
}; | ||
|
||
FormStepper.prototype.decrement = function(){ | ||
var val = parseInt(this.value) || this.min; | ||
this.set(val - 1); | ||
}; | ||
|
||
FormStepper.prototype.set = function(number){ | ||
number = parseInt(number) || this.min; | ||
|
||
if( number <= this.min ){ | ||
number = this.min; | ||
} else if (number >= this.max ){ | ||
number = this.max; | ||
} | ||
|
||
this.value = number; | ||
this.input.val(number); | ||
this.stepper.trigger("FormStepper:changed", [number]); | ||
}; | ||
|
||
FormStepper.prototype.addListeners = function(){ | ||
var self = this; | ||
|
||
this.incrementButton.on("click", function(){ | ||
self.increment(); | ||
}); | ||
|
||
this.decrementButton.on("click", function(){ | ||
self.decrement(); | ||
}); | ||
|
||
this.input.on("change", function(){ | ||
var val = parseInt(self.input.val()) || self.min; | ||
self.set(val); | ||
}); | ||
}; | ||
|
||
$(function(){ | ||
$.each($('.form-stepper'), function(i, el) { | ||
var fs = new FormStepper(el); | ||
fs.addListeners(); | ||
}); | ||
}); | ||
|
||
|
||
module.exports = FormStepper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters