Skip to content

Commit

Permalink
fix(chips): do not set chips value if there's no ngControl or value (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao authored and andrewseguin committed Sep 29, 2017
1 parent 86bea91 commit d9ba13f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('MatChipList', () => {
InputChipList,
MultiSelectionChipList,
FalsyValueChipList,
SelectedChipList
],
providers: [{
provide: Directionality, useFactory: () => {
Expand All @@ -63,6 +64,21 @@ describe('MatChipList', () => {
});
});

describe('with selected chips', () => {
beforeEach(async(() => {
fixture = TestBed.createComponent(SelectedChipList);
fixture.detectChanges();
}));

it('should not override chips selected', () => {
const instanceChips = fixture.componentInstance.chips.toArray();

expect(instanceChips[0].selected).toBe(true, 'Expected first option to be selected.');
expect(instanceChips[1].selected).toBe(false, 'Expected second option to be not selected.');
expect(instanceChips[2].selected).toBe(true, 'Expected third option to be selected.');
});
});

describe('focus behaviors', () => {
beforeEach(async(() => {
setupStandardList();
Expand Down Expand Up @@ -1027,3 +1043,21 @@ class FalsyValueChipList {
control = new FormControl();
@ViewChildren(MatChip) chips: QueryList<MatChip>;
}

@Component({
template: `
<mat-chip-list>
<mat-chip *ngFor="let food of foods" [value]="food.value" [selected]="food.selected">
{{ food.viewValue }}
</mat-chip>
</mat-chip-list>
`
})
class SelectedChipList {
foods: any[] = [
{ value: 0, viewValue: 'Steak', selected: true },
{ value: 1, viewValue: 'Pizza', selected: false },
{ value: 2, viewValue: 'Pasta', selected: true },
];
@ViewChildren(MdChip) chips: QueryList<MdChip>;
}
6 changes: 4 additions & 2 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,10 @@ export class MatChipList implements MatFormFieldControl<any>, ControlValueAccess
// Defer setting the value in order to avoid the "Expression
// has changed after it was checked" errors from Angular.
Promise.resolve().then(() => {
this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value, false);
this.stateChanges.next();
if (this.ngControl || this._value) {
this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value, false);
this.stateChanges.next();
}
});
}

Expand Down

0 comments on commit d9ba13f

Please sign in to comment.