Skip to content

fix(select): don't select active item when tabbing away while closed #18797

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
Apr 20, 2020
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
30 changes: 29 additions & 1 deletion src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TAB,
UP_ARROW,
A,
ESCAPE,
} from '@angular/cdk/keycodes';
import {OverlayContainer} from '@angular/cdk/overlay';
import {Platform} from '@angular/cdk/platform';
Expand Down Expand Up @@ -3102,7 +3103,7 @@ describe('MatSelect', () => {
expect(spy).toHaveBeenCalledWith('steak-0');
}));

it('should set the value when options are clicked', fakeAsync(() => {
it('should select the active option when tabbing away while open', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicSelectWithoutForms);
fixture.detectChanges();
const select = fixture.nativeElement.querySelector('.mat-select');
Expand All @@ -3129,6 +3130,33 @@ describe('MatSelect', () => {
expect(trigger.textContent).toContain('Sandwich');
}));

it('should not select the active option when tabbing away while close', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicSelectWithoutForms);
fixture.detectChanges();
const select = fixture.nativeElement.querySelector('.mat-select');

expect(fixture.componentInstance.selectedFood).toBeFalsy();

const trigger = fixture.nativeElement.querySelector('.mat-select-trigger');

trigger.click();
fixture.detectChanges();
flush();

dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
fixture.detectChanges();
dispatchKeyboardEvent(select, 'keydown', DOWN_ARROW);
fixture.detectChanges();
dispatchKeyboardEvent(select, 'keydown', ESCAPE);
fixture.detectChanges();

dispatchKeyboardEvent(select, 'keydown', TAB);
fixture.detectChanges();
flush();

expect(fixture.componentInstance.selectedFood).toBeFalsy();
}));

it('should not change the multiple value selection when tabbing away', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicSelectWithoutFormsMultiple);
fixture.detectChanges();
Expand Down
20 changes: 11 additions & 9 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,16 +936,18 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
.withAllowedModifierKeys(['shiftKey']);

this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(() => {
// Select the active item when tabbing away. This is consistent with how the native
// select behaves. Note that we only want to do this in single selection mode.
if (!this.multiple && this._keyManager.activeItem) {
this._keyManager.activeItem._selectViaInteraction();
}
if (this.panelOpen) {
// Select the active item when tabbing away. This is consistent with how the native
// select behaves. Note that we only want to do this in single selection mode.
if (!this.multiple && this._keyManager.activeItem) {
this._keyManager.activeItem._selectViaInteraction();
}

// Restore focus to the trigger before closing. Ensures that the focus
// position won't be lost if the user got focus into the overlay.
this.focus();
this.close();
// Restore focus to the trigger before closing. Ensures that the focus
// position won't be lost if the user got focus into the overlay.
this.focus();
this.close();
}
});

this._keyManager.change.pipe(takeUntil(this._destroy)).subscribe(() => {
Expand Down