Skip to content

fix(select): unable to toggle multi select option after using the mouse #11061

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
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 @@ -702,6 +702,29 @@ describe('MatSelect', () => {
expect(host.getAttribute('aria-activedescendant')).toBe(options[0].id);
}));

it('should restore focus to the trigger after selecting an option in multi-select mode',
fakeAsync(() => {
fixture.destroy();

const multiFixture = TestBed.createComponent(MultiSelect);
const instance = multiFixture.componentInstance;

multiFixture.detectChanges();
select = multiFixture.debugElement.query(By.css('mat-select')).nativeElement;
instance.select.open();
multiFixture.detectChanges();

// Ensure that the select isn't focused to begin with.
select.blur();
expect(document.activeElement).not.toBe(select, 'Expected trigger not to be focused.');

const option = overlayContainerElement.querySelector('mat-option')! as HTMLElement;
option.click();
multiFixture.detectChanges();

expect(document.activeElement).toBe(select, 'Expected trigger to be focused.');
}));

});

describe('for options', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
.withVerticalOrientation()
.withHorizontalOrientation(this._isRtl() ? 'rtl' : 'ltr');

this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(() => this.close());
this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(() => this.close());
this._keyManager.change.pipe(takeUntil(this._destroy)).subscribe(() => {
if (this._panelOpen && this.panel) {
this._scrollActiveOptionIntoView();
Expand Down Expand Up @@ -874,6 +874,12 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
wasSelected ? option.deselect() : option.select();
this._keyManager.setActiveItem(option);
this._sortValues();

// In case the user select the option with their mouse, we
// want to restore focus back to the trigger, in order to
// prevent the select keyboard controls from clashing with
// the ones from `mat-option`.
this.focus();
} else {
this._clearSelection(option.value == null ? undefined : option);

Expand Down