From a4c042b6c958f6e169252d820d47df88debd4fea Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 13 Dec 2017 22:53:00 +0100 Subject: [PATCH] fix(select): alt + arrow key not opening in single-selection mode (#8910) Fixes not being able to open a closed `mat-select` in single selection mode using the alt + up/down arrow combo. It seems like this was introduced when we added the ability go through the values using arrow keys on a closed select. --- src/lib/select/select.spec.ts | 30 ++++++++++++++++++++++++++++++ src/lib/select/select.ts | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index d0ccd49d82a..b609b414894 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -253,6 +253,36 @@ describe('MatSelect', () => { 'Expected value from second option to have been set on the model.'); })); + it('should open a single-selection select using ALT + DOWN_ARROW', fakeAsync(() => { + const {control: formControl, select: selectInstance} = fixture.componentInstance; + + expect(selectInstance.panelOpen).toBe(false, 'Expected select to be closed.'); + expect(formControl.value).toBeFalsy('Expected no initial value.'); + + const event = createKeyboardEvent('keydown', DOWN_ARROW); + Object.defineProperty(event, 'altKey', {get: () => true}); + + dispatchEvent(select, event); + + expect(selectInstance.panelOpen).toBe(true, 'Expected select to be open.'); + expect(formControl.value).toBeFalsy('Expected value not to have changed.'); + })); + + it('should open a single-selection select using ALT + UP_ARROW', fakeAsync(() => { + const {control: formControl, select: selectInstance} = fixture.componentInstance; + + expect(selectInstance.panelOpen).toBe(false, 'Expected select to be closed.'); + expect(formControl.value).toBeFalsy('Expected no initial value.'); + + const event = createKeyboardEvent('keydown', UP_ARROW); + Object.defineProperty(event, 'altKey', {get: () => true}); + + dispatchEvent(select, event); + + expect(selectInstance.panelOpen).toBe(true, 'Expected select to be open.'); + expect(formControl.value).toBeFalsy('Expected value not to have changed.'); + })); + it('should be able to select options by typing on a closed select', fakeAsync(() => { const formControl = fixture.componentInstance.control; const options = fixture.componentInstance.options.toArray(); diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index 09eea167a9f..a0e9df39e8f 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -644,7 +644,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit, const isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW; const isOpenKey = keyCode === ENTER || keyCode === SPACE; - if (isOpenKey || (this.multiple && isArrowKey)) { + if (isOpenKey || ((this.multiple || event.altKey) && isArrowKey)) { event.preventDefault(); // prevents the page from scrolling down when pressing space this.open(); } else if (!this.multiple) {