Skip to content

Commit 39543a3

Browse files
devversionandrewseguin
authored andcommitted
fix(slider): change event is not being emitted (#7278)
* In some situations, the change event of the `<mat-slider>` is not being emitted if the user drags the thumb of the slider. This is because the `slidestart` event fires a second time before the sliding ends. This causes the slider to think that there was no value change. Fixes #7207
1 parent 4122ae2 commit 39543a3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/lib/slider/slider.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ describe('MatSlider without forms', () => {
142142
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
143143
});
144144

145+
it('should not change value without emitting a change event', () => {
146+
const onChangeSpy = jasmine.createSpy('slider onChange');
147+
148+
sliderInstance.change.subscribe(onChangeSpy);
149+
sliderInstance.value = 50;
150+
fixture.detectChanges();
151+
152+
dispatchSlideStartEvent(sliderNativeElement, 0, gestureConfig);
153+
fixture.detectChanges();
154+
155+
dispatchSlideEvent(sliderNativeElement, 10, gestureConfig);
156+
fixture.detectChanges();
157+
158+
// In some situations, HammerJS will fire a second "slidestart" event because the user
159+
// holds the thumb and drags it around. This would mean that the `_valueOnSlideStart`
160+
// value will be updated to the actual end value. Causing the slider to think that the value
161+
// didn't change at all.
162+
dispatchSlideStartEvent(sliderNativeElement, 10, gestureConfig);
163+
fixture.detectChanges();
164+
165+
dispatchSlideEndEvent(sliderNativeElement, 10, gestureConfig);
166+
fixture.detectChanges();
167+
168+
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
169+
expect(onChangeSpy).toHaveBeenCalledTimes(1);
170+
});
171+
145172
it('should reset active state upon blur', () => {
146173
sliderInstance._isActive = true;
147174

src/lib/slider/slider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ export class MatSlider extends _MatSliderMixinBase
492492
}
493493

494494
_onSlideStart(event: HammerInput | null) {
495-
if (this.disabled) {
495+
if (this.disabled || this._isSliding) {
496496
return;
497497
}
498498

0 commit comments

Comments
 (0)