Skip to content

fix(material/autocomplete): not closing when clicking outside while propagation is stopped #17381

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ describe('MDC-based MatAutocomplete', () => {
.toEqual('');
}));

it('should close the panel when clicking away and propagation is stopped', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
zone.simulateZoneExit();

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

fixture.nativeElement.querySelector('.stop-propagation').click();
fixture.detectChanges();

expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
}));

it('should close the panel when the user taps away on a touch device', fakeAsync(() => {
dispatchFakeEvent(input, 'focus');
fixture.detectChanges();
Expand Down Expand Up @@ -3374,6 +3388,8 @@ const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
<span>{{ state.code }}: {{ state.name }}</span>
</mat-option>
</mat-autocomplete>

<button class="stop-propagation" (click)="$event.stopPropagation()">Click me</button>
`;

@Component({template: SIMPLE_AUTOCOMPLETE_TEMPLATE})
Expand Down
8 changes: 5 additions & 3 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,12 @@ export abstract class _MatAutocompleteTriggerBase

/** Stream of clicks outside of the autocomplete panel. */
private _getOutsideClickStream(): Observable<any> {
// Use capturing so we can close even if propagation is stopped.
const eventOptions = {capture: true};
return merge(
fromEvent(this._document, 'click') as Observable<MouseEvent>,
fromEvent(this._document, 'auxclick') as Observable<MouseEvent>,
fromEvent(this._document, 'touchend') as Observable<TouchEvent>,
fromEvent(this._document, 'click', eventOptions) as Observable<MouseEvent>,
fromEvent(this._document, 'auxclick', eventOptions) as Observable<MouseEvent>,
fromEvent(this._document, 'touchend', eventOptions) as Observable<TouchEvent>,
).pipe(
filter(event => {
// If we're in the Shadow DOM, the event target will be the shadow root, so we have to
Expand Down
16 changes: 16 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ describe('MatAutocomplete', () => {
.toEqual('');
}));

it('should close the panel when clicking away and propagation is stopped', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
dispatchFakeEvent(input, 'focusin');
fixture.detectChanges();
zone.simulateZoneExit();

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

fixture.nativeElement.querySelector('.stop-propagation').click();
fixture.detectChanges();

expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
}));

it('should close the panel when the user taps away on a touch device', fakeAsync(() => {
dispatchFakeEvent(input, 'focus');
fixture.detectChanges();
Expand Down Expand Up @@ -3379,6 +3393,8 @@ const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
<span>{{ state.code }}: {{ state.name }}</span>
</mat-option>
</mat-autocomplete>

<button class="stop-propagation" (click)="$event.stopPropagation()">Click me</button>
`;

@Component({template: SIMPLE_AUTOCOMPLETE_TEMPLATE})
Expand Down