Skip to content

fix(select): handle home and end keys on closed select #13278

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
Oct 2, 2018
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: 23 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,29 @@ describe('MatSelect', () => {
'Expected value from second option to have been set on the model.');
}));

it('should select first/last options via the HOME/END keys on a closed select',
fakeAsync(() => {
const formControl = fixture.componentInstance.control;
const firstOption = fixture.componentInstance.options.first;
const lastOption = fixture.componentInstance.options.last;

expect(formControl.value).toBeFalsy('Expected no initial value.');

const endEvent = dispatchKeyboardEvent(select, 'keydown', END);

expect(endEvent.defaultPrevented).toBe(true);
expect(lastOption.selected).toBe(true, 'Expected last option to be selected.');
expect(formControl.value).toBe(lastOption.value,
'Expected value from last option to have been set on the model.');

const homeEvent = dispatchKeyboardEvent(select, 'keydown', HOME);

expect(homeEvent.defaultPrevented).toBe(true);
expect(firstOption.selected).toBe(true, 'Expected first option to be selected.');
expect(formControl.value).toBe(firstOption.value,
'Expected value from first option to have been set on the model.');
}));

it('should resume focus from selected item after selecting via click', fakeAsync(() => {
const formControl = fixture.componentInstance.control;
const options = fixture.componentInstance.options.toArray();
Expand Down
10 changes: 8 additions & 2 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,15 +691,21 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
private _handleClosedKeydown(event: KeyboardEvent): void {
const keyCode = event.keyCode;
const isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW ||
keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW;
keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW;
const isOpenKey = keyCode === ENTER || keyCode === SPACE;
const manager = this._keyManager;

// Open the select on ALT + arrow key to match the native <select>
if (isOpenKey || ((this.multiple || event.altKey) && isArrowKey)) {
event.preventDefault(); // prevents the page from scrolling down when pressing space
this.open();
} else if (!this.multiple) {
this._keyManager.onKeydown(event);
if (keyCode === HOME || keyCode === END) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to just add this to ListKeyManager?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to have them on the ListKeyManager, but we removed them because they don't make sense in all cases (e.g. on an input).

keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive();
event.preventDefault();
} else {
manager.onKeydown(event);
}
}
}

Expand Down