Skip to content

fix(material/slider): unable to assign min/max values if they are more precise than then step #21155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2020
Merged
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
26 changes: 25 additions & 1 deletion src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,29 @@ describe('MatSlider', () => {
expect(ticksElement.style.transform).toContain('translateX(25%)');
expect(ticksContainerElement.style.transform).toBe('translateX(-25%)');
});

it('should be able to set the min and max values when they are more precise ' +
'than the step', () => {
// Note that we assign min/max with more decimals than the
// step to ensure that the value doesn't get rounded up.
testComponent.step = 0.5;
testComponent.min = 10.15;
testComponent.max = 50.15;
fixture.detectChanges();

dispatchSlideEventSequence(sliderNativeElement, 0.5, 0);
fixture.detectChanges();

expect(sliderInstance.value).toBe(10.15);
expect(sliderInstance.percent).toBe(0);

dispatchSlideEventSequence(sliderNativeElement, 0.5, 1);
fixture.detectChanges();

expect(sliderInstance.value).toBe(50.15);
expect(sliderInstance.percent).toBe(1);
});

});

describe('slider with set value', () => {
Expand Down Expand Up @@ -1510,12 +1533,13 @@ class StandardSlider { }
class DisabledSlider { }

@Component({
template: `<mat-slider [min]="min" [max]="max" tickInterval="6"></mat-slider>`,
template: `<mat-slider [min]="min" [max]="max" [step]="step" tickInterval="6"></mat-slider>`,
styles: [styles],
})
class SliderWithMinAndMax {
min = 4;
max = 6;
step = 1;
}

@Component({
Expand Down
2 changes: 1 addition & 1 deletion src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class MatSlider extends _MatSliderMixinBase

// While incrementing by a decimal we can end up with values like 33.300000000000004.
// Truncate it to ensure that it matches the label and to make it easier to work with.
if (this._roundToDecimal) {
if (this._roundToDecimal && value !== this.min && value !== this.max) {
value = parseFloat(value.toFixed(this._roundToDecimal));
}

Expand Down