Skip to content

Commit fbdb00e

Browse files
committed
fix(autocomplete): don't handle enter events with modifier keys
Doesn't handle `ENTER` key presses and doesn't prevent their default action, if they have a modifier, in order to avoid intefering with any OS-level shortcuts.
1 parent dc859fe commit fbdb00e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/material/autocomplete/autocomplete-trigger.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {Directionality} from '@angular/cdk/bidi';
9-
import {DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW} from '@angular/cdk/keycodes';
9+
import {DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW, hasModifierKey} from '@angular/cdk/keycodes';
1010
import {
1111
FlexibleConnectedPositionStrategy,
1212
Overlay,
@@ -387,7 +387,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
387387
event.preventDefault();
388388
}
389389

390-
if (this.activeOption && keyCode === ENTER && this.panelOpen) {
390+
if (this.activeOption && keyCode === ENTER && this.panelOpen && !hasModifierKey(event)) {
391391
this.activeOption._selectViaInteraction();
392392
this._resetActiveItem();
393393
event.preventDefault();

src/material/autocomplete/autocomplete.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,26 @@ describe('MatAutocomplete', () => {
938938
expect(ENTER_EVENT.defaultPrevented).toBe(false, 'Default action should not be prevented.');
939939
});
940940

941+
it('should not interfere with the ENTER key when pressing a modifier', fakeAsync(() => {
942+
const trigger = fixture.componentInstance.trigger;
943+
944+
expect(input.value).toBeFalsy('Expected input to start off blank.');
945+
expect(trigger.panelOpen).toBe(true, 'Expected panel to start off open.');
946+
947+
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
948+
flush();
949+
fixture.detectChanges();
950+
951+
Object.defineProperty(ENTER_EVENT, 'altKey', {get: () => true});
952+
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
953+
fixture.detectChanges();
954+
955+
expect(trigger.panelOpen).toBe(true, 'Expected panel to remain open.');
956+
expect(input.value).toBeFalsy('Expected input to remain blank.');
957+
expect(ENTER_EVENT.defaultPrevented)
958+
.toBe(false, 'Expected the default ENTER action not to have been prevented.');
959+
}));
960+
941961
it('should fill the text field, not select an option, when SPACE is entered', () => {
942962
typeInElement('New', input);
943963
fixture.detectChanges();

0 commit comments

Comments
 (0)