Skip to content

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
  • Loading branch information
kennethcachia committed May 1, 2015
1 parent 44e6984 commit 375df24
Show file tree
Hide file tree
Showing 2 changed files with 24 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 parseFloat(formattedValue.toFixed(3));
}
}

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);
}));

});

0 comments on commit 375df24

Please sign in to comment.