Skip to content

fix(material/autocomplete): always emit closed event #24642

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

Merged
merged 1 commit into from
Mar 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
19 changes: 19 additions & 0 deletions src/material-experimental/mdc-autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2825,6 +2825,25 @@ describe('MDC-based MatAutocomplete', () => {
expect(closedSpy).toHaveBeenCalledTimes(1);
}));

it('should emit a closed event if no option is displayed', fakeAsync(() => {
const {openedSpy, closedSpy} = fixture.componentInstance;
const input: HTMLInputElement = fixture.nativeElement.querySelector('input');

typeInElement(input, 'Alabama'); // Valid option
fixture.detectChanges();
tick();

expect(openedSpy).toHaveBeenCalledTimes(1);
expect(closedSpy).toHaveBeenCalledTimes(0);

typeInElement(input, '_x'); // Invalidate option to 'Alabama_x'
fixture.detectChanges();
tick();

expect(openedSpy).toHaveBeenCalledTimes(1);
expect(closedSpy).toHaveBeenCalledTimes(1);
}));

it(
'should clear the input if the user presses escape while there was a pending ' +
'auto selection and there is no previous value',
Expand Down
17 changes: 12 additions & 5 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,20 @@ export abstract class _MatAutocompleteTriggerBase

if (this.panelOpen) {
this._overlayRef!.updatePosition();
}

// If the `panelOpen` state changed, we need to make sure to emit the `opened`
// event, because we may not have emitted it when the panel was attached. This
// can happen if the users opens the panel and there are no options, but the
// options come in slightly later or as a result of the value changing.
if (wasOpen !== this.panelOpen) {
if (wasOpen !== this.panelOpen) {
// If the `panelOpen` state changed, we need to make sure to emit the `opened` or
// `closed` event, because we may not have emitted it. This can happen
// - if the users opens the panel and there are no options, but the
// options come in slightly later or as a result of the value changing,
// - if the panel is closed after the user entered a string that did not match any
// of the available options,
// - if a valid string is entered after an invalid one.
if (this.panelOpen) {
this.autocomplete.opened.emit();
} else {
this.autocomplete.closed.emit();
}
}
});
Expand Down
19 changes: 19 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2827,6 +2827,25 @@ describe('MatAutocomplete', () => {
expect(closedSpy).toHaveBeenCalledTimes(1);
}));

it('should emit a closed event if no option is displayed', fakeAsync(() => {
const {openedSpy, closedSpy} = fixture.componentInstance;
const input: HTMLInputElement = fixture.nativeElement.querySelector('input');

typeInElement(input, 'Alabama'); // Valid option
fixture.detectChanges();
tick();

expect(openedSpy).toHaveBeenCalledTimes(1);
expect(closedSpy).toHaveBeenCalledTimes(0);

typeInElement(input, '_x'); // Invalidate option to 'Alabama_x'
fixture.detectChanges();
tick();

expect(openedSpy).toHaveBeenCalledTimes(1);
expect(closedSpy).toHaveBeenCalledTimes(1);
}));

it(
'should clear the input if the user presses escape while there was a pending ' +
'auto selection and there is no previous value',
Expand Down