Skip to content

Commit

Permalink
fix(autocomplete): alt + up arrow to close panel not working (angular…
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
crisbeto authored and jelbourn committed Jun 19, 2019
1 parent a2d91e4 commit 4b82786
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
if (event.keyCode === ESCAPE || (event.keyCode === UP_ARROW && event.altKey)) {
this._resetActiveItem();
this._closeKeyEventStream.next();

// We need to stop propagation, otherwise the event will eventually
// reach the input itself and cause the overlay to be reopened.
event.stopPropagation();
event.preventDefault();
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ describe('MatAutocomplete', () => {
const trigger = fixture.componentInstance.trigger;
const upArrowEvent = createKeyboardEvent('keydown', UP_ARROW);
Object.defineProperty(upArrowEvent, 'altKey', {get: () => true});
spyOn(upArrowEvent, 'stopPropagation').and.callThrough();

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

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

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

0 comments on commit 4b82786

Please sign in to comment.