Skip to content

Commit 029e363

Browse files
fix(material-experimental/mdc-chips) Replace default values (#19006)
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 bf2c910 commit 029e363

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
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: Array<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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
173173
}
174174
protected _disabled: boolean = false;
175175

176+
private _textElement!: HTMLElement;
176177

177-
/** The value of the chip. Defaults to the content inside `<mat-chip>` tags. */
178+
/** The value of the chip. Defaults to the content inside the mdc-chip__text element. */
178179
@Input()
179180
get value(): any {
180181
return this._value !== undefined
181182
? this._value
182-
: this._elementRef.nativeElement.textContent;
183+
: this._textElement.textContent!.trim();
183184
}
184185
set value(value: any) { this._value = value; }
185186
protected _value: any;
@@ -321,7 +322,6 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
321322
this._animationsDisabled = animationMode === 'NoopAnimations';
322323
this._isBasicChip = _elementRef.nativeElement.hasAttribute(this.basicChipAttrName) ||
323324
_elementRef.nativeElement.tagName.toLowerCase() === this.basicChipAttrName;
324-
325325
}
326326

327327
ngAfterContentInit() {
@@ -330,6 +330,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
330330

331331
ngAfterViewInit() {
332332
this._chipFoundation.init();
333+
this._textElement = this._elementRef.nativeElement.querySelector('.mdc-chip__text');
333334
}
334335

335336
ngOnDestroy() {

0 commit comments

Comments
 (0)