Skip to content

Commit a3dba76

Browse files
crisbetojosephperrott
authored andcommitted
fix(select): not resuming keyboard selection after clicking on single-select option (#11882)
1 parent 4f3cbfe commit a3dba76

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/lib/select/select.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,28 @@ describe('MatSelect', () => {
282282
'Expected value from second option to have been set on the model.');
283283
}));
284284

285+
it('should resume focus from selected item after selecting via click', fakeAsync(() => {
286+
const formControl = fixture.componentInstance.control;
287+
const options = fixture.componentInstance.options.toArray();
288+
289+
expect(formControl.value).toBeFalsy('Expected no initial value.');
290+
291+
fixture.componentInstance.select.open();
292+
fixture.detectChanges();
293+
flush();
294+
295+
(overlayContainerElement.querySelectorAll('mat-option')[3] as HTMLElement).click();
296+
fixture.detectChanges();
297+
flush();
298+
299+
expect(formControl.value).toBe(options[3].value);
300+
301+
dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
302+
fixture.detectChanges();
303+
304+
expect(formControl.value).toBe(options[4].value);
305+
}));
306+
285307
it('should select options via LEFT/RIGHT arrow keys on a closed select', fakeAsync(() => {
286308
const formControl = fixture.componentInstance.control;
287309
const options = fixture.componentInstance.options.toArray();

src/lib/select/select.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,12 +892,14 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
892892
} else {
893893
option.selected ? this._selectionModel.select(option) : this._selectionModel.deselect(option);
894894

895+
if (isUserInput) {
896+
this._keyManager.setActiveItem(option);
897+
}
898+
895899
if (this.multiple) {
896900
this._sortValues();
897901

898902
if (isUserInput) {
899-
this._keyManager.setActiveItem(option);
900-
901903
// In case the user selected the option with their mouse, we
902904
// want to restore focus back to the trigger, in order to
903905
// prevent the select keyboard controls from clashing with

0 commit comments

Comments
 (0)