diff --git a/js/bootstrap-formhelpers-number.js b/js/bootstrap-formhelpers-number.js index 1aa5c6a..f983dad 100644 --- a/js/bootstrap-formhelpers-number.js +++ b/js/bootstrap-formhelpers-number.js @@ -166,7 +166,7 @@ value = this.getValue(); - value = value + 1; + value = value + this.options.steps; this.$element.val(value).change(); }, @@ -176,7 +176,7 @@ value = this.getValue(); - value = value - 1; + value = value - this.options.steps; this.$element.val(value).change(); }, @@ -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() { @@ -266,7 +266,8 @@ zeros: false, keyboard: true, buttons: true, - wrap: false + wrap: false, + steps: 1, }; diff --git a/js/tests/unit/bootstrap-formhelpers-number.js b/js/tests/unit/bootstrap-formhelpers-number.js index 0a722b5..94c3d3a 100644 --- a/js/tests/unit/bootstrap-formhelpers-number.js +++ b/js/tests/unit/bootstrap-formhelpers-number.js @@ -233,5 +233,48 @@ $(function () { number.remove(); }); + + test('should increment value with steps on click increment button', function() { + var numberHTML = '', + 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 = '', + 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 = '', + 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 = '', + number = $(numberHTML).appendTo('#qunit-fixture').bfhnumber({steps: 3}); + + number.parent().find('.dec').mousedown().mouseup(); + ok(number.val() === '0', 'value is 0'); + + number.remove(); + }); }); \ No newline at end of file