Skip to content

Commit 3a9fdba

Browse files
committed
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.
1 parent 3712b8f commit 3a9fdba

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export function getMatAutocompleteMissingPanelError(): Error {
115115
'(blur)': '_onTouched()',
116116
'(input)': '_handleInput($event)',
117117
'(keydown)': '_handleKeydown($event)',
118+
'(click)': '_handleClick()',
118119
},
119120
exportAs: 'matAutocompleteTrigger',
120121
providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR]
@@ -445,6 +446,12 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
445446
}
446447
}
447448

449+
_handleClick() {
450+
if (!this.panelOpen) {
451+
this.openPanel();
452+
}
453+
}
454+
448455
/**
449456
* In "auto" mode, the label will animate down as soon as focus is lost.
450457
* This causes the value to jump when selecting an option with the mouse.

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,29 @@ describe('MatAutocomplete', () => {
491491
expect(input.getAttribute('aria-haspopup')).toBe('false');
492492
});
493493

494+
it('should close the panel when pressing escape', fakeAsync(() => {
495+
const trigger = fixture.componentInstance.trigger;
496+
497+
input.focus();
498+
flush();
499+
fixture.detectChanges();
500+
501+
expect(document.activeElement).toBe(input, 'Expected input to be focused.');
502+
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
503+
504+
trigger.closePanel();
505+
fixture.detectChanges();
506+
507+
expect(document.activeElement).toBe(input, 'Expected input to continue to be focused.');
508+
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
509+
510+
input.click();
511+
flush();
512+
fixture.detectChanges();
513+
514+
expect(trigger.panelOpen).toBe(true, 'Expected panel to reopen on click.');
515+
}));
516+
494517
});
495518

496519
it('should have the correct text direction in RTL', () => {

tools/public_api_guard/material/autocomplete.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export declare class MatAutocompleteTrigger implements ControlValueAccessor, OnC
8282
readonly panelOpen: boolean;
8383
position: 'auto' | 'above' | 'below';
8484
constructor(_element: ElementRef<HTMLInputElement>, _overlay: Overlay, _viewContainerRef: ViewContainerRef, _zone: NgZone, _changeDetectorRef: ChangeDetectorRef, scrollStrategy: any, _dir: Directionality, _formField: MatFormField, _document: any, _viewportRuler?: ViewportRuler | undefined);
85+
_handleClick(): void;
8586
_handleFocus(): void;
8687
_handleInput(event: KeyboardEvent): void;
8788
_handleKeydown(event: KeyboardEvent): void;

0 commit comments

Comments
 (0)