Skip to content

Commit b48c23a

Browse files
fix(material-experimental/mdc-chips) Replace default values
mat-chip-grid implements ControlValueAccessor. Its default value right now is undefined; once a chip is added, its value is an array of all the chip values. The default value for a given chip is all the text content inside the chip. For chips with icons, this causes the icon text to be included in the value, so a row chip using matChipRemove would have a value like " hello cancel ". (There's also some unwanted whitespace in the textContent). Replace undefined with an empty array, and the textContent with just the actual chip text, which in the case of row chips corresponds to the text that the user entered. These unintuitive values are the same as the old non-mdc mat-chip-listbox. But due to the way the old listbox implemented ControlValueAccessor, chips being used as a chip grid didn't actually ever call the onChange handler to update the FormControl value, so their values were basically ignored.
1 parent e0b5797 commit b48c23a

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/material-experimental/mdc-chips/chip-grid.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class MatChipGrid extends _MatChipGridMixinBase implements AfterContentIn
204204
set value(value: any) {
205205
this._value = value;
206206
}
207-
protected _value: any;
207+
protected _value: any = [];
208208

209209
/** Combined stream of all of the child chips' blur events. */
210210
get chipBlurChanges(): Observable<MatChipEvent> {
@@ -478,9 +478,9 @@ export class MatChipGrid extends _MatChipGridMixinBase implements AfterContentIn
478478
}
479479

480480
/** Emits change event to set the model value. */
481-
private _propagateChanges(fallbackValue?: any): void {
481+
private _propagateChanges(): void {
482482
const valueToEmit = this._chips.length ? this._chips.toArray().map(
483-
chip => chip.value) : fallbackValue;
483+
chip => chip.value) : [];
484484
this._value = valueToEmit;
485485
this.change.emit(new MatChipGridChange(this, valueToEmit));
486486
this.valueChange.emit(valueToEmit);

src/material-experimental/mdc-chips/chip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
179179
get value(): any {
180180
return this._value !== undefined
181181
? this._value
182-
: this._elementRef.nativeElement.textContent;
182+
: this._elementRef.nativeElement.querySelector('.mdc-chip__text').textContent.trim();
183183
}
184184
set value(value: any) { this._value = value; }
185185
protected _value: any;

0 commit comments

Comments
 (0)