@@ -131,11 +131,12 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
131
131
132
132
private _typeahead : ComponentLoader < TypeaheadContainerComponent > ;
133
133
private _subscriptions : Subscription [ ] = [ ] ;
134
+ private _outsideClickListener : Function ;
134
135
135
136
constructor ( private ngControl : NgControl ,
136
137
private element : ElementRef ,
137
138
viewContainerRef : ViewContainerRef ,
138
- renderer : Renderer2 ,
139
+ private renderer : Renderer2 ,
139
140
cis : ComponentLoaderFactory ) {
140
141
this . _typeahead = cis . createLoader < TypeaheadContainerComponent > (
141
142
element ,
@@ -233,7 +234,6 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
233
234
onBlur ( ) : void {
234
235
if ( this . _container && ! this . _container . isFocused ) {
235
236
this . typeaheadOnBlur . emit ( this . _container . active ) ;
236
- this . hide ( ) ;
237
237
}
238
238
}
239
239
@@ -276,6 +276,10 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
276
276
dropup : this . dropup
277
277
} ) ;
278
278
279
+ this . _outsideClickListener = this . renderer . listen ( 'document' , 'click' , ( ) => {
280
+ this . onOutsideClick ( ) ;
281
+ } ) ;
282
+
279
283
this . _container = this . _typeahead . instance ;
280
284
this . _container . parent = this ;
281
285
// 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 {
298
302
hide ( ) : void {
299
303
if ( this . _typeahead . isShown ) {
300
304
this . _typeahead . hide ( ) ;
305
+ this . _outsideClickListener ( ) ;
301
306
this . _container = null ;
302
307
}
303
308
}
304
309
310
+ onOutsideClick ( ) : void {
311
+ if ( this . _container && ! this . _container . isFocused ) {
312
+ this . hide ( ) ;
313
+ }
314
+ }
315
+
305
316
ngOnDestroy ( ) : any {
306
317
// clean up subscriptions
307
318
for ( const sub of this . _subscriptions ) {
0 commit comments