Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typeahead): fix close on blur #2816

Merged
merged 2 commits into from
Oct 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 comment was marked as off-topic.

This comment was marked as off-topic.

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