Skip to content

Commit d52a45d

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 a30094b commit d52a45d

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
@@ -7,7 +7,7 @@
77
*/
88
import {Directionality} from '@angular/cdk/bidi';
99
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
10-
import {DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW} from '@angular/cdk/keycodes';
10+
import {DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW, hasModifierKey} from '@angular/cdk/keycodes';
1111
import {
1212
FlexibleConnectedPositionStrategy,
1313
Overlay,
@@ -406,7 +406,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn
406406
event.preventDefault();
407407
}
408408

409-
if (this.activeOption && keyCode === ENTER && this.panelOpen) {
409+
if (this.activeOption && keyCode === ENTER && this.panelOpen && !hasModifierKey(event)) {
410410
this.activeOption._selectViaInteraction();
411411
this._resetActiveItem();
412412
event.preventDefault();

src/material/autocomplete/autocomplete.spec.ts

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

991+
it('should not interfere with the ENTER key when pressing a modifier', fakeAsync(() => {
992+
const trigger = fixture.componentInstance.trigger;
993+
994+
expect(input.value).toBeFalsy('Expected input to start off blank.');
995+
expect(trigger.panelOpen).toBe(true, 'Expected panel to start off open.');
996+
997+
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
998+
flush();
999+
fixture.detectChanges();
1000+
1001+
Object.defineProperty(ENTER_EVENT, 'altKey', {get: () => true});
1002+
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
1003+
fixture.detectChanges();
1004+
1005+
expect(trigger.panelOpen).toBe(true, 'Expected panel to remain open.');
1006+
expect(input.value).toBeFalsy('Expected input to remain blank.');
1007+
expect(ENTER_EVENT.defaultPrevented)
1008+
.toBe(false, 'Expected the default ENTER action not to have been prevented.');
1009+
}));
1010+
9911011
it('should fill the text field, not select an option, when SPACE is entered', () => {
9921012
typeInElement(input, 'New');
9931013
fixture.detectChanges();

0 commit comments

Comments
 (0)