Skip to content

Commit 4b82786

Browse files
crisbetojelbourn
authored andcommitted
fix(autocomplete): alt + up arrow to close panel not working (#15364)
Currently we support the part of the spec where the autocomplete panel is supposed to close when pressing alt + up arrow, however at some point we added more functionality that ends up reopening the panel immediately. These changes ensure that the user is able to close the panel via alt + up arrow.
1 parent a2d91e4 commit 4b82786

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
611611
if (event.keyCode === ESCAPE || (event.keyCode === UP_ARROW && event.altKey)) {
612612
this._resetActiveItem();
613613
this._closeKeyEventStream.next();
614+
615+
// We need to stop propagation, otherwise the event will eventually
616+
// reach the input itself and cause the overlay to be reopened.
617+
event.stopPropagation();
618+
event.preventDefault();
614619
}
615620
});
616621

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,7 @@ describe('MatAutocomplete', () => {
11111111
const trigger = fixture.componentInstance.trigger;
11121112
const upArrowEvent = createKeyboardEvent('keydown', UP_ARROW);
11131113
Object.defineProperty(upArrowEvent, 'altKey', {get: () => true});
1114+
spyOn(upArrowEvent, 'stopPropagation').and.callThrough();
11141115

11151116
input.focus();
11161117
flush();
@@ -1124,6 +1125,7 @@ describe('MatAutocomplete', () => {
11241125

11251126
expect(document.activeElement).toBe(input, 'Expected input to continue to be focused.');
11261127
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
1128+
expect(upArrowEvent.stopPropagation).toHaveBeenCalled();
11271129
}));
11281130

11291131
it('should close the panel when tabbing away from a trigger without results', fakeAsync(() => {

0 commit comments

Comments
 (0)