Skip to content

Commit 3fec9e0

Browse files
committed
fix(autocomplete): alt + up arrow to close panel not working
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 ae41a0a commit 3fec9e0

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
585585
if (event.keyCode === ESCAPE || (event.keyCode === UP_ARROW && event.altKey)) {
586586
this._resetActiveItem();
587587
this._closeKeyEventStream.next();
588+
589+
// We need to stop propagation, otherwise the event will eventually
590+
// reach the input itself and cause the overlay to be reopened.
591+
event.stopPropagation();
592+
event.preventDefault();
588593
}
589594
});
590595

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ describe('MatAutocomplete', () => {
10921092
const trigger = fixture.componentInstance.trigger;
10931093
const upArrowEvent = createKeyboardEvent('keydown', UP_ARROW);
10941094
Object.defineProperty(upArrowEvent, 'altKey', {get: () => true});
1095+
spyOn(upArrowEvent, 'stopPropagation').and.callThrough();
10951096

10961097
input.focus();
10971098
flush();
@@ -1105,6 +1106,7 @@ describe('MatAutocomplete', () => {
11051106

11061107
expect(document.activeElement).toBe(input, 'Expected input to continue to be focused.');
11071108
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
1109+
expect(upArrowEvent.stopPropagation).toHaveBeenCalled();
11081110
}));
11091111

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

0 commit comments

Comments
 (0)