Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/autocomplete): reopen panel on input click #16020

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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