Skip to content

Commit

Permalink
fix(typeahead): fix close on blur (#2816)
Browse files Browse the repository at this point in the history
fixes #2588
  • Loading branch information
IlyaSurmay authored and valorkin committed Oct 10, 2017
1 parent bd04f61 commit 8bedcee
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/typeahead/typeahead.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ export class TypeaheadDirective implements OnInit, OnDestroy {

private _typeahead: ComponentLoader<TypeaheadContainerComponent>;
private _subscriptions: Subscription[] = [];
private _outsideClickListener: Function;

constructor(private ngControl: NgControl,
private element: ElementRef,
viewContainerRef: ViewContainerRef,
renderer: Renderer2,
private renderer: Renderer2,
cis: ComponentLoaderFactory) {
this._typeahead = cis.createLoader<TypeaheadContainerComponent>(
element,
Expand Down Expand Up @@ -233,7 +234,6 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
onBlur(): void {
if (this._container && !this._container.isFocused) {
this.typeaheadOnBlur.emit(this._container.active);
this.hide();
}
}

Expand Down Expand Up @@ -276,6 +276,10 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
dropup: this.dropup
});

this._outsideClickListener = this.renderer.listen('document', 'click', () => {
this.onOutsideClick();
});

this._container = this._typeahead.instance;
this._container.parent = this;
// This improves the speed as it won't have to be done for each list item
Expand All @@ -298,10 +302,17 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
hide(): void {
if (this._typeahead.isShown) {
this._typeahead.hide();
this._outsideClickListener();
this._container = null;
}
}

onOutsideClick(): void {
if (this._container && !this._container.isFocused) {
this.hide();
}
}

ngOnDestroy(): any {
// clean up subscriptions
for (const sub of this._subscriptions) {
Expand Down

0 comments on commit 8bedcee

Please sign in to comment.