Skip to content

fix(material/slider): first keypress ignored if out-of-bounds value is assigned #23827

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
Jan 13, 2022
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
17 changes: 17 additions & 0 deletions src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ describe('MatSlider', () => {
let sliderNativeElement: HTMLElement;
let testComponent: SliderWithChangeHandler;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;

beforeEach(() => {
fixture = createComponent(SliderWithChangeHandler);
Expand All @@ -974,6 +975,7 @@ describe('MatSlider', () => {
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.injector.get<MatSlider>(MatSlider);
trackFillElement = sliderNativeElement.querySelector('.mat-slider-track-fill') as HTMLElement;
});

it('should increment slider by 1 on up arrow pressed', () => {
Expand Down Expand Up @@ -1028,6 +1030,21 @@ describe('MatSlider', () => {
expect(sliderInstance.value).toBe(99);
});

it('should decrement from max when interacting after out-of-bounds value is assigned', () => {
sliderInstance.max = 100;
sliderInstance.value = 200;
fixture.detectChanges();

expect(sliderInstance.value).toBe(200);
expect(trackFillElement.style.transform).toContain('scale3d(1, 1, 1)');

dispatchKeyboardEvent(sliderNativeElement, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(sliderInstance.value).toBe(99);
expect(trackFillElement.style.transform).toContain('scale3d(0.99, 1, 1)');
});

it('should increment slider by 10 on page up pressed', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();

Expand Down
5 changes: 4 additions & 1 deletion src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,10 @@ export class MatSlider

/** Increments the slider by the given number of steps (negative number decrements). */
private _increment(numSteps: number) {
this.value = this._clamp((this.value || 0) + this.step * numSteps, this.min, this.max);
// Pre-clamp the current value since it's allowed to be
// out of bounds when assigned programmatically.
const clampedValue = this._clamp(this.value || 0, this.min, this.max);
this.value = this._clamp(clampedValue + this.step * numSteps, this.min, this.max);
}

/** Calculate the new value from the new physical location. The value will always be snapped. */
Expand Down