Skip to content

Commit f4bec43

Browse files
crisbetojelbourn
authored andcommitted
fix(chips): chip list disabled state out of sync when swapping out form group with a disabled one (#17993)
Related to #17872. Ensures that the chip list's disabled state is in sync with its form control, if the control is swapped out with a disabled one. (cherry picked from commit c67337b)
1 parent d25fe8b commit f4bec43

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/material/chips/chip-list.spec.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ import {
3232
ChangeDetectionStrategy,
3333
} from '@angular/core';
3434
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
35-
import {FormControl, FormsModule, NgForm, ReactiveFormsModule, Validators} from '@angular/forms';
35+
import {
36+
FormControl,
37+
FormsModule,
38+
NgForm,
39+
ReactiveFormsModule,
40+
Validators,
41+
FormGroup,
42+
FormBuilder,
43+
} from '@angular/forms';
3644
import {MatFormFieldModule} from '@angular/material/form-field';
3745
import {By} from '@angular/platform-browser';
3846
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
@@ -935,6 +943,23 @@ describe('MatChipList', () => {
935943
.toBeFalsy(`Expected chip with the old value not to be selected.`);
936944
});
937945
});
946+
947+
it('should keep the disabled state in sync if the form group is swapped and ' +
948+
'disabled at the same time', fakeAsync(() => {
949+
fixture = createComponent(ChipListInsideDynamicFormGroup);
950+
fixture.detectChanges();
951+
const instance = fixture.componentInstance;
952+
const list: MatChipList = instance.chipList;
953+
954+
expect(list.disabled).toBe(false);
955+
expect(list.chips.toArray().every(chip => chip.disabled)).toBe(false);
956+
957+
instance.assignGroup(true);
958+
fixture.detectChanges();
959+
960+
expect(list.disabled).toBe(true);
961+
expect(list.chips.toArray().every(chip => chip.disabled)).toBe(true);
962+
}));
938963
});
939964

940965
describe('chip list with chip input', () => {
@@ -1642,3 +1667,31 @@ class ChipListWithRemove {
16421667
class PreselectedChipInsideOnPush {
16431668
control = new FormControl('Pizza');
16441669
}
1670+
1671+
1672+
@Component({
1673+
template: `
1674+
<form [formGroup]="form">
1675+
<mat-form-field>
1676+
<mat-chip-list formControlName="control">
1677+
<mat-chip>Pizza</mat-chip>
1678+
<mat-chip>Pasta</mat-chip>
1679+
</mat-chip-list>
1680+
</mat-form-field>
1681+
</form>
1682+
`
1683+
})
1684+
class ChipListInsideDynamicFormGroup {
1685+
@ViewChild(MatChipList) chipList: MatChipList;
1686+
form: FormGroup;
1687+
1688+
constructor(private _formBuilder: FormBuilder) {
1689+
this.assignGroup(false);
1690+
}
1691+
1692+
assignGroup(isDisabled: boolean) {
1693+
this.form = this._formBuilder.group({
1694+
control: {value: [], disabled: isDisabled}
1695+
});
1696+
}
1697+
}

src/material/chips/chip-list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
399399
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
400400
// that whatever logic is in here has to be super lean or we risk destroying the performance.
401401
this.updateErrorState();
402+
403+
if (this.ngControl.disabled !== this._disabled) {
404+
this.disabled = !!this.ngControl.disabled;
405+
}
402406
}
403407
}
404408

0 commit comments

Comments
 (0)