Skip to content

Commit ce72369

Browse files
authored
fix(slider): don't emit change events on mousedown (#20240)
Currently when the user presses down somewhere on the slider we emit a `change` event, as well as when they release. While we don't emit the same value twice, the timing seems to be unexpected by consumers, because one happens at the begging of the sequence and another one at the end. These changes make it so that we only emit the change event once the user has released the slider. Fixes #14363.
1 parent b69a75b commit ce72369

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/material/slider/slider.spec.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,12 @@ describe('MatSlider', () => {
763763
sliderNativeElement = sliderDebugElement.nativeElement;
764764
});
765765

766-
it('should emit change on mousedown', () => {
766+
it('should emit change on mouseup', () => {
767767
expect(testComponent.onChange).not.toHaveBeenCalled();
768768

769769
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
770770
fixture.detectChanges();
771+
dispatchSlideEndEvent(sliderNativeElement, 0.2);
771772

772773
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
773774
});
@@ -799,12 +800,15 @@ describe('MatSlider', () => {
799800
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
800801
fixture.detectChanges();
801802

802-
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
803+
expect(testComponent.onChange).not.toHaveBeenCalled();
803804
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
804805

805806
dispatchSlideEndEvent(sliderNativeElement, 0.2);
806807
fixture.detectChanges();
807808

809+
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
810+
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
811+
808812
testComponent.slider.value = 0;
809813
fixture.detectChanges();
810814

@@ -813,6 +817,7 @@ describe('MatSlider', () => {
813817

814818
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
815819
fixture.detectChanges();
820+
dispatchSlideEndEvent(sliderNativeElement, 0.2);
816821

817822
expect(testComponent.onChange).toHaveBeenCalledTimes(2);
818823
expect(testComponent.onInput).toHaveBeenCalledTimes(2);
@@ -857,8 +862,8 @@ describe('MatSlider', () => {
857862
expect(testComponent.onChange).not.toHaveBeenCalled();
858863

859864
dispatchMousedownEventSequence(sliderNativeElement, 0.75);
860-
861865
fixture.detectChanges();
866+
dispatchSlideEndEvent(sliderNativeElement, 0.75);
862867

863868
// The `onInput` event should be emitted once due to a single click.
864869
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
@@ -1270,11 +1275,12 @@ describe('MatSlider', () => {
12701275
sliderNativeElement = sliderDebugElement.nativeElement;
12711276
});
12721277

1273-
it('should update the model on mousedown', () => {
1278+
it('should update the model on mouseup', () => {
12741279
expect(testComponent.val).toBe(0);
12751280

12761281
dispatchMousedownEventSequence(sliderNativeElement, 0.76);
12771282
fixture.detectChanges();
1283+
dispatchSlideEndEvent(sliderNativeElement, 0.76);
12781284

12791285
expect(testComponent.val).toBe(76);
12801286
});
@@ -1342,11 +1348,12 @@ describe('MatSlider', () => {
13421348
expect(testComponent.control.value).toBe(0);
13431349
});
13441350

1345-
it('should update the control on mousedown', () => {
1351+
it('should update the control on mouseup', () => {
13461352
expect(testComponent.control.value).toBe(0);
13471353

13481354
dispatchMousedownEventSequence(sliderNativeElement, 0.76);
13491355
fixture.detectChanges();
1356+
dispatchSlideEndEvent(sliderNativeElement, 0.76);
13501357

13511358
expect(testComponent.control.value).toBe(76);
13521359
});
@@ -1399,6 +1406,7 @@ describe('MatSlider', () => {
13991406
// but remain untouched.
14001407
dispatchMousedownEventSequence(sliderNativeElement, 0.5);
14011408
fixture.detectChanges();
1409+
dispatchSlideEndEvent(sliderNativeElement, 0.5);
14021410

14031411
expect(sliderControl.valid).toBe(true);
14041412
expect(sliderControl.pristine).toBe(false);
@@ -1435,6 +1443,7 @@ describe('MatSlider', () => {
14351443

14361444
dispatchMousedownEventSequence(sliderNativeElement, 0.1);
14371445
fixture.detectChanges();
1446+
dispatchSlideEndEvent(sliderNativeElement, 0.1);
14381447

14391448
expect(testComponent.value).toBe(10);
14401449
expect(testComponent.slider.value).toBe(10);

src/material/slider/slider.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,6 @@ export class MatSlider extends _MatSliderMixinBase
470470
/** The value of the slider when the slide start event fires. */
471471
private _valueOnSlideStart: number | null;
472472

473-
/** Position of the pointer when the dragging started. */
474-
private _pointerPositionOnStart: {x: number, y: number} | null;
475-
476473
/** Reference to the inner slider wrapper element. */
477474
@ViewChild('sliderWrapper') private _sliderWrapper: ElementRef;
478475

@@ -639,13 +636,11 @@ export class MatSlider extends _MatSliderMixinBase
639636
this._bindGlobalEvents(event);
640637
this._focusHostElement();
641638
this._updateValueFromPosition(pointerPosition);
642-
this._valueOnSlideStart = this.value;
643-
this._pointerPositionOnStart = pointerPosition;
639+
this._valueOnSlideStart = oldValue;
644640

645641
// Emit a change and input event if the value changed.
646642
if (oldValue != this.value) {
647643
this._emitInputEvent();
648-
this._emitChangeEvent();
649644
}
650645
});
651646
}
@@ -672,19 +667,15 @@ export class MatSlider extends _MatSliderMixinBase
672667
/** Called when the user has lifted their pointer. Bound on the document level. */
673668
private _pointerUp = (event: TouchEvent | MouseEvent) => {
674669
if (this._isSliding) {
675-
const pointerPositionOnStart = this._pointerPositionOnStart;
676-
const currentPointerPosition = getPointerPositionOnPage(event);
677-
678670
event.preventDefault();
679671
this._removeGlobalEvents();
680-
this._valueOnSlideStart = this._pointerPositionOnStart = this._lastPointerEvent = null;
681672
this._isSliding = false;
682673

683-
if (this._valueOnSlideStart != this.value && !this.disabled &&
684-
pointerPositionOnStart && (pointerPositionOnStart.x !== currentPointerPosition.x ||
685-
pointerPositionOnStart.y !== currentPointerPosition.y)) {
674+
if (this._valueOnSlideStart != this.value && !this.disabled) {
686675
this._emitChangeEvent();
687676
}
677+
678+
this._valueOnSlideStart = this._lastPointerEvent = null;
688679
}
689680
}
690681

0 commit comments

Comments
 (0)