Skip to content

fix(autocomplete): closed event emitting when panel wasn't shown due to lack of options #10176

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 6, 2018
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
50 changes: 29 additions & 21 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
}

/** Whether or not the autocomplete panel is open. */
get panelOpen(): boolean { return this._panelOpen && this.autocomplete.showPanel; }
private _panelOpen: boolean = false;
get panelOpen(): boolean {
return this._overlayAttached && this.autocomplete.showPanel;
}
private _overlayAttached: boolean = false;

/** Opens the autocomplete suggestion panel. */
openPanel(): void {
Expand All @@ -179,24 +181,30 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
closePanel(): void {
this._resetLabel();

if (this._panelOpen) {
this.autocomplete._isOpen = this._panelOpen = false;
if (!this._overlayAttached) {
return;
}

if (this.panelOpen) {
// Only emit if the panel was visible.
this.autocomplete.closed.emit();
}

if (this._overlayRef && this._overlayRef.hasAttached()) {
this._overlayRef.detach();
this._closingActionsSubscription.unsubscribe();
}
this.autocomplete._isOpen = this._overlayAttached = false;

// Note that in some cases this can end up being called after the component is destroyed.
// Add a check to ensure that we don't try to run change detection on a destroyed view.
if (!this._componentDestroyed) {
// We need to trigger change detection manually, because
// `fromEvent` doesn't seem to do it at the proper time.
// This ensures that the label is reset when the
// user clicks outside.
this._changeDetectorRef.detectChanges();
}
if (this._overlayRef && this._overlayRef.hasAttached()) {
this._overlayRef.detach();
this._closingActionsSubscription.unsubscribe();
}

// Note that in some cases this can end up being called after the component is destroyed.
// Add a check to ensure that we don't try to run change detection on a destroyed view.
if (!this._componentDestroyed) {
// We need to trigger change detection manually, because
// `fromEvent` doesn't seem to do it at the proper time.
// This ensures that the label is reset when the
// user clicks outside.
this._changeDetectorRef.detectChanges();
}
}

Expand All @@ -207,11 +215,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
get panelClosingActions(): Observable<MatOptionSelectionChange> {
return merge(
this.optionSelections,
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._panelOpen)),
this.autocomplete._keyManager.tabOut.pipe(filter(() => this._overlayAttached)),
this._closeKeyEventStream,
this._outsideClickStream,
this._overlayRef ?
this._overlayRef.detachments().pipe(filter(() => this._panelOpen)) :
this._overlayRef.detachments().pipe(filter(() => this._overlayAttached)) :
observableOf()
);
}
Expand Down Expand Up @@ -253,7 +261,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
const formField = this._formField ?
this._formField._elementRef.nativeElement : null;

return this._panelOpen &&
return this._overlayAttached &&
clickTarget !== this._element.nativeElement &&
(!formField || !formField.contains(clickTarget)) &&
(!!this._overlayRef && !this._overlayRef.overlayElement.contains(clickTarget));
Expand Down Expand Up @@ -503,7 +511,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
const wasOpen = this.panelOpen;

this.autocomplete._setVisibility();
this.autocomplete._isOpen = this._panelOpen = true;
this.autocomplete._isOpen = this._overlayAttached = true;

// We need to do an extra `panelOpen` check in here, because the
// autocomplete won't be shown if there are no options.
Expand Down
23 changes: 23 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ describe('MatAutocomplete', () => {
expect(fixture.componentInstance.openedSpy).toHaveBeenCalled();
});

it('should not emit the `opened` event when no options are being shown', () => {
fixture.componentInstance.filteredStates = fixture.componentInstance.states = [];
fixture.detectChanges();

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

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

it('should not emit the opened event multiple times while typing', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
Expand All @@ -442,6 +452,19 @@ describe('MatAutocomplete', () => {
expect(fixture.componentInstance.closedSpy).toHaveBeenCalled();
});

it('should not emit the `closed` event when no options were shown', () => {
fixture.componentInstance.filteredStates = fixture.componentInstance.states = [];
fixture.detectChanges();

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

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

expect(fixture.componentInstance.closedSpy).not.toHaveBeenCalled();
});

});

it('should have the correct text direction in RTL', () => {
Expand Down