@@ -166,8 +166,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
166
166
}
167
167
168
168
/** Whether or not the autocomplete panel is open. */
169
- get panelOpen ( ) : boolean { return this . _panelOpen && this . autocomplete . showPanel ; }
170
- private _panelOpen : boolean = false ;
169
+ get panelOpen ( ) : boolean {
170
+ return this . _overlayAttached && this . autocomplete . showPanel ;
171
+ }
172
+ private _overlayAttached : boolean = false ;
171
173
172
174
/** Opens the autocomplete suggestion panel. */
173
175
openPanel ( ) : void {
@@ -179,24 +181,30 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
179
181
closePanel ( ) : void {
180
182
this . _resetLabel ( ) ;
181
183
182
- if ( this . _panelOpen ) {
183
- this . autocomplete . _isOpen = this . _panelOpen = false ;
184
+ if ( ! this . _overlayAttached ) {
185
+ return ;
186
+ }
187
+
188
+ if ( this . panelOpen ) {
189
+ // Only emit if the panel was visible.
184
190
this . autocomplete . closed . emit ( ) ;
191
+ }
185
192
186
- if ( this . _overlayRef && this . _overlayRef . hasAttached ( ) ) {
187
- this . _overlayRef . detach ( ) ;
188
- this . _closingActionsSubscription . unsubscribe ( ) ;
189
- }
193
+ this . autocomplete . _isOpen = this . _overlayAttached = false ;
190
194
191
- // Note that in some cases this can end up being called after the component is destroyed.
192
- // Add a check to ensure that we don't try to run change detection on a destroyed view.
193
- if ( ! this . _componentDestroyed ) {
194
- // We need to trigger change detection manually, because
195
- // `fromEvent` doesn't seem to do it at the proper time.
196
- // This ensures that the label is reset when the
197
- // user clicks outside.
198
- this . _changeDetectorRef . detectChanges ( ) ;
199
- }
195
+ if ( this . _overlayRef && this . _overlayRef . hasAttached ( ) ) {
196
+ this . _overlayRef . detach ( ) ;
197
+ this . _closingActionsSubscription . unsubscribe ( ) ;
198
+ }
199
+
200
+ // Note that in some cases this can end up being called after the component is destroyed.
201
+ // Add a check to ensure that we don't try to run change detection on a destroyed view.
202
+ if ( ! this . _componentDestroyed ) {
203
+ // We need to trigger change detection manually, because
204
+ // `fromEvent` doesn't seem to do it at the proper time.
205
+ // This ensures that the label is reset when the
206
+ // user clicks outside.
207
+ this . _changeDetectorRef . detectChanges ( ) ;
200
208
}
201
209
}
202
210
@@ -207,11 +215,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
207
215
get panelClosingActions ( ) : Observable < MatOptionSelectionChange > {
208
216
return merge (
209
217
this . optionSelections ,
210
- this . autocomplete . _keyManager . tabOut . pipe ( filter ( ( ) => this . _panelOpen ) ) ,
218
+ this . autocomplete . _keyManager . tabOut . pipe ( filter ( ( ) => this . _overlayAttached ) ) ,
211
219
this . _closeKeyEventStream ,
212
220
this . _outsideClickStream ,
213
221
this . _overlayRef ?
214
- this . _overlayRef . detachments ( ) . pipe ( filter ( ( ) => this . _panelOpen ) ) :
222
+ this . _overlayRef . detachments ( ) . pipe ( filter ( ( ) => this . _overlayAttached ) ) :
215
223
observableOf ( )
216
224
) ;
217
225
}
@@ -253,7 +261,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
253
261
const formField = this . _formField ?
254
262
this . _formField . _elementRef . nativeElement : null ;
255
263
256
- return this . _panelOpen &&
264
+ return this . _overlayAttached &&
257
265
clickTarget !== this . _element . nativeElement &&
258
266
( ! formField || ! formField . contains ( clickTarget ) ) &&
259
267
( ! ! this . _overlayRef && ! this . _overlayRef . overlayElement . contains ( clickTarget ) ) ;
@@ -503,7 +511,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
503
511
const wasOpen = this . panelOpen ;
504
512
505
513
this . autocomplete . _setVisibility ( ) ;
506
- this . autocomplete . _isOpen = this . _panelOpen = true ;
514
+ this . autocomplete . _isOpen = this . _overlayAttached = true ;
507
515
508
516
// We need to do an extra `panelOpen` check in here, because the
509
517
// autocomplete won't be shown if there are no options.
0 commit comments