Skip to content

Commit ed2f516

Browse files
committed
fix(material/autocomplete): auto-highlighted first option not display correctly if the floating label is disabled (#14507)
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. (cherry picked from commit b7ee958)
1 parent 2e15f54 commit ed2f516

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/material-experimental/mdc-autocomplete/autocomplete.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,22 @@ describe('MDC-based MatAutocomplete', () => {
23122312
.toBe(false);
23132313
}));
23142314

2315+
it('should be able to preselect the first option when the floating label is disabled', fakeAsync(() => {
2316+
fixture.componentInstance.floatLabel = 'never';
2317+
fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true;
2318+
fixture.detectChanges();
2319+
2320+
fixture.componentInstance.trigger.openPanel();
2321+
fixture.detectChanges();
2322+
zone.simulateZoneExit();
2323+
// Note: should not have a detectChanges call here
2324+
// in order for the test to fail when it's supposed to.
2325+
2326+
expect(overlayContainerElement.querySelectorAll('mat-option')[0].classList)
2327+
.withContext('Expected first option to be highlighted.')
2328+
.toContain('mat-mdc-option-active');
2329+
}));
2330+
23152331
it('should be able to configure preselecting the first option globally', fakeAsync(() => {
23162332
fixture.destroy();
23172333
TestBed.resetTestingModule();

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ export abstract class _MatAutocompleteTriggerBase
509509
const wasOpen = this.panelOpen;
510510
this._resetActiveItem();
511511
this.autocomplete._setVisibility();
512+
this._changeDetectorRef.detectChanges();
512513

513514
if (this.panelOpen) {
514515
this._overlayRef!.updatePosition();

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,22 @@ describe('MatAutocomplete', () => {
22972297
.toBe(false);
22982298
}));
22992299

2300+
it('should be able to preselect the first option when the floating label is disabled', fakeAsync(() => {
2301+
fixture.componentInstance.floatLabel = 'never';
2302+
fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true;
2303+
fixture.detectChanges();
2304+
2305+
fixture.componentInstance.trigger.openPanel();
2306+
fixture.detectChanges();
2307+
zone.simulateZoneExit();
2308+
// Note: should not have a detectChanges call here
2309+
// in order for the test to fail when it's supposed to.
2310+
2311+
expect(overlayContainerElement.querySelectorAll('mat-option')[0].classList)
2312+
.withContext('Expected first option to be highlighted.')
2313+
.toContain('mat-active');
2314+
}));
2315+
23002316
it('should be able to configure preselecting the first option globally', fakeAsync(() => {
23012317
fixture.destroy();
23022318
TestBed.resetTestingModule();

0 commit comments

Comments
 (0)