Skip to content

fix(chips): newly-added chips not being disabled when added to a disabled list #14976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ describe('MatChipList', () => {

expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
});

it('should disable a chip that is added after the list became disabled', fakeAsync(() => {
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);

chipListInstance.disabled = true;
fixture.detectChanges();

expect(chips.toArray().every(chip => chip.disabled)).toBe(true);

fixture.componentInstance.items.push(5, 6);
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
}));

});

describe('with selected chips', () => {
Expand Down Expand Up @@ -1305,7 +1322,7 @@ describe('MatChipList', () => {
@Component({
template: `
<mat-chip-list [tabIndex]="tabIndex" [selectable]="selectable">
<div *ngFor="let i of [0,1,2,3,4]">
<div *ngFor="let i of items">
<div *ngIf="remove != i">
<mat-chip (select)="chipSelect(i)" (deselect)="chipDeselect(i)">
{{name}} {{i + 1}}
Expand All @@ -1315,6 +1332,7 @@ describe('MatChipList', () => {
</mat-chip-list>`
})
class StandardChipList {
items = [0, 1, 2, 3, 4];
name: string = 'Test';
selectable: boolean = true;
remove: number;
Expand Down
22 changes: 18 additions & 4 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
get disabled(): boolean { return this.ngControl ? !!this.ngControl.disabled : this._disabled; }
set disabled(value: boolean) {
this._disabled = coerceBooleanProperty(value);

if (this.chips) {
this.chips.forEach(chip => chip.disabled = this._disabled);
}
this._syncChipsDisabledState();
}
protected _disabled: boolean = false;

Expand Down Expand Up @@ -370,6 +367,14 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo

// When the list changes, re-subscribe
this.chips.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => {
if (this.disabled) {
// Since this happens after the content has been
// checked, we need to defer it to the next tick.
Promise.resolve().then(() => {
this._syncChipsDisabledState();
});
}

this._resetChips();

// Reset chips selected/deselected status
Expand Down Expand Up @@ -775,4 +780,13 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
private _hasFocusedChip() {
return this.chips.some(chip => chip._hasFocus);
}

/** Syncs the list's disabled state with the individual chips. */
private _syncChipsDisabledState() {
if (this.chips) {
this.chips.forEach(chip => {
chip.disabled = this._disabled;
});
}
}
}