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

fix(slider): increment the value properly when step is a decimal number #2657

Closed
wants to merge 2 commits into from
Closed
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
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
23 changes: 21 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,24 @@ describe('md-slider', function() {
});
expect(slider).not.toHaveClass('active');
}));

it('should increment at a predictable step', inject(function($rootScope, $timeout) {
function simulateDrag(step, max) {
var slider = setup('ng-model="value" min="0" max="' + max + '" step="' + step + '"');
$rootScope.$apply('value = 0.5');
slider.triggerHandler({type: '$md.pressdown', pointer: { x: 70 }});
slider.triggerHandler({type: '$md.dragstart', pointer: { x: 70 }});
$timeout.flush();
}

simulateDrag(0.1, 1);
expect($rootScope.value).toBe(0.7);

simulateDrag(0.25, 1);
expect($rootScope.value).toBe(0.75);

simulateDrag(1, 100);
expect($rootScope.value).toBe(70);
}));

});