Skip to content

fix(autocomplete): not emitting opened event if options come in after open #15582

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
May 13, 2019
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
44 changes: 26 additions & 18 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,24 +510,32 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,

// When the zone is stable initially, and when the option list changes...
return merge(firstStable, optionChanges)
.pipe(
// create a new stream of panelClosingActions, replacing any previous streams
// that were created, and flatten it so our stream only emits closing events...
switchMap(() => {
this._resetActiveItem();
this.autocomplete._setVisibility();

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

return this.panelClosingActions;
}),
// when the first closing event occurs...
take(1)
)
// set the value, close the panel, and complete.
.subscribe(event => this._setValueAndClose(event));
.pipe(
// create a new stream of panelClosingActions, replacing any previous streams
// that were created, and flatten it so our stream only emits closing events...
switchMap(() => {
const wasOpen = this.panelOpen;
this._resetActiveItem();
this.autocomplete._setVisibility();

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) {
this.autocomplete.opened.emit();
}
}

return this.panelClosingActions;
}),
// when the first closing event occurs...
take(1))
// set the value, close the panel, and complete.
.subscribe(event => this._setValueAndClose(event));
}

/** Destroys the autocomplete suggestion panel. */
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 @@ -421,6 +421,25 @@ describe('MatAutocomplete', () => {
expect(fixture.componentInstance.openedSpy).not.toHaveBeenCalled();
});

it('should emit the `opened` event if the options come in after the panel is shown',
fakeAsync(() => {
fixture.componentInstance.filteredStates = fixture.componentInstance.states = [];
fixture.detectChanges();

fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

expect(fixture.componentInstance.openedSpy).not.toHaveBeenCalled();

fixture.componentInstance.filteredStates = fixture.componentInstance.states =
[{name: 'California', code: 'CA'}];
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(fixture.componentInstance.openedSpy).toHaveBeenCalled();
}));

it('should not emit the opened event multiple times while typing', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
Expand Down