Skip to content
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

fix(material/autocomplete): outside click in Angular zone. #24817

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
25 changes: 25 additions & 0 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,31 @@ describe('MDC-based MatAutocomplete', () => {

expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
});

it('should emit from `autocomplete.closed` after click outside inside the NgZone', fakeAsync(() => {
const inZoneSpy = jasmine.createSpy('in zone spy');

const fixture = createComponent(SimpleAutocomplete, [
{provide: NgZone, useFactory: () => new NgZone({enableLongStackTrace: false})},
]);
const ngZone = TestBed.inject(NgZone);
fixture.detectChanges();

fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
flush();

const subscription = fixture.componentInstance.trigger.autocomplete.closed.subscribe(() =>
inZoneSpy(NgZone.isInAngularZone()),
);
ngZone.onStable.emit(null);

dispatchFakeEvent(document, 'click');

expect(inZoneSpy).toHaveBeenCalledWith(true);

subscription.unsubscribe();
}));
});

const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
Expand Down
7 changes: 6 additions & 1 deletion src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ export abstract class _MatAutocompleteTriggerBase

if (this.panelOpen) {
// Only emit if the panel was visible.
this.autocomplete.closed.emit();
// The `NgZone.onStable` always emits outside of the Angular zone,
// so all the subscriptions from `_subscribeToClosingActions()` are also outside of the Angular zone.
// We should manually run in Angular zone to update UI after panel closing.
this._zone.run(() => {
this.autocomplete.closed.emit();
});
}

this.autocomplete._isOpen = this._overlayAttached = false;
Expand Down
25 changes: 25 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3371,6 +3371,31 @@ describe('MatAutocomplete', () => {

expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
});

it('should emit from `autocomplete.closed` after click outside inside the NgZone', fakeAsync(() => {
const inZoneSpy = jasmine.createSpy('in zone spy');

const fixture = createComponent(SimpleAutocomplete, [
{provide: NgZone, useFactory: () => new NgZone({enableLongStackTrace: false})},
crisbeto marked this conversation as resolved.
Show resolved Hide resolved
]);
const ngZone = TestBed.inject(NgZone);
fixture.detectChanges();
crisbeto marked this conversation as resolved.
Show resolved Hide resolved

fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
flush();

const subscription = fixture.componentInstance.trigger.autocomplete.closed.subscribe(() =>
inZoneSpy(NgZone.isInAngularZone()),
);
ngZone.onStable.emit(null);

dispatchFakeEvent(document, 'click');

expect(inZoneSpy).toHaveBeenCalledWith(true);

subscription.unsubscribe();
}));
});

const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
Expand Down