Skip to content

Commit 861642c

Browse files
crisbetojosephperrott
authored andcommitted
fix(slider): update value on mousedown instead of click (#13020)
1 parent 87e1742 commit 861642c

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed

src/lib/slider/slider.spec.ts

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ describe('MatSlider', () => {
6060
expect(sliderInstance.max).toBe(100);
6161
});
6262

63-
it('should update the value on a click', () => {
63+
it('should update the value on mousedown', () => {
6464
expect(sliderInstance.value).toBe(0);
6565

66-
dispatchClickEventSequence(sliderNativeElement, 0.19);
66+
dispatchMousedownEventSequence(sliderNativeElement, 0.19);
6767

6868
expect(sliderInstance.value).toBe(19);
6969
});
@@ -92,10 +92,10 @@ describe('MatSlider', () => {
9292
expect(sliderInstance.value).toBe(100);
9393
});
9494

95-
it('should update the track fill on click', () => {
95+
it('should update the track fill on mousedown', () => {
9696
expect(trackFillElement.style.transform).toContain('scale3d(0, 1, 1)');
9797

98-
dispatchClickEventSequence(sliderNativeElement, 0.39);
98+
dispatchMousedownEventSequence(sliderNativeElement, 0.39);
9999
fixture.detectChanges();
100100

101101
expect(trackFillElement.style.transform).toContain('scale3d(0.39, 1, 1)');
@@ -178,7 +178,7 @@ describe('MatSlider', () => {
178178
});
179179

180180
it('should not have thumb gap when not at min value', () => {
181-
dispatchClickEventSequence(sliderNativeElement, 1);
181+
dispatchMousedownEventSequence(sliderNativeElement, 1);
182182
fixture.detectChanges();
183183

184184
// Some browsers use '0' and some use '0px', so leave off the closing paren.
@@ -223,10 +223,10 @@ describe('MatSlider', () => {
223223
expect(sliderInstance.disabled).toBeTruthy();
224224
});
225225

226-
it('should not change the value on click when disabled', () => {
226+
it('should not change the value on mousedown when disabled', () => {
227227
expect(sliderInstance.value).toBe(0);
228228

229-
dispatchClickEventSequence(sliderNativeElement, 0.63);
229+
dispatchMousedownEventSequence(sliderNativeElement, 0.63);
230230

231231
expect(sliderInstance.value).toBe(0);
232232
});
@@ -248,10 +248,10 @@ describe('MatSlider', () => {
248248
expect(onChangeSpy).toHaveBeenCalledTimes(0);
249249
});
250250

251-
it('should not add the mat-slider-active class on click when disabled', () => {
251+
it('should not add the mat-slider-active class on mousedown when disabled', () => {
252252
expect(sliderNativeElement.classList).not.toContain('mat-slider-active');
253253

254-
dispatchClickEventSequence(sliderNativeElement, 0.43);
254+
dispatchMousedownEventSequence(sliderNativeElement, 0.43);
255255
fixture.detectChanges();
256256

257257
expect(sliderNativeElement.classList).not.toContain('mat-slider-active');
@@ -305,12 +305,12 @@ describe('MatSlider', () => {
305305
expect(sliderInstance.max).toBe(6);
306306
});
307307

308-
it('should set the correct value on click', () => {
309-
dispatchClickEventSequence(sliderNativeElement, 0.09);
308+
it('should set the correct value on mousedown', () => {
309+
dispatchMousedownEventSequence(sliderNativeElement, 0.09);
310310
fixture.detectChanges();
311311

312312
// Computed by multiplying the difference between the min and the max by the percentage from
313-
// the click and adding that to the minimum.
313+
// the mousedown and adding that to the minimum.
314314
let value = Math.round(4 + (0.09 * (6 - 4)));
315315
expect(sliderInstance.value).toBe(value);
316316
});
@@ -320,13 +320,13 @@ describe('MatSlider', () => {
320320
fixture.detectChanges();
321321

322322
// Computed by multiplying the difference between the min and the max by the percentage from
323-
// the click and adding that to the minimum.
323+
// the mousedown and adding that to the minimum.
324324
let value = Math.round(4 + (0.62 * (6 - 4)));
325325
expect(sliderInstance.value).toBe(value);
326326
});
327327

328-
it('should snap the fill to the nearest value on click', () => {
329-
dispatchClickEventSequence(sliderNativeElement, 0.68);
328+
it('should snap the fill to the nearest value on mousedown', () => {
329+
dispatchMousedownEventSequence(sliderNativeElement, 0.68);
330330
fixture.detectChanges();
331331

332332
// The closest snap is halfway on the slider.
@@ -392,8 +392,8 @@ describe('MatSlider', () => {
392392
expect(sliderInstance.value).toBe(26);
393393
});
394394

395-
it('should set the correct value on click', () => {
396-
dispatchClickEventSequence(sliderNativeElement, 0.92);
395+
it('should set the correct value on mousedown', () => {
396+
dispatchMousedownEventSequence(sliderNativeElement, 0.92);
397397
fixture.detectChanges();
398398

399399
// On a slider with default max and min the value should be approximately equal to the
@@ -426,17 +426,17 @@ describe('MatSlider', () => {
426426
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
427427
});
428428

429-
it('should set the correct step value on click', () => {
429+
it('should set the correct step value on mousedown', () => {
430430
expect(sliderInstance.value).toBe(0);
431431

432-
dispatchClickEventSequence(sliderNativeElement, 0.13);
432+
dispatchMousedownEventSequence(sliderNativeElement, 0.13);
433433
fixture.detectChanges();
434434

435435
expect(sliderInstance.value).toBe(25);
436436
});
437437

438-
it('should snap the fill to a step on click', () => {
439-
dispatchClickEventSequence(sliderNativeElement, 0.66);
438+
it('should snap the fill to a step on mousedown', () => {
439+
dispatchMousedownEventSequence(sliderNativeElement, 0.66);
440440
fixture.detectChanges();
441441

442442
// The closest step is at 75% of the slider.
@@ -596,10 +596,10 @@ describe('MatSlider', () => {
596596
expect(sliderNativeElement.classList).toContain('mat-slider-thumb-label-showing');
597597
});
598598

599-
it('should update the thumb label text on click', () => {
599+
it('should update the thumb label text on mousedown', () => {
600600
expect(thumbLabelTextElement.textContent).toBe('0');
601601

602-
dispatchClickEventSequence(sliderNativeElement, 0.13);
602+
dispatchMousedownEventSequence(sliderNativeElement, 0.13);
603603
fixture.detectChanges();
604604

605605
// The thumb label text is set to the slider's value. These should always be the same.
@@ -757,10 +757,10 @@ describe('MatSlider', () => {
757757
sliderNativeElement = sliderDebugElement.nativeElement;
758758
});
759759

760-
it('should emit change on click', () => {
760+
it('should emit change on mousedown', () => {
761761
expect(testComponent.onChange).not.toHaveBeenCalled();
762762

763-
dispatchClickEventSequence(sliderNativeElement, 0.2);
763+
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
764764
fixture.detectChanges();
765765

766766
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
@@ -778,7 +778,7 @@ describe('MatSlider', () => {
778778
it('should not emit multiple changes for same value', () => {
779779
expect(testComponent.onChange).not.toHaveBeenCalled();
780780

781-
dispatchClickEventSequence(sliderNativeElement, 0.6);
781+
dispatchMousedownEventSequence(sliderNativeElement, 0.6);
782782
dispatchSlideEventSequence(sliderNativeElement, 0.6, 0.6, gestureConfig);
783783
fixture.detectChanges();
784784

@@ -790,7 +790,7 @@ describe('MatSlider', () => {
790790
expect(testComponent.onChange).not.toHaveBeenCalled();
791791
expect(testComponent.onInput).not.toHaveBeenCalled();
792792

793-
dispatchClickEventSequence(sliderNativeElement, 0.2);
793+
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
794794
fixture.detectChanges();
795795

796796
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
@@ -802,7 +802,7 @@ describe('MatSlider', () => {
802802
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
803803
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
804804

805-
dispatchClickEventSequence(sliderNativeElement, 0.2);
805+
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
806806
fixture.detectChanges();
807807

808808
expect(testComponent.onChange).toHaveBeenCalledTimes(2);
@@ -846,7 +846,7 @@ describe('MatSlider', () => {
846846
it('should emit an input event when clicking', () => {
847847
expect(testComponent.onChange).not.toHaveBeenCalled();
848848

849-
dispatchClickEventSequence(sliderNativeElement, 0.75);
849+
dispatchMousedownEventSequence(sliderNativeElement, 0.75);
850850

851851
fixture.detectChanges();
852852

@@ -1017,7 +1017,7 @@ describe('MatSlider', () => {
10171017
testComponent.invert = true;
10181018
fixture.detectChanges();
10191019

1020-
dispatchClickEventSequence(sliderNativeElement, 0.3);
1020+
dispatchMousedownEventSequence(sliderNativeElement, 0.3);
10211021
fixture.detectChanges();
10221022

10231023
expect(sliderInstance.value).toBe(70);
@@ -1027,7 +1027,7 @@ describe('MatSlider', () => {
10271027
testComponent.dir = 'rtl';
10281028
fixture.detectChanges();
10291029

1030-
dispatchClickEventSequence(sliderNativeElement, 0.3);
1030+
dispatchMousedownEventSequence(sliderNativeElement, 0.3);
10311031
fixture.detectChanges();
10321032

10331033
expect(sliderInstance.value).toBe(70);
@@ -1038,7 +1038,7 @@ describe('MatSlider', () => {
10381038
testComponent.invert = true;
10391039
fixture.detectChanges();
10401040

1041-
dispatchClickEventSequence(sliderNativeElement, 0.3);
1041+
dispatchMousedownEventSequence(sliderNativeElement, 0.3);
10421042
fixture.detectChanges();
10431043

10441044
expect(sliderInstance.value).toBe(30);
@@ -1155,39 +1155,39 @@ describe('MatSlider', () => {
11551155
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
11561156
});
11571157

1158-
it('updates value on click', () => {
1159-
dispatchClickEventSequence(sliderNativeElement, 0.3);
1158+
it('updates value on mousedown', () => {
1159+
dispatchMousedownEventSequence(sliderNativeElement, 0.3);
11601160
fixture.detectChanges();
11611161

11621162
expect(sliderInstance.value).toBe(70);
11631163
});
11641164

1165-
it('updates value on click in inverted mode', () => {
1165+
it('updates value on mousedown in inverted mode', () => {
11661166
testComponent.invert = true;
11671167
fixture.detectChanges();
11681168

1169-
dispatchClickEventSequence(sliderNativeElement, 0.3);
1169+
dispatchMousedownEventSequence(sliderNativeElement, 0.3);
11701170
fixture.detectChanges();
11711171

11721172
expect(sliderInstance.value).toBe(30);
11731173
});
11741174

1175-
it('should update the track fill on click', () => {
1175+
it('should update the track fill on mousedown', () => {
11761176
expect(trackFillElement.style.transform).toContain('scale3d(1, 0, 1)');
11771177

1178-
dispatchClickEventSequence(sliderNativeElement, 0.39);
1178+
dispatchMousedownEventSequence(sliderNativeElement, 0.39);
11791179
fixture.detectChanges();
11801180

11811181
expect(trackFillElement.style.transform).toContain('scale3d(1, 0.61, 1)');
11821182
});
11831183

1184-
it('should update the track fill on click in inverted mode', () => {
1184+
it('should update the track fill on mousedown in inverted mode', () => {
11851185
testComponent.invert = true;
11861186
fixture.detectChanges();
11871187

11881188
expect(trackFillElement.style.transform).toContain('scale3d(1, 0, 1)');
11891189

1190-
dispatchClickEventSequence(sliderNativeElement, 0.39);
1190+
dispatchMousedownEventSequence(sliderNativeElement, 0.39);
11911191
fixture.detectChanges();
11921192

11931193
expect(trackFillElement.style.transform).toContain('scale3d(1, 0.39, 1)');
@@ -1241,10 +1241,10 @@ describe('MatSlider', () => {
12411241
sliderNativeElement = sliderDebugElement.nativeElement;
12421242
});
12431243

1244-
it('should update the model on click', () => {
1244+
it('should update the model on mousedown', () => {
12451245
expect(testComponent.val).toBe(0);
12461246

1247-
dispatchClickEventSequence(sliderNativeElement, 0.76);
1247+
dispatchMousedownEventSequence(sliderNativeElement, 0.76);
12481248
fixture.detectChanges();
12491249

12501250
expect(testComponent.val).toBe(76);
@@ -1313,10 +1313,10 @@ describe('MatSlider', () => {
13131313
expect(testComponent.control.value).toBe(0);
13141314
});
13151315

1316-
it('should update the control on click', () => {
1316+
it('should update the control on mousedown', () => {
13171317
expect(testComponent.control.value).toBe(0);
13181318

1319-
dispatchClickEventSequence(sliderNativeElement, 0.76);
1319+
dispatchMousedownEventSequence(sliderNativeElement, 0.76);
13201320
fixture.detectChanges();
13211321

13221322
expect(testComponent.control.value).toBe(76);
@@ -1368,7 +1368,7 @@ describe('MatSlider', () => {
13681368

13691369
// After changing the value, the control should become dirty (not pristine),
13701370
// but remain untouched.
1371-
dispatchClickEventSequence(sliderNativeElement, 0.5);
1371+
dispatchMousedownEventSequence(sliderNativeElement, 0.5);
13721372
fixture.detectChanges();
13731373

13741374
expect(sliderControl.valid).toBe(true);
@@ -1404,7 +1404,7 @@ describe('MatSlider', () => {
14041404
expect(testComponent.value).toBe(0);
14051405
expect(testComponent.slider.value).toBe(0);
14061406

1407-
dispatchClickEventSequence(sliderNativeElement, 0.1);
1407+
dispatchMousedownEventSequence(sliderNativeElement, 0.1);
14081408
fixture.detectChanges();
14091409

14101410
expect(testComponent.value).toBe(10);
@@ -1594,20 +1594,20 @@ class SliderWithTwoWayBinding {
15941594
}
15951595

15961596
/**
1597-
* Dispatches a click event sequence (consisting of moueseenter, click) from an element.
1598-
* Note: The mouse event truncates the position for the click.
1597+
* Dispatches a mousedown event sequence (consisting of moueseenter, mousedown) from an element.
1598+
* Note: The mouse event truncates the position for the event.
15991599
* @param sliderElement The mat-slider element from which the event will be dispatched.
1600-
* @param percentage The percentage of the slider where the click should occur. Used to find the
1601-
* physical location of the click.
1600+
* @param percentage The percentage of the slider where the event should occur. Used to find the
1601+
* physical location of the pointer.
16021602
*/
1603-
function dispatchClickEventSequence(sliderElement: HTMLElement, percentage: number): void {
1603+
function dispatchMousedownEventSequence(sliderElement: HTMLElement, percentage: number): void {
16041604
let trackElement = sliderElement.querySelector('.mat-slider-wrapper')!;
16051605
let dimensions = trackElement.getBoundingClientRect();
16061606
let x = dimensions.left + (dimensions.width * percentage);
16071607
let y = dimensions.top + (dimensions.height * percentage);
16081608

16091609
dispatchMouseenterEvent(sliderElement);
1610-
dispatchMouseEvent(sliderElement, 'click', x, y);
1610+
dispatchMouseEvent(sliderElement, 'mousedown', x, y);
16111611
}
16121612

16131613
/**
@@ -1687,7 +1687,7 @@ function dispatchSlideEndEvent(sliderElement: HTMLElement, percent: number,
16871687

16881688
/**
16891689
* Dispatches a mouseenter event from an element.
1690-
* Note: The mouse event truncates the position for the click.
1690+
* Note: The mouse event truncates the position for the event.
16911691
* @param element The element from which the event will be dispatched.
16921692
*/
16931693
function dispatchMouseenterEvent(element: HTMLElement): void {

src/lib/slider/slider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export const _MatSliderMixinBase:
112112
host: {
113113
'(focus)': '_onFocus()',
114114
'(blur)': '_onBlur()',
115-
'(click)': '_onClick($event)',
115+
'(mousedown)': '_onMousedown($event)',
116116
'(keydown)': '_onKeydown($event)',
117117
'(keyup)': '_onKeyup()',
118118
'(mouseenter)': '_onMouseenter()',
@@ -501,12 +501,12 @@ export class MatSlider extends _MatSliderMixinBase
501501
this._updateTickIntervalPercent();
502502
}
503503

504-
_onClick(event: MouseEvent) {
504+
_onMousedown(event: MouseEvent) {
505505
if (this.disabled) {
506506
return;
507507
}
508508

509-
let oldValue = this.value;
509+
const oldValue = this.value;
510510
this._isSliding = false;
511511
this._focusHostElement();
512512
this._updateValueFromPosition({x: event.clientX, y: event.clientY});

0 commit comments

Comments
 (0)