Skip to content

Commit dfa3a9f

Browse files
dekajpscottgonzalez
authored andcommitted
Slider: Fix max calculation, when step is float
Fixes #10721 Closes gh-1398 (cherry picked from commit ae1d6d5)
1 parent fcb26aa commit dfa3a9f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

tests/unit/slider/slider_options.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test( "disabled", function(){
4040
});
4141

4242
test( "max", function() {
43-
expect( 4 );
43+
expect( 5 );
4444
element = $( "<div></div>" );
4545

4646
options = {
@@ -72,6 +72,18 @@ test( "max", function() {
7272
ok( element.slider( "value" ) === options.max, "value method will max, step is changed" );
7373
element.slider( "destroy" );
7474

75+
options = {
76+
max: 60,
77+
min: 50,
78+
orientation: "horizontal",
79+
step: 0.1,
80+
value: 60
81+
};
82+
83+
element.slider( options );
84+
ok( element.slider( "value" ) === options.max, "value method will max, step is changed and step is float" );
85+
element.slider( "destroy" );
86+
7587
});
7688

7789
test( "min", function() {

ui/slider.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,26 @@ return $.widget( "ui.slider", $.ui.mouse, {
547547
},
548548

549549
_calculateNewMax: function() {
550-
var remainder = ( this.options.max - this._valueMin() ) % this.options.step;
551-
this.max = this.options.max - remainder;
550+
var max = this.options.max,
551+
min = this._valueMin(),
552+
step = this.options.step,
553+
aboveMin = Math.floor( ( max - min ) / step ) * step;
554+
max = aboveMin + min;
555+
this.max = parseFloat( max.toFixed( this._precision() ) );
556+
},
557+
558+
_precision: function() {
559+
var precision = this._precisionOf( this.options.step );
560+
if ( this.options.min !== null ) {
561+
precision = Math.max( precision, this._precisionOf( this.options.min ) );
562+
}
563+
return precision;
564+
},
565+
566+
_precisionOf: function( num ) {
567+
var str = num.toString(),
568+
decimal = str.indexOf( "." );
569+
return decimal === -1 ? 0 : str.length - decimal - 1;
552570
},
553571

554572
_valueMin: function() {

0 commit comments

Comments
 (0)