Skip to content

Commit

Permalink
fix(autocomplete): not resetting completely when overlay is detached …
Browse files Browse the repository at this point in the history
…externally (#8515)

Fixes the autocomplete not closing correctly when its `OverlayRef` is detached externally (e.g. by a scroll strategy).
  • Loading branch information
crisbeto authored and andrewseguin committed Dec 13, 2017
1 parent 92bc503 commit a8cd033
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
OverlayRef,
OverlayConfig,
PositionStrategy,
RepositionScrollStrategy,
ScrollStrategy,
} from '@angular/cdk/overlay';
import {TemplatePortal} from '@angular/cdk/portal';
Expand Down Expand Up @@ -67,7 +66,7 @@ export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY =

/** @docs-private */
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay: Overlay):
() => RepositionScrollStrategy {
() => ScrollStrategy {
return () => overlay.scrollStrategies.reposition();
}

Expand Down Expand Up @@ -195,7 +194,8 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
this.optionSelections,
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._panelOpen)),
this._escapeEventStream,
this._outsideClickStream
this._outsideClickStream,
this._overlayRef ? this._overlayRef.detachments() : observableOf()
);
}

Expand Down
26 changes: 25 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Direction, Directionality} from '@angular/cdk/bidi';
import {DOWN_ARROW, ENTER, ESCAPE, SPACE, UP_ARROW, TAB} from '@angular/cdk/keycodes';
import {OverlayContainer} from '@angular/cdk/overlay';
import {OverlayContainer, Overlay} from '@angular/cdk/overlay';
import {map} from 'rxjs/operators/map';
import {startWith} from 'rxjs/operators/startWith';
import {ScrollDispatcher} from '@angular/cdk/scrolling';
Expand Down Expand Up @@ -45,6 +45,7 @@ import {
MatAutocompleteModule,
MatAutocompleteSelectedEvent,
MatAutocompleteTrigger,
MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
} from './index';


Expand Down Expand Up @@ -1519,6 +1520,29 @@ describe('MatAutocomplete', () => {
}));


it('should reset correctly when closed programmatically', async(() => {
TestBed.overrideProvider(MAT_AUTOCOMPLETE_SCROLL_STRATEGY, {
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
deps: [Overlay]
});

const fixture = TestBed.createComponent(SimpleAutocomplete);
fixture.detectChanges();
const trigger = fixture.componentInstance.trigger;

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

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

scrolledSubject.next();
fixture.detectChanges();

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

});

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

0 comments on commit a8cd033

Please sign in to comment.