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 committed Sep 26, 2017
1 parent 3571f68 commit 638adf4
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('MdChipList', () => {
InputChipList,
MultiSelectionChipList,
FalsyValueChipList,
SelectedChipList
],
providers: [{
provide: Directionality, useFactory: () => {
Expand All @@ -63,6 +64,21 @@ describe('MdChipList', () => {
});
});

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(MdChip) chips: QueryList<MdChip>;
}

@Component({
template: `
<md-chip-list>
<md-chip *ngFor="let food of foods" [value]="food.value" [selected]="food.selected">
{{ food.viewValue }}
</md-chip>
</md-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 @@ -572,8 +572,10 @@ export class MdChipList implements MdFormFieldControl<any>, ControlValueAccessor
// 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 638adf4

Please sign in to comment.