Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added increament/decreament steps. #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions js/bootstrap-formhelpers-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

value = this.getValue();

value = value + 1;
value = value + this.options.steps;

this.$element.val(value).change();
},
Expand All @@ -176,7 +176,7 @@

value = this.getValue();

value = value - 1;
value = value - this.options.steps;

this.$element.val(value).change();
},
Expand All @@ -185,14 +185,14 @@
var value;

value = this.$element.val();
if (value !== '-1') {
value = String(value).replace(/\D/g, '');
}
if (String(value).length === 0) {

if ($.isNumeric(value)) {
value = parseInt(value);
} else {
value = this.options.min;
}
return parseInt(value);
};

return value;
},

formatNumber: function() {
Expand Down Expand Up @@ -266,7 +266,8 @@
zeros: false,
keyboard: true,
buttons: true,
wrap: false
wrap: false,
steps: 1,
};


Expand Down
43 changes: 43 additions & 0 deletions js/tests/unit/bootstrap-formhelpers-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,48 @@ $(function () {

number.remove();
});

test('should increment value with steps on click increment button', function() {
var numberHTML = '<input type="text" class="form-control bfh-number" value="5" data-steps="5">',
number = $(numberHTML).appendTo('#qunit-fixture').bfhnumber({steps: 5});

number.parent().find('.inc').mousedown().mouseup();

ok(number.val() === '10', 'value is 10');

number.remove();
});

test('should decrement value with steps on click decrement button', function() {
var numberHTML = '<input type="text" class="form-control bfh-number" value="5" data-steps="2">',
number = $(numberHTML).appendTo('#qunit-fixture').bfhnumber({steps: 2});

number.parent().find('.dec').mousedown().mouseup();

ok(number.val() === '3', 'value is 3');

number.remove();
});

test('should not increment value over the max', function() {
var numberHTML = '<input type="text" class="form-control bfh-number" value="3" data-max="5" data-steps="3">',
number = $(numberHTML).appendTo('#qunit-fixture').bfhnumber({max: 5, steps: 3});

number.parent().find('.inc').mousedown().mouseup();

ok(number.val() === '5', 'value is 5');

number.remove();
});

test('should not decrement value under the min', function() {
var numberHTML = '<input type="text" class="form-control bfh-number" data-steps="3">',
number = $(numberHTML).appendTo('#qunit-fixture').bfhnumber({steps: 3});

number.parent().find('.dec').mousedown().mouseup();
ok(number.val() === '0', 'value is 0');

number.remove();
});

});