Skip to content

Commit 8e14343

Browse files
crisbetoandrewseguin
authored andcommitted
fix(autocomplete): not resetting completely when overlay is detached externally (#8515)
Fixes the autocomplete not closing correctly when its `OverlayRef` is detached externally (e.g. by a scroll strategy).
1 parent ae639da commit 8e14343

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
OverlayRef,
1414
OverlayConfig,
1515
PositionStrategy,
16-
RepositionScrollStrategy,
1716
ScrollStrategy,
1817
} from '@angular/cdk/overlay';
1918
import {TemplatePortal} from '@angular/cdk/portal';
@@ -67,7 +66,7 @@ export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY =
6766

6867
/** @docs-private */
6968
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
70-
() => RepositionScrollStrategy {
69+
() => ScrollStrategy {
7170
return () => overlay.scrollStrategies.reposition();
7271
}
7372

@@ -195,7 +194,8 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
195194
this.optionSelections,
196195
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._panelOpen)),
197196
this._escapeEventStream,
198-
this._outsideClickStream
197+
this._outsideClickStream,
198+
this._overlayRef ? this._overlayRef.detachments() : observableOf()
199199
);
200200
}
201201

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Direction, Directionality} from '@angular/cdk/bidi';
22
import {DOWN_ARROW, ENTER, ESCAPE, SPACE, UP_ARROW, TAB} from '@angular/cdk/keycodes';
3-
import {OverlayContainer} from '@angular/cdk/overlay';
3+
import {OverlayContainer, Overlay} from '@angular/cdk/overlay';
44
import {map} from 'rxjs/operators/map';
55
import {startWith} from 'rxjs/operators/startWith';
66
import {ScrollDispatcher} from '@angular/cdk/scrolling';
@@ -45,6 +45,7 @@ import {
4545
MatAutocompleteModule,
4646
MatAutocompleteSelectedEvent,
4747
MatAutocompleteTrigger,
48+
MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
4849
} from './index';
4950

5051

@@ -1519,6 +1520,29 @@ describe('MatAutocomplete', () => {
15191520
}));
15201521

15211522

1523+
it('should reset correctly when closed programmatically', async(() => {
1524+
TestBed.overrideProvider(MAT_AUTOCOMPLETE_SCROLL_STRATEGY, {
1525+
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
1526+
deps: [Overlay]
1527+
});
1528+
1529+
const fixture = TestBed.createComponent(SimpleAutocomplete);
1530+
fixture.detectChanges();
1531+
const trigger = fixture.componentInstance.trigger;
1532+
1533+
trigger.openPanel();
1534+
fixture.detectChanges();
1535+
1536+
fixture.whenStable().then(() => {
1537+
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
1538+
1539+
scrolledSubject.next();
1540+
fixture.detectChanges();
1541+
1542+
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
1543+
});
1544+
}));
1545+
15221546
});
15231547

15241548
it('should have correct width when opened', () => {

0 commit comments

Comments
 (0)