Skip to content

fix(material/autocomplete): closing immediately when input is focused programmatically #21081

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
19 changes: 19 additions & 0 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
MockNgZone,
typeInElement,
} from '../../cdk/testing/private';
Expand Down Expand Up @@ -1374,6 +1375,24 @@ describe('MDC-based MatAutocomplete', () => {
.toBeFalsy();
}));

it('should not close when a click event occurs on the outside while the panel has focus',
fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;

input.focus();
flush();
fixture.detectChanges();

expect(document.activeElement).toBe(input, 'Expected input to be focused.');
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');

dispatchMouseEvent(document.body, 'click');
fixture.detectChanges();

expect(document.activeElement).toBe(input, 'Expected input to continue to be focused.');
expect(trigger.panelOpen).toBe(true, 'Expected panel to stay open.');
}));

it('should reset the active option when closing with the escape key', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;

Expand Down
5 changes: 5 additions & 0 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ export abstract class _MatAutocompleteTriggerBase
return (
this._overlayAttached &&
clickTarget !== this._element.nativeElement &&
// Normally focus moves inside `mousedown` so this condition will almost always be
// true. Its main purpose is to handle the case where the input is focused from an
// outside click which propagates up to the `body` listener within the same sequence
// and causes the panel to close immediately (see #3106).
this._document.activeElement !== this._element.nativeElement &&
(!formField || !formField.contains(clickTarget)) &&
(!customOrigin || !customOrigin.contains(clickTarget)) &&
!!this._overlayRef &&
Expand Down
21 changes: 20 additions & 1 deletion src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
dispatchFakeEvent,
dispatchKeyboardEvent,
typeInElement,
} from '../../cdk/testing/private';
dispatchMouseEvent,
} from '@angular/cdk/testing/private';
import {
ChangeDetectionStrategy,
Component,
Expand Down Expand Up @@ -1357,6 +1358,24 @@ describe('MatAutocomplete', () => {
.toBeFalsy();
}));

it('should not close when a click event occurs on the outside while the panel has focus',
fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;

input.focus();
flush();
fixture.detectChanges();

expect(document.activeElement).toBe(input, 'Expected input to be focused.');
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');

dispatchMouseEvent(document.body, 'click');
fixture.detectChanges();

expect(document.activeElement).toBe(input, 'Expected input to continue to be focused.');
expect(trigger.panelOpen).toBe(true, 'Expected panel to stay open.');
}));

it('should reset the active option when closing with the escape key', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;

Expand Down