Skip to content

fix(slider): don't handle keyboard events with modifier keys #14675

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 3, 2019
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
23 changes: 22 additions & 1 deletion src/lib/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
createKeyboardEvent,
} from '@angular/cdk/testing';
import {Component, DebugElement, Type, ViewChild} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
Expand Down Expand Up @@ -995,7 +996,7 @@ describe('MatSlider', () => {
expect(sliderInstance.value).toBe(0);
});

it(`should take not action for presses of keys it doesn't care about`, () => {
it(`should take no action for presses of keys it doesn't care about`, () => {
sliderInstance.value = 50;

expect(testComponent.onChange).not.toHaveBeenCalled();
Expand All @@ -1008,6 +1009,26 @@ describe('MatSlider', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
expect(sliderInstance.value).toBe(50);
});

it('should ignore events modifier keys', () => {
sliderInstance.value = 0;

[
UP_ARROW, DOWN_ARROW, RIGHT_ARROW,
LEFT_ARROW, PAGE_DOWN, PAGE_UP, HOME, END
].forEach(key => {
const event = createKeyboardEvent('keydown', key);
Object.defineProperty(event, 'altKey', {get: () => true});
dispatchEvent(sliderNativeElement, event);
fixture.detectChanges();
expect(event.defaultPrevented).toBe(false);
});

expect(testComponent.onInput).not.toHaveBeenCalled();
expect(testComponent.onChange).not.toHaveBeenCalled();
expect(sliderInstance.value).toBe(0);
});

});

describe('slider with direction and invert', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
PAGE_UP,
RIGHT_ARROW,
UP_ARROW,
hasModifierKey,
} from '@angular/cdk/keycodes';
import {
Attribute,
Expand Down Expand Up @@ -582,9 +583,11 @@ export class MatSlider extends _MatSliderMixinBase
}

_onKeydown(event: KeyboardEvent) {
if (this.disabled) { return; }
if (this.disabled || hasModifierKey(event)) {
return;
}

let oldValue = this.value;
const oldValue = this.value;

switch (event.keyCode) {
case PAGE_UP:
Expand Down