Skip to content

Commit

Permalink
fix(autocomplete): auto-highlighted first option not display correctl…
Browse files Browse the repository at this point in the history
…y if the floating label is disabled

Fixes the first option not appearing as highlighted if the consumer has set `floatLabel="never"` and `autoActiveFirstOption`. It looks like we were depending on the `transitioned` event from the label to kick off the change detection that shows the highlight.

Fixes #13734.
  • Loading branch information
crisbeto committed Jun 8, 2019
1 parent 8daaf4d commit 8630e2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
const wasOpen = this.panelOpen;
this._resetActiveItem();
this.autocomplete._setVisibility();
this._changeDetectorRef.detectChanges();

if (this.panelOpen) {
this._overlayRef!.updatePosition();
Expand Down
16 changes: 16 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,22 @@ describe('MatAutocomplete', () => {
.toBe(false, 'Expected no active descendant when closed.');
}));

it('should be able to preselect the first option when the floating label is disabled',
fakeAsync(() => {
fixture.componentInstance.floatLabel = 'never';
fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true;
fixture.detectChanges();

fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
zone.simulateZoneExit();
// Note: should not have a detectChanges call here
// in order for the test to fail when it's supposed to.

expect(overlayContainerElement.querySelectorAll('mat-option')[0].classList)
.toContain('mat-active', 'Expected first option to be highlighted.');
}));

it('should be able to configure preselecting the first option globally', fakeAsync(() => {
overlayContainer.ngOnDestroy();
fixture.destroy();
Expand Down

0 comments on commit 8630e2b

Please sign in to comment.