Skip to content

fix(select): error when navigating via keyboard to reset option on a closed select #15160

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
Feb 22, 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
15 changes: 15 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@ describe('MatSelect', () => {
flush();
})));

it('should not throw when reaching a reset option using the arrow keys on a closed select',
fakeAsync(() => {
fixture.componentInstance.foods =
[{value: 'steak-0', viewValue: 'Steak'}, {value: null, viewValue: 'None'}];
fixture.detectChanges();
fixture.componentInstance.control.setValue('steak-0');

expect(() => {
dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
fixture.detectChanges();
}).not.toThrow();

flush();
}));

it('should open a single-selection select using ALT + DOWN_ARROW', fakeAsync(() => {
const {control: formControl, select: selectInstance} = fixture.componentInstance;

Expand Down
8 changes: 5 additions & 3 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
event.preventDefault(); // prevents the page from scrolling down when pressing space
this.open();
} else if (!this.multiple) {
const selectedOption = this.selected;
const previouslySelectedOption = this.selected;

if (keyCode === HOME || keyCode === END) {
keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive();
Expand All @@ -714,10 +714,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
manager.onKeydown(event);
}

const selectedOption = this.selected;

// Since the value has changed, we need to announce it ourselves.
// @breaking-change 8.0.0 remove null check for _liveAnnouncer.
if (this._liveAnnouncer && selectedOption !== this.selected) {
this._liveAnnouncer.announce((this.selected as MatOption).viewValue);
if (this._liveAnnouncer && selectedOption && previouslySelectedOption !== selectedOption) {
this._liveAnnouncer.announce((selectedOption as MatOption).viewValue);
}
}
}
Expand Down