Skip to content

Commit

Permalink
fix(material/autocomplete): outside click in Angular zone. (#24817)
Browse files Browse the repository at this point in the history
Fixes a bug in Angular Material `autocomplete` when outside click doesn't trigger `changeDetection`.

Fixes #24811

(cherry picked from commit 4e5e286)
  • Loading branch information
volvachev authored and andrewseguin committed Apr 22, 2022
1 parent 68d09dd commit 86a1e8c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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 @@ -3135,6 +3135,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 @@ -259,7 +259,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 @@ -3141,6 +3141,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})},
]);
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

0 comments on commit 86a1e8c

Please sign in to comment.