Skip to content

Commit

Permalink
fix(autocomplete): reopen panel on input click
Browse files Browse the repository at this point in the history
Currently if the user clicks an autocomplete to open it, selects an option and then clicks again, the panel won't open, because we use `focus` and the input was focused already. These changes add an extra `click` listener so the panel can reopen.

Fixes #15177.
  • Loading branch information
crisbeto committed Sep 9, 2019
1 parent 492fdcc commit 7f79de5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
'(blur)': '_onTouched()',
'(input)': '_handleInput($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': 'openPanel()',
},
exportAs: 'matAutocompleteTrigger',
providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR]
Expand Down Expand Up @@ -270,8 +271,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn

/** Opens the autocomplete suggestion panel. */
openPanel(): void {
this._attachOverlay();
this._floatLabel();
if (!this.panelOpen) {
this._attachOverlay();
this._floatLabel();
}
}

/** Closes the autocomplete suggestion panel. */
Expand Down
23 changes: 23 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,29 @@ describe('MatAutocomplete', () => {
expect(input.getAttribute('aria-haspopup')).toBe('false');
});

it('should close the panel when pressing escape', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;

input.focus();
flush();
fixture.detectChanges();

expect(document.activeElement).toBe(input, 'Expected input to be focused.');
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');

trigger.closePanel();
fixture.detectChanges();

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

input.click();
flush();
fixture.detectChanges();

expect(trigger.panelOpen).toBe(true, 'Expected panel to reopen on click.');
}));

});

it('should not close the panel when clicking on the input', fakeAsync(() => {
Expand Down

0 comments on commit 7f79de5

Please sign in to comment.