Skip to content

Commit c99c512

Browse files
crisbetommalerba
authored andcommitted
fix(autocomplete): auto-highlighted first option not display correctly if the floating label is disabled (#13774)
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.
1 parent 904a5ea commit c99c512

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

+1
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
493493
switchMap(() => {
494494
this._resetActiveItem();
495495
this.autocomplete._setVisibility();
496+
this._changeDetectorRef.detectChanges();
496497

497498
if (this.panelOpen) {
498499
this._overlayRef!.updatePosition();

src/lib/autocomplete/autocomplete.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,22 @@ describe('MatAutocomplete', () => {
16441644
.toContain('mat-active', 'Expected first option to be highlighted.');
16451645
}));
16461646

1647+
it('should be able to preselect the first option when the floating label is disabled',
1648+
fakeAsync(() => {
1649+
fixture.componentInstance.floatLabel = 'never';
1650+
fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true;
1651+
fixture.detectChanges();
1652+
1653+
fixture.componentInstance.trigger.openPanel();
1654+
fixture.detectChanges();
1655+
zone.simulateZoneExit();
1656+
// Note: should not have a detectChanges call here
1657+
// in order for the test to fail when it's supposed to.
1658+
1659+
expect(overlayContainerElement.querySelectorAll('mat-option')[0].classList)
1660+
.toContain('mat-active', 'Expected first option to be highlighted.');
1661+
}));
1662+
16471663
it('should be able to configure preselecting the first option globally', fakeAsync(() => {
16481664
overlayContainer.ngOnDestroy();
16491665
fixture.destroy();

0 commit comments

Comments
 (0)