From 7f79de5bf0e647a88658ce6e84d60cc8f83a49a3 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Mon, 9 Sep 2019 18:40:43 +0200 Subject: [PATCH] fix(autocomplete): reopen panel on input click 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. --- .../autocomplete/autocomplete-trigger.ts | 7 ++++-- .../autocomplete/autocomplete.spec.ts | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/material/autocomplete/autocomplete-trigger.ts b/src/material/autocomplete/autocomplete-trigger.ts index 2ab382278601..4e21b9f67c76 100644 --- a/src/material/autocomplete/autocomplete-trigger.ts +++ b/src/material/autocomplete/autocomplete-trigger.ts @@ -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] @@ -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. */ diff --git a/src/material/autocomplete/autocomplete.spec.ts b/src/material/autocomplete/autocomplete.spec.ts index 347cc48196b3..03318ba23a7b 100644 --- a/src/material/autocomplete/autocomplete.spec.ts +++ b/src/material/autocomplete/autocomplete.spec.ts @@ -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(() => {