Skip to content
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

fix(select): highlighted option not updated if value is reset while closed #17213

Merged
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
28 changes: 28 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,34 @@ describe('MatSelect', () => {
flush();
}));

it('should go back to first option if value is reset after interacting using the' +
'arrow keys on a closed select', fakeAsync(() => {
const formControl = fixture.componentInstance.control;
const options = fixture.componentInstance.options.toArray();

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

dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
flush();

expect(options[0].selected).toBe(true, 'Expected first option to be selected.');
expect(formControl.value).toBe(options[0].value,
'Expected value from first option to have been set on the model.');

formControl.reset();
fixture.detectChanges();

expect(options[0].selected).toBe(false, 'Expected first option to be deselected.');
expect(formControl.value).toBeFalsy('Expected value to be reset.');

dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
flush();

expect(options[0].selected).toBe(true, 'Expected first option to be selected again.');
expect(formControl.value).toBe(options[0].value,
'Expected value from first option to have been set on the model again.');
}));

it('should select first/last options via the HOME/END keys on a closed select',
fakeAsync(() => {
const formControl = fixture.componentInstance.control;
Expand Down
4 changes: 4 additions & 0 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,10 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
// mode, because we don't know what option the user interacted with last.
if (correspondingOption) {
this._keyManager.setActiveItem(correspondingOption);
} else if (!this.panelOpen) {
// Otherwise reset the highlighted option. Note that we only want to do this while
// closed, because doing it while open can shift the user's focus unnecessarily.
this._keyManager.setActiveItem(-1);
}
}

Expand Down