File tree 4 files changed +48
-2
lines changed
material-experimental/mdc-autocomplete
4 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -2801,6 +2801,28 @@ expect(scrollContainer.scrollTop)
2801
2801
expect ( spy . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( { source : autocomplete , option : options [ 2 ] } ) ;
2802
2802
} ) ) ;
2803
2803
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
+
2804
2826
it ( 'should be able to set a custom panel connection element' , ( ) => {
2805
2827
const fixture = createComponent ( AutocompleteWithDifferentOrigin ) ;
2806
2828
Original file line number Diff line number Diff line change @@ -574,8 +574,8 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
574
574
// See: https://www.w3.org/TR/wai-aria-practices-1.1/#textbox-keyboard-interaction
575
575
if ( ( event . keyCode === ESCAPE && ! hasModifierKey ( event ) ) ||
576
576
( event . keyCode === UP_ARROW && hasModifierKey ( event , 'altKey' ) ) ) {
577
- this . _resetActiveItem ( ) ;
578
577
this . _closeKeyEventStream . next ( ) ;
578
+ this . _resetActiveItem ( ) ;
579
579
580
580
// We need to stop propagation, otherwise the event will eventually
581
581
// 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)
2798
2798
expect ( spy . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( { source : autocomplete , option : options [ 2 ] } ) ;
2799
2799
} ) ) ;
2800
2800
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
+
2801
2823
it ( 'should be able to set a custom panel connection element' , ( ) => {
2802
2824
const fixture = createComponent ( AutocompleteWithDifferentOrigin ) ;
2803
2825
Original file line number Diff line number Diff line change @@ -215,7 +215,9 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
215
215
ngAfterContentInit ( ) {
216
216
this . _keyManager = new ActiveDescendantKeyManager < _MatOptionBase > ( this . options ) . withWrap ( ) ;
217
217
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
+ }
219
221
} ) ;
220
222
221
223
// Set the initial visibility state.
You can’t perform that action at this time.
0 commit comments