@@ -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. */
@@ -434,12 +435,25 @@ export abstract class _MatAutocompleteTriggerBase
434
435
}
435
436
436
437
_handleFocus ( ) : void {
437
- if ( ! this . _canOpenOnNextFocus ) {
438
- this . _canOpenOnNextFocus = true ;
439
- } else if ( this . _canOpen ( ) ) {
440
- this . _previousValue = this . _element . nativeElement . value ;
441
- this . _attachOverlay ( ) ;
442
- this . _floatLabel ( true ) ;
438
+ // Normally the event won't fire again if the input already
439
+ // has focus, but it may receive a fake event during tests.
440
+ if ( ! this . _hasFocus ) {
441
+ this . _hasFocus = true ;
442
+
443
+ if ( ! this . _canOpenOnNextFocus ) {
444
+ this . _canOpenOnNextFocus = true ;
445
+ } else if ( this . _canOpen ( ) ) {
446
+ this . _previousValue = this . _element . nativeElement . value ;
447
+ this . _attachOverlay ( ) ;
448
+ this . _floatLabel ( true ) ;
449
+ }
450
+ }
451
+ }
452
+
453
+ _handleBlur ( ) : void {
454
+ if ( this . _hasFocus ) {
455
+ this . _hasFocus = false ;
456
+ this . _onTouched ( ) ;
443
457
}
444
458
}
445
459
@@ -786,7 +800,7 @@ export abstract class _MatAutocompleteTriggerBase
786
800
// Note: we use `focusin`, as opposed to `focus`, in order to open the panel
787
801
// a little earlier. This avoids issues where IE delays the focusing of the input.
788
802
'(focusin)' : '_handleFocus()' ,
789
- '(blur)' : '_onTouched ()' ,
803
+ '(blur)' : '_handleBlur ()' ,
790
804
'(input)' : '_handleInput($event)' ,
791
805
'(keydown)' : '_handleKeydown($event)' ,
792
806
} ,
0 commit comments