Skip to content

Commit 8bedcee

Browse files
IlyaSurmayvalorkin
authored andcommitted
fix(typeahead): fix close on blur (#2816)
fixes #2588
1 parent bd04f61 commit 8bedcee

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/typeahead/typeahead.directive.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,12 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
131131

132132
private _typeahead: ComponentLoader<TypeaheadContainerComponent>;
133133
private _subscriptions: Subscription[] = [];
134+
private _outsideClickListener: Function;
134135

135136
constructor(private ngControl: NgControl,
136137
private element: ElementRef,
137138
viewContainerRef: ViewContainerRef,
138-
renderer: Renderer2,
139+
private renderer: Renderer2,
139140
cis: ComponentLoaderFactory) {
140141
this._typeahead = cis.createLoader<TypeaheadContainerComponent>(
141142
element,
@@ -233,7 +234,6 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
233234
onBlur(): void {
234235
if (this._container && !this._container.isFocused) {
235236
this.typeaheadOnBlur.emit(this._container.active);
236-
this.hide();
237237
}
238238
}
239239

@@ -276,6 +276,10 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
276276
dropup: this.dropup
277277
});
278278

279+
this._outsideClickListener = this.renderer.listen('document', 'click', () => {
280+
this.onOutsideClick();
281+
});
282+
279283
this._container = this._typeahead.instance;
280284
this._container.parent = this;
281285
// This improves the speed as it won't have to be done for each list item
@@ -298,10 +302,17 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
298302
hide(): void {
299303
if (this._typeahead.isShown) {
300304
this._typeahead.hide();
305+
this._outsideClickListener();
301306
this._container = null;
302307
}
303308
}
304309

310+
onOutsideClick(): void {
311+
if (this._container && !this._container.isFocused) {
312+
this.hide();
313+
}
314+
}
315+
305316
ngOnDestroy(): any {
306317
// clean up subscriptions
307318
for (const sub of this._subscriptions) {

0 commit comments

Comments
 (0)