Skip to content

fix(autocomplete): panel not resetting properly in certain scenarios #5911

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
Jul 25, 2017
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: 9 additions & 10 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,22 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {

/** Closes the autocomplete suggestion panel. */
closePanel(): void {
if (!this.panelOpen) {
return;
}

if (this._overlayRef && this._overlayRef.hasAttached()) {
this._overlayRef.detach();
this._closingActionsSubscription.unsubscribe();
}

this._panelOpen = false;
this._resetPlaceholder();

// We need to trigger change detection manually, because
// `fromEvent` doesn't seem to do it at the proper time.
// This ensures that the placeholder is reset when the
// user clicks outside.
this._changeDetectorRef.detectChanges();
if (this._panelOpen) {
this._panelOpen = false;

// We need to trigger change detection manually, because
// `fromEvent` doesn't seem to do it at the proper time.
// This ensures that the placeholder is reset when the
// user clicks outside.
this._changeDetectorRef.detectChanges();
}
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,37 @@ describe('MdAutocomplete', () => {
});
}));

it('should toggle the visibility when typing and closing the panel', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
tick();
fixture.detectChanges();

expect(overlayContainerElement.querySelector('.mat-autocomplete-panel')!.classList)
.toContain('mat-autocomplete-visible', 'Expected panel to be visible.');

typeInElement('x', input);
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(overlayContainerElement.querySelector('.mat-autocomplete-panel')!.classList)
.toContain('mat-autocomplete-hidden', 'Expected panel to be hidden.');

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

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

typeInElement('al', input);
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(overlayContainerElement.querySelector('.mat-autocomplete-panel')!.classList)
.toContain('mat-autocomplete-visible', 'Expected panel to be visible.');
}));

});

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