Skip to content

Commit c23512a

Browse files
crisbetommalerba
authored andcommittedFeb 4, 2019
fix(chips): newly-added chips not being disabled when added to a disable list (#14976)
Fixes newly-added chips not being disabled when they're added to a disabled chip list. The problem comes from the fact that the disabled state is only synced to the chips when it changes.
1 parent 916c532 commit c23512a

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed
 

‎src/lib/chips/chip-list.spec.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ describe('MatChipList', () => {
8686

8787
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
8888
});
89+
90+
it('should disable a chip that is added after the list became disabled', fakeAsync(() => {
91+
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
92+
93+
chipListInstance.disabled = true;
94+
fixture.detectChanges();
95+
96+
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
97+
98+
fixture.componentInstance.items.push(5, 6);
99+
fixture.detectChanges();
100+
tick();
101+
fixture.detectChanges();
102+
103+
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
104+
}));
105+
89106
});
90107

91108
describe('with selected chips', () => {
@@ -1305,7 +1322,7 @@ describe('MatChipList', () => {
13051322
@Component({
13061323
template: `
13071324
<mat-chip-list [tabIndex]="tabIndex" [selectable]="selectable">
1308-
<div *ngFor="let i of [0,1,2,3,4]">
1325+
<div *ngFor="let i of items">
13091326
<div *ngIf="remove != i">
13101327
<mat-chip (select)="chipSelect(i)" (deselect)="chipDeselect(i)">
13111328
{{name}} {{i + 1}}
@@ -1315,6 +1332,7 @@ describe('MatChipList', () => {
13151332
</mat-chip-list>`
13161333
})
13171334
class StandardChipList {
1335+
items = [0, 1, 2, 3, 4];
13181336
name: string = 'Test';
13191337
selectable: boolean = true;
13201338
remove: number;

‎src/lib/chips/chip-list.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
267267
get disabled(): boolean { return this.ngControl ? !!this.ngControl.disabled : this._disabled; }
268268
set disabled(value: boolean) {
269269
this._disabled = coerceBooleanProperty(value);
270-
271-
if (this.chips) {
272-
this.chips.forEach(chip => chip.disabled = this._disabled);
273-
}
270+
this._syncChipsDisabledState();
274271
}
275272
protected _disabled: boolean = false;
276273

@@ -370,6 +367,14 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
370367

371368
// When the list changes, re-subscribe
372369
this.chips.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => {
370+
if (this.disabled) {
371+
// Since this happens after the content has been
372+
// checked, we need to defer it to the next tick.
373+
Promise.resolve().then(() => {
374+
this._syncChipsDisabledState();
375+
});
376+
}
377+
373378
this._resetChips();
374379

375380
// Reset chips selected/deselected status
@@ -775,4 +780,13 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
775780
private _hasFocusedChip() {
776781
return this.chips.some(chip => chip._hasFocus);
777782
}
783+
784+
/** Syncs the list's disabled state with the individual chips. */
785+
private _syncChipsDisabledState() {
786+
if (this.chips) {
787+
this.chips.forEach(chip => {
788+
chip.disabled = this._disabled;
789+
});
790+
}
791+
}
778792
}

0 commit comments

Comments
 (0)
Please sign in to comment.