Skip to content

Commit ed857c9

Browse files
crisbetoandrewseguin
authored andcommitted
fix(slider): changing value on right click (#14083)
Fixes the slider re-assigning the value when it's right clicked.
1 parent c55b78e commit ed857c9

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/lib/slider/slider.spec.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {
1010
RIGHT_ARROW,
1111
UP_ARROW,
1212
} from '@angular/cdk/keycodes';
13-
import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing';
13+
import {
14+
dispatchFakeEvent,
15+
dispatchKeyboardEvent,
16+
dispatchMouseEvent,
17+
createMouseEvent,
18+
dispatchEvent,
19+
} from '@angular/cdk/testing';
1420
import {Component, DebugElement, ViewChild, Type} from '@angular/core';
1521
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
1622
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
@@ -68,6 +74,14 @@ describe('MatSlider', () => {
6874
expect(sliderInstance.value).toBe(19);
6975
});
7076

77+
it('should not update when pressing the right mouse button', () => {
78+
expect(sliderInstance.value).toBe(0);
79+
80+
dispatchMousedownEventSequence(sliderNativeElement, 0.19, 1);
81+
82+
expect(sliderInstance.value).toBe(0);
83+
});
84+
7185
it('should update the value on a slide', () => {
7286
expect(sliderInstance.value).toBe(0);
7387

@@ -1599,15 +1613,17 @@ class SliderWithTwoWayBinding {
15991613
* @param sliderElement The mat-slider element from which the event will be dispatched.
16001614
* @param percentage The percentage of the slider where the event should occur. Used to find the
16011615
* physical location of the pointer.
1616+
* @param button Button that should be held down when starting to drag the slider.
16021617
*/
1603-
function dispatchMousedownEventSequence(sliderElement: HTMLElement, percentage: number): void {
1604-
let trackElement = sliderElement.querySelector('.mat-slider-wrapper')!;
1605-
let dimensions = trackElement.getBoundingClientRect();
1606-
let x = dimensions.left + (dimensions.width * percentage);
1607-
let y = dimensions.top + (dimensions.height * percentage);
1618+
function dispatchMousedownEventSequence(sliderElement: HTMLElement, percentage: number,
1619+
button = 0): void {
1620+
const trackElement = sliderElement.querySelector('.mat-slider-wrapper')!;
1621+
const dimensions = trackElement.getBoundingClientRect();
1622+
const x = dimensions.left + (dimensions.width * percentage);
1623+
const y = dimensions.top + (dimensions.height * percentage);
16081624

16091625
dispatchMouseenterEvent(sliderElement);
1610-
dispatchMouseEvent(sliderElement, 'mousedown', x, y);
1626+
dispatchEvent(sliderElement, createMouseEvent('mousedown', x, y, button));
16111627
}
16121628

16131629
/**

src/lib/slider/slider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,9 @@ export class MatSlider extends _MatSliderMixinBase
502502
}
503503

504504
_onMousedown(event: MouseEvent) {
505-
if (this.disabled) {
505+
// Don't do anything if the slider is disabled or the
506+
// user is using anything other than the main mouse button.
507+
if (this.disabled || event.button !== 0) {
506508
return;
507509
}
508510

0 commit comments

Comments
 (0)