Skip to content

Commit

Permalink
fix(select): alt + arrow key not opening in single-selection mode (#8910
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
crisbeto authored and andrewseguin committed Dec 13, 2017
1 parent 9105302 commit a4c042b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a4c042b

Please sign in to comment.