Skip to content

Commit

Permalink
fix(material/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 angular#15177.
  • Loading branch information
crisbeto committed Feb 25, 2022
1 parent 8a12da7 commit 78561ab
Show file tree
Hide file tree
Showing 4 changed files with 58 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

0 comments on commit 78561ab

Please sign in to comment.