File tree Expand file tree Collapse file tree 4 files changed +48
-2
lines changed
material-experimental/mdc-autocomplete Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -2801,6 +2801,28 @@ expect(scrollContainer.scrollTop)
28012801 expect ( spy . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( { source : autocomplete , option : options [ 2 ] } ) ;
28022802 } ) ) ;
28032803
2804+ it ( 'should not emit the optionActivated event when the active option is reset' , fakeAsync ( ( ) => {
2805+ const fixture = createComponent ( AutocompleteWithActivatedEvent ) ;
2806+
2807+ fixture . detectChanges ( ) ;
2808+ fixture . componentInstance . trigger . openPanel ( ) ;
2809+ zone . simulateZoneExit ( ) ;
2810+ fixture . detectChanges ( ) ;
2811+
2812+ const input = fixture . nativeElement . querySelector ( 'input' ) ;
2813+ const spy = fixture . componentInstance . optionActivated ;
2814+
2815+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
2816+
2817+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2818+ fixture . detectChanges ( ) ;
2819+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
2820+
2821+ dispatchKeyboardEvent ( input , 'keydown' , ESCAPE ) ;
2822+ fixture . detectChanges ( ) ;
2823+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
2824+ } ) ) ;
2825+
28042826 it ( 'should be able to set a custom panel connection element' , ( ) => {
28052827 const fixture = createComponent ( AutocompleteWithDifferentOrigin ) ;
28062828
Original file line number Diff line number Diff line change @@ -574,8 +574,8 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
574574 // See: https://www.w3.org/TR/wai-aria-practices-1.1/#textbox-keyboard-interaction
575575 if ( ( event . keyCode === ESCAPE && ! hasModifierKey ( event ) ) ||
576576 ( event . keyCode === UP_ARROW && hasModifierKey ( event , 'altKey' ) ) ) {
577- this . _resetActiveItem ( ) ;
578577 this . _closeKeyEventStream . next ( ) ;
578+ this . _resetActiveItem ( ) ;
579579
580580 // We need to stop propagation, otherwise the event will eventually
581581 // reach the input itself and cause the overlay to be reopened.
Original file line number Diff line number Diff line change @@ -2798,6 +2798,28 @@ expect(container.scrollTop)
27982798 expect ( spy . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( { source : autocomplete , option : options [ 2 ] } ) ;
27992799 } ) ) ;
28002800
2801+ it ( 'should not emit the optionActivated event when the active option is reset' , fakeAsync ( ( ) => {
2802+ const fixture = createComponent ( AutocompleteWithActivatedEvent ) ;
2803+
2804+ fixture . detectChanges ( ) ;
2805+ fixture . componentInstance . trigger . openPanel ( ) ;
2806+ zone . simulateZoneExit ( ) ;
2807+ fixture . detectChanges ( ) ;
2808+
2809+ const input = fixture . nativeElement . querySelector ( 'input' ) ;
2810+ const spy = fixture . componentInstance . optionActivated ;
2811+
2812+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
2813+
2814+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2815+ fixture . detectChanges ( ) ;
2816+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
2817+
2818+ dispatchKeyboardEvent ( input , 'keydown' , ESCAPE ) ;
2819+ fixture . detectChanges ( ) ;
2820+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
2821+ } ) ) ;
2822+
28012823 it ( 'should be able to set a custom panel connection element' , ( ) => {
28022824 const fixture = createComponent ( AutocompleteWithDifferentOrigin ) ;
28032825
Original file line number Diff line number Diff line change @@ -215,7 +215,9 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
215215 ngAfterContentInit ( ) {
216216 this . _keyManager = new ActiveDescendantKeyManager < _MatOptionBase > ( this . options ) . withWrap ( ) ;
217217 this . _activeOptionChanges = this . _keyManager . change . subscribe ( index => {
218- this . optionActivated . emit ( { source : this , option : this . options . toArray ( ) [ index ] || null } ) ;
218+ if ( this . isOpen ) {
219+ this . optionActivated . emit ( { source : this , option : this . options . toArray ( ) [ index ] || null } ) ;
220+ }
219221 } ) ;
220222
221223 // Set the initial visibility state.
You can’t perform that action at this time.
0 commit comments