Skip to content

Commit 769e204

Browse files
authored
fix(material/select): active class not removed from reset option when new value is assigned through form control (#26414)
Fixes that we weren't removing the active class from a reset option when a new value is assigned through a `FormControl`. The problem was that we were removing the active classes only from selected options, but a reset option is never selected. Fixes #26390.
1 parent 85dad35 commit 769e204

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/material/legacy-select/select.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3361,12 +3361,14 @@ describe('MatSelect', () => {
33613361
describe('with reset option and a form control', () => {
33623362
let fixture: ComponentFixture<SelectWithResetOptionAndFormControl>;
33633363
let options: HTMLElement[];
3364+
let trigger: HTMLElement;
33643365

33653366
beforeEach(fakeAsync(() => {
33663367
configureMatSelectTestingModule([SelectWithResetOptionAndFormControl]);
33673368
fixture = TestBed.createComponent(SelectWithResetOptionAndFormControl);
33683369
fixture.detectChanges();
3369-
fixture.debugElement.query(By.css('.mat-select-trigger'))!.nativeElement.click();
3370+
trigger = fixture.debugElement.query(By.css('.mat-select-trigger'))!.nativeElement;
3371+
trigger.click();
33703372
fixture.detectChanges();
33713373
options = Array.from(overlayContainerElement.querySelectorAll('mat-option'));
33723374
}));
@@ -3394,6 +3396,22 @@ describe('MatSelect', () => {
33943396
expect(fixture.componentInstance.select.value).toBe('a');
33953397
expect(fixture.componentInstance.control.value).toBe('a');
33963398
}));
3399+
3400+
it('should deselect the reset option when a value is assigned through the form control', fakeAsync(() => {
3401+
expect(options[0].classList).toContain('mat-active');
3402+
3403+
options[0].click();
3404+
fixture.detectChanges();
3405+
flush();
3406+
3407+
fixture.componentInstance.control.setValue('c');
3408+
fixture.detectChanges();
3409+
trigger.click();
3410+
fixture.detectChanges();
3411+
3412+
expect(options[0].classList).not.toContain('mat-active');
3413+
expect(options[3].classList).toContain('mat-active');
3414+
}));
33973415
});
33983416

33993417
describe('without Angular forms', () => {

src/material/select/select.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3406,12 +3406,14 @@ describe('MDC-based MatSelect', () => {
34063406
describe('with reset option and a form control', () => {
34073407
let fixture: ComponentFixture<SelectWithResetOptionAndFormControl>;
34083408
let options: HTMLElement[];
3409+
let trigger: HTMLElement;
34093410

34103411
beforeEach(fakeAsync(() => {
34113412
configureMatSelectTestingModule([SelectWithResetOptionAndFormControl]);
34123413
fixture = TestBed.createComponent(SelectWithResetOptionAndFormControl);
34133414
fixture.detectChanges();
3414-
fixture.debugElement.query(By.css('.mat-mdc-select-trigger'))!.nativeElement.click();
3415+
trigger = fixture.debugElement.query(By.css('.mat-mdc-select-trigger'))!.nativeElement;
3416+
trigger.click();
34153417
fixture.detectChanges();
34163418
options = Array.from(overlayContainerElement.querySelectorAll('mat-option'));
34173419
}));
@@ -3439,6 +3441,22 @@ describe('MDC-based MatSelect', () => {
34393441
expect(fixture.componentInstance.select.value).toBe('a');
34403442
expect(fixture.componentInstance.control.value).toBe('a');
34413443
}));
3444+
3445+
it('should deselect the reset option when a value is assigned through the form control', fakeAsync(() => {
3446+
expect(options[0].classList).toContain('mat-mdc-option-active');
3447+
3448+
options[0].click();
3449+
fixture.detectChanges();
3450+
flush();
3451+
3452+
fixture.componentInstance.control.setValue('c');
3453+
fixture.detectChanges();
3454+
trigger.click();
3455+
fixture.detectChanges();
3456+
3457+
expect(options[0].classList).not.toContain('mat-mdc-option-active');
3458+
expect(options[3].classList).toContain('mat-mdc-option-active');
3459+
}));
34423460
});
34433461

34443462
describe('without Angular forms', () => {

src/material/select/select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ export abstract class _MatSelectBase<C>
837837
* found with the designated value, the select trigger is cleared.
838838
*/
839839
private _setSelectionByValue(value: any | any[]): void {
840-
this._selectionModel.selected.forEach(option => option.setInactiveStyles());
840+
this.options.forEach(option => option.setInactiveStyles());
841841
this._selectionModel.clear();
842842

843843
if (this.multiple && value) {

0 commit comments

Comments
 (0)