Skip to content

Commit 01d0993

Browse files
crisbetojosephperrott
authored andcommitted
fix(select): unable to toggle multi select option after using the mouse (#11061)
1 parent bf1bdc0 commit 01d0993

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/lib/select/select.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,29 @@ describe('MatSelect', () => {
702702
expect(host.getAttribute('aria-activedescendant')).toBe(options[0].id);
703703
}));
704704

705+
it('should restore focus to the trigger after selecting an option in multi-select mode',
706+
fakeAsync(() => {
707+
fixture.destroy();
708+
709+
const multiFixture = TestBed.createComponent(MultiSelect);
710+
const instance = multiFixture.componentInstance;
711+
712+
multiFixture.detectChanges();
713+
select = multiFixture.debugElement.query(By.css('mat-select')).nativeElement;
714+
instance.select.open();
715+
multiFixture.detectChanges();
716+
717+
// Ensure that the select isn't focused to begin with.
718+
select.blur();
719+
expect(document.activeElement).not.toBe(select, 'Expected trigger not to be focused.');
720+
721+
const option = overlayContainerElement.querySelector('mat-option')! as HTMLElement;
722+
option.click();
723+
multiFixture.detectChanges();
724+
725+
expect(document.activeElement).toBe(select, 'Expected trigger to be focused.');
726+
}));
727+
705728
});
706729

707730
describe('for options', () => {

src/lib/select/select.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
826826
.withVerticalOrientation()
827827
.withHorizontalOrientation(this._isRtl() ? 'rtl' : 'ltr');
828828

829-
this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(() => this.close());
829+
this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(() => this.close());
830830
this._keyManager.change.pipe(takeUntil(this._destroy)).subscribe(() => {
831831
if (this._panelOpen && this.panel) {
832832
this._scrollActiveOptionIntoView();
@@ -874,6 +874,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
874874
wasSelected ? option.deselect() : option.select();
875875
this._keyManager.setActiveItem(option);
876876
this._sortValues();
877+
878+
// In case the user select the option with their mouse, we
879+
// want to restore focus back to the trigger, in order to
880+
// prevent the select keyboard controls from clashing with
881+
// the ones from `mat-option`.
882+
this.focus();
877883
} else {
878884
this._clearSelection(option.value == null ? undefined : option);
879885

0 commit comments

Comments
 (0)