Skip to content

Commit 57998a2

Browse files
crisbetojelbourn
authored andcommitted
fix(chips): allow null to be set as chip value (#16950)
Allows for the value `null` to be set as the value of a chip. Previously we were treating it as if it's `undefined`, presumably because of a type coercion. Fixes #16844.
1 parent 7594ca1 commit 57998a2

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {Subject} from 'rxjs';
88
import {MatChip, MatChipEvent, MatChipSet, MatChipsModule} from './index';
99

1010

11-
describe('Chips', () => {
11+
describe('MatChip', () => {
1212
let fixture: ComponentFixture<any>;
1313
let chipDebugElement: DebugElement;
1414
let chipNativeElement: HTMLElement;
@@ -135,6 +135,24 @@ describe('Chips', () => {
135135
it('should not be focusable', () => {
136136
expect(chipNativeElement.getAttribute('tabindex')).toBeFalsy();
137137
});
138+
139+
it('should return the chip text if value is undefined', () => {
140+
expect(chipInstance.value.trim()).toBe(fixture.componentInstance.name);
141+
});
142+
143+
it('should return the chip value if defined', () => {
144+
fixture.componentInstance.value = 123;
145+
fixture.detectChanges();
146+
147+
expect(chipInstance.value).toBe(123);
148+
});
149+
150+
it('should return the chip value if set to null', () => {
151+
fixture.componentInstance.value = null;
152+
fixture.detectChanges();
153+
154+
expect(chipInstance.value).toBeNull();
155+
});
138156
});
139157
});
140158

@@ -145,7 +163,7 @@ describe('Chips', () => {
145163
<mat-chip [removable]="removable"
146164
[color]="color" [disabled]="disabled"
147165
(focus)="chipFocus($event)" (destroyed)="chipDestroy($event)"
148-
(removed)="chipRemove($event)">
166+
(removed)="chipRemove($event)" [value]="value">
149167
{{name}}
150168
</mat-chip>
151169
</div>
@@ -158,6 +176,7 @@ class SingleChip {
158176
color: string = 'primary';
159177
removable: boolean = true;
160178
shouldShow: boolean = true;
179+
value: any;
161180

162181
chipFocus: (event?: MatChipEvent) => void = () => {};
163182
chipDestroy: (event?: MatChipEvent) => void = () => {};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
155155
/** The value of the chip. Defaults to the content inside `<mat-chip>` tags. */
156156
@Input()
157157
get value(): any {
158-
return this._value != undefined
158+
return this._value !== undefined
159159
? this._value
160160
: this._elementRef.nativeElement.textContent;
161161
}

src/material/chips/chip.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {Subject} from 'rxjs';
99
import {MatChip, MatChipEvent, MatChipSelectionChange, MatChipsModule, MatChipList} from './index';
1010

1111

12-
describe('Chips', () => {
12+
describe('MatChip', () => {
1313
let fixture: ComponentFixture<any>;
1414
let chipDebugElement: DebugElement;
1515
let chipNativeElement: HTMLElement;
@@ -214,6 +214,24 @@ describe('Chips', () => {
214214

215215
expect(chipInstance.rippleDisabled).toBe(true, 'Expected chip ripples to be disabled.');
216216
});
217+
218+
it('should return the chip text if value is undefined', () => {
219+
expect(chipInstance.value.trim()).toBe(fixture.componentInstance.name);
220+
});
221+
222+
it('should return the chip value if defined', () => {
223+
fixture.componentInstance.value = 123;
224+
fixture.detectChanges();
225+
226+
expect(chipInstance.value).toBe(123);
227+
});
228+
229+
it('should return the chip value if set to null', () => {
230+
fixture.componentInstance.value = null;
231+
fixture.detectChanges();
232+
233+
expect(chipInstance.value).toBeNull();
234+
});
217235
});
218236

219237
describe('keyboard behavior', () => {
@@ -396,7 +414,7 @@ describe('Chips', () => {
396414
[color]="color" [selected]="selected" [disabled]="disabled"
397415
(focus)="chipFocus($event)" (destroyed)="chipDestroy($event)"
398416
(selectionChange)="chipSelectionChange($event)"
399-
(removed)="chipRemove($event)">
417+
(removed)="chipRemove($event)" [value]="value">
400418
{{name}}
401419
</mat-chip>
402420
</div>
@@ -411,6 +429,7 @@ class SingleChip {
411429
selectable: boolean = true;
412430
removable: boolean = true;
413431
shouldShow: boolean = true;
432+
value: any;
414433

415434
chipFocus: (event?: MatChipEvent) => void = () => {};
416435
chipDestroy: (event?: MatChipEvent) => void = () => {};

src/material/chips/chip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
176176
/** The value of the chip. Defaults to the content inside `<mat-chip>` tags. */
177177
@Input()
178178
get value(): any {
179-
return this._value != undefined
179+
return this._value !== undefined
180180
? this._value
181181
: this._elementRef.nativeElement.textContent;
182182
}

0 commit comments

Comments
 (0)