Skip to content

Commit

Permalink
feat(forms): add stepper form input
Browse files Browse the repository at this point in the history
This input type allows the user to add and subtract from a number input
type by pressing the buttons beside it.
  • Loading branch information
jefflembeck committed Apr 13, 2016
1 parent 0c9f7fe commit 202f3e8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/pivotal-ui/components/forms/forms.scss
Expand Up @@ -1305,6 +1305,65 @@ A toggle switch is a horizontally styled checkbox which represents true with blu
}
}

/*doc
---
title: Stepper
name: form_stepper
categories:
- css_components_forms
- css_all
---
A stepper is a component that increments and decrements a number in an associated
text field by interacting with one of two buttons.
```html_example
<form class="styleguide-form form-inline" role="form">
<div class="form-group">
<label class="control-label" for="subscribers">Subscriber count</label>
<div class="form-stepper">
<button type="button" class="btn btn-default btn-decrement" title="decrement value"><span class="a11y-only">decrement value</span>&minus;</button>
<input class="form-control" min="1" max="99999" step="1" name="subscribers" required="" type="number" value=10>
<button type="button" class="btn btn-default btn-increment" title="increment value"><span class="a11y-only">increment value</span>+</button>
</div>
</div>
</form>
```
*/

.form-stepper {
/* omg hacks
this is to get rid of the native number spinner
*/
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
appearance: none;
margin: 0;
}
input[type=number] {
-moz-appearance: textfield;
appearance: textfield;
width: 5em;
text-align: center;
}

input[type=number],
button {
display: inline-block;
vertical-align: middle;
}

button {
border-radius: 4px;
padding: 10px 15px;
text-align: center;
}
}

/*pending
---
title: Focus Inputs
Expand Down
1 change: 1 addition & 0 deletions src/pivotal-ui/components/forms/index.js
@@ -0,0 +1 @@
require('./stepper');
3 changes: 2 additions & 1 deletion src/pivotal-ui/components/forms/package.json
@@ -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"
}
61 changes: 61 additions & 0 deletions src/pivotal-ui/components/forms/stepper.js
@@ -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;
1 change: 1 addition & 0 deletions src/pivotal-ui/javascripts/pivotal-ui.js
Expand Up @@ -7,3 +7,4 @@ require('pui-prismjs');
require('./scale')();
require('@npmcorp/pui-react-back-to-top/jquery-plugin');
require('@npmcorp/pui-css-drop-down-menu');
require('../components/forms');

0 comments on commit 202f3e8

Please sign in to comment.