@@ -104,6 +104,7 @@ export abstract class _MatAutocompleteTriggerBase
104
104
private _portal : TemplatePortal ;
105
105
private _componentDestroyed = false ;
106
106
private _autocompleteDisabled = false ;
107
+ private _hasFocus : boolean ;
107
108
private _scrollStrategy : ( ) => ScrollStrategy ;
108
109
109
110
/** Old value of the native input. Used to work around issues with the `input` event on IE. */
@@ -465,12 +466,25 @@ export abstract class _MatAutocompleteTriggerBase
465
466
}
466
467
467
468
_handleFocus ( ) : void {
468
- if ( ! this . _canOpenOnNextFocus ) {
469
- this . _canOpenOnNextFocus = true ;
470
- } else if ( this . _canOpen ( ) ) {
471
- this . _previousValue = this . _element . nativeElement . value ;
472
- this . _attachOverlay ( ) ;
473
- this . _floatLabel ( true ) ;
469
+ // Normally the event won't fire again if the input already
470
+ // has focus, but it may receive a fake event during tests.
471
+ if ( ! this . _hasFocus ) {
472
+ this . _hasFocus = true ;
473
+
474
+ if ( ! this . _canOpenOnNextFocus ) {
475
+ this . _canOpenOnNextFocus = true ;
476
+ } else if ( this . _canOpen ( ) ) {
477
+ this . _previousValue = this . _element . nativeElement . value ;
478
+ this . _attachOverlay ( ) ;
479
+ this . _floatLabel ( true ) ;
480
+ }
481
+ }
482
+ }
483
+
484
+ _handleBlur ( ) : void {
485
+ if ( this . _hasFocus ) {
486
+ this . _hasFocus = false ;
487
+ this . _onTouched ( ) ;
474
488
}
475
489
}
476
490
@@ -836,7 +850,7 @@ export abstract class _MatAutocompleteTriggerBase
836
850
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
837
851
// a little earlier. This avoids issues where IE delays the focusing of the input.
838
852
'(focusin)' : '_handleFocus()' ,
839
- '(blur)' : '_onTouched ()' ,
853
+ '(blur)' : '_handleBlur ()' ,
840
854
'(input)' : '_handleInput($event)' ,
841
855
'(keydown)' : '_handleKeydown($event)' ,
842
856
'(click)' : '_handleClick()' ,
0 commit comments