Skip to content

Commit

Permalink
fix(material/autocomplete): reopen panel on input click (#16020)
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.

(cherry picked from commit 16dea18)
  • Loading branch information
crisbeto committed Feb 25, 2022
1 parent f71ca08 commit f31fd3f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const MAT_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
'(blur)': '_onTouched()',
'(input)': '_handleInput($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick()',
},
exportAs: 'matAutocompleteTrigger',
providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR],
Expand Down
25 changes: 25 additions & 0 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,31 @@ describe('MDC-based MatAutocomplete', () => {

expect(input.hasAttribute('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).withContext('Expected input to be focused.').toBe(input);
expect(trigger.panelOpen).withContext('Expected panel to be open.').toBe(true);

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

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

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

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

it('should not close the panel when clicking on the input', fakeAsync(() => {
Expand Down
7 changes: 7 additions & 0 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ export abstract class _MatAutocompleteTriggerBase
}
}

_handleClick(): void {
if (this._canOpen() && !this.panelOpen) {
this.openPanel();
}
}

/**
* In "auto" mode, the label will animate down as soon as focus is lost.
* This causes the value to jump when selecting an option with the mouse.
Expand Down Expand Up @@ -800,6 +806,7 @@ export abstract class _MatAutocompleteTriggerBase
'(blur)': '_onTouched()',
'(input)': '_handleInput($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick()',
},
exportAs: 'matAutocompleteTrigger',
providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR],
Expand Down
25 changes: 25 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,31 @@ describe('MatAutocomplete', () => {

expect(input.hasAttribute('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).withContext('Expected input to be focused.').toBe(input);
expect(trigger.panelOpen).withContext('Expected panel to be open.').toBe(true);

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

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

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

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

it('should not close the panel when clicking on the input', fakeAsync(() => {
Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/material/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
closePanel(): void;
connectedTo: _MatAutocompleteOriginBase;
// (undocumented)
_handleClick(): void;
// (undocumented)
_handleFocus(): void;
// (undocumented)
_handleInput(event: KeyboardEvent): void;
Expand Down

0 comments on commit f31fd3f

Please sign in to comment.