Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(slider): increment the value properly when step is a decimal number
Browse files Browse the repository at this point in the history
switch from toFixed to Math.round
update tests for predictable steps

Closes #2015. Closes #2657.
  • Loading branch information
kennethcachia authored and ThomasBurleson committed Jun 4, 2015
1 parent 8edbae5 commit 476d068
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/components/slider/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ function SliderDirective($$rAF, $window, $mdAria, $mdUtil, $mdConstant, $mdThemi
}
function stepValidator(value) {
if (angular.isNumber(value)) {
return Math.round(value / step) * step;
var formattedValue = (Math.round(value / step) * step);
// Format to 3 digits after the decimal point - fixes #2015.
return (Math.round(formattedValue * 1000) / 1000);
}
}

Expand Down
35 changes: 33 additions & 2 deletions src/components/slider/slider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ describe('md-slider', function() {
$rootScope.$apply('value = 50');

slider.triggerHandler({type: '$md.pressdown', pointer: { x: 30 }});
slider.triggerHandler({type: '$md.dragstart', pointer: { x: 30 } });
slider.triggerHandler({type: '$md.dragstart', pointer: { x: 30 }});
$timeout.flush();
expect($rootScope.value).toBe(30);

//When going past max, it should clamp to max
slider.triggerHandler({type: '$md.drag', pointer: { x: 150 } });
slider.triggerHandler({type: '$md.drag', pointer: { x: 150 }});
$timeout.flush();
expect($rootScope.value).toBe(100);

Expand Down Expand Up @@ -194,5 +194,36 @@ describe('md-slider', function() {
});
expect(slider).not.toHaveClass('active');
}));

it('should increment at a predictable step', inject(function($rootScope, $timeout) {

buildSlider(0.1, 1).drag({x:70});
expect($rootScope.value).toBe(0.7);

buildSlider(0.25, 1).drag({x:45});
expect($rootScope.value).toBe(0.5);

buildSlider(0.25, 1).drag({x:35});
expect($rootScope.value).toBe(0.25);

buildSlider(1, 100).drag({x:90});
expect($rootScope.value).toBe(90);

function buildSlider(step, max) {
var slider = setup('ng-model="value" min="0" max="' + max + '" step="' + step + '"');
$rootScope.$apply('value = 0.5');

return {
drag : function simulateDrag(drag) {

slider.triggerHandler({type: '$md.pressdown', pointer: drag });
slider.triggerHandler({type: '$md.dragstart', pointer: drag });

$timeout.flush();
}
};
}

}));

});

0 comments on commit 476d068

Please sign in to comment.