Skip to content

Commit 4f97232

Browse files
crisbetojosephperrott
authored andcommitted
fix(autocomplete): panelClosingActions emitting twice in some cases (#8998)
1 parent 5674c83 commit 4f97232

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,16 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
167167

168168
/** Closes the autocomplete suggestion panel. */
169169
closePanel(): void {
170-
if (this._overlayRef && this._overlayRef.hasAttached()) {
171-
this._overlayRef.detach();
172-
this._closingActionsSubscription.unsubscribe();
173-
}
174-
175170
this._resetLabel();
176171

177172
if (this._panelOpen) {
178173
this.autocomplete._isOpen = this._panelOpen = false;
179174

175+
if (this._overlayRef && this._overlayRef.hasAttached()) {
176+
this._overlayRef.detach();
177+
this._closingActionsSubscription.unsubscribe();
178+
}
179+
180180
// We need to trigger change detection manually, because
181181
// `fromEvent` doesn't seem to do it at the proper time.
182182
// This ensures that the label is reset when the
@@ -195,7 +195,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
195195
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._panelOpen)),
196196
this._escapeEventStream,
197197
this._outsideClickStream,
198-
this._overlayRef ? this._overlayRef.detachments() : observableOf()
198+
this._overlayRef ?
199+
this._overlayRef.detachments().pipe(filter(() => this._panelOpen)) :
200+
observableOf()
199201
);
200202
}
201203

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,27 +1520,26 @@ describe('MatAutocomplete', () => {
15201520
}));
15211521

15221522

1523-
it('should reset correctly when closed programmatically', async(() => {
1523+
it('should reset correctly when closed programmatically', fakeAsync(() => {
15241524
TestBed.overrideProvider(MAT_AUTOCOMPLETE_SCROLL_STRATEGY, {
15251525
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
15261526
deps: [Overlay]
15271527
});
15281528

1529-
const fixture = TestBed.createComponent(SimpleAutocomplete);
1529+
const fixture = createComponent(SimpleAutocomplete);
15301530
fixture.detectChanges();
15311531
const trigger = fixture.componentInstance.trigger;
15321532

15331533
trigger.openPanel();
15341534
fixture.detectChanges();
1535+
zone.simulateZoneExit();
15351536

1536-
fixture.whenStable().then(() => {
1537-
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
1537+
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
15381538

1539-
scrolledSubject.next();
1540-
fixture.detectChanges();
1539+
scrolledSubject.next();
1540+
fixture.detectChanges();
15411541

1542-
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
1543-
});
1542+
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
15441543
}));
15451544

15461545
});

0 commit comments

Comments
 (0)