Skip to content

Commit

Permalink
fix(chips): allow null to be set as chip value (#16950)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and jelbourn committed Sep 9, 2019
1 parent 7594ca1 commit 57998a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/material-experimental/mdc-chips/chip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Subject} from 'rxjs';
import {MatChip, MatChipEvent, MatChipSet, MatChipsModule} from './index';


describe('Chips', () => {
describe('MatChip', () => {
let fixture: ComponentFixture<any>;
let chipDebugElement: DebugElement;
let chipNativeElement: HTMLElement;
Expand Down Expand Up @@ -135,6 +135,24 @@ describe('Chips', () => {
it('should not be focusable', () => {
expect(chipNativeElement.getAttribute('tabindex')).toBeFalsy();
});

it('should return the chip text if value is undefined', () => {
expect(chipInstance.value.trim()).toBe(fixture.componentInstance.name);
});

it('should return the chip value if defined', () => {
fixture.componentInstance.value = 123;
fixture.detectChanges();

expect(chipInstance.value).toBe(123);
});

it('should return the chip value if set to null', () => {
fixture.componentInstance.value = null;
fixture.detectChanges();

expect(chipInstance.value).toBeNull();
});
});
});

Expand All @@ -145,7 +163,7 @@ describe('Chips', () => {
<mat-chip [removable]="removable"
[color]="color" [disabled]="disabled"
(focus)="chipFocus($event)" (destroyed)="chipDestroy($event)"
(removed)="chipRemove($event)">
(removed)="chipRemove($event)" [value]="value">
{{name}}
</mat-chip>
</div>
Expand All @@ -158,6 +176,7 @@ class SingleChip {
color: string = 'primary';
removable: boolean = true;
shouldShow: boolean = true;
value: any;

chipFocus: (event?: MatChipEvent) => void = () => {};
chipDestroy: (event?: MatChipEvent) => void = () => {};
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
/** The value of the chip. Defaults to the content inside `<mat-chip>` tags. */
@Input()
get value(): any {
return this._value != undefined
return this._value !== undefined
? this._value
: this._elementRef.nativeElement.textContent;
}
Expand Down
23 changes: 21 additions & 2 deletions src/material/chips/chip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Subject} from 'rxjs';
import {MatChip, MatChipEvent, MatChipSelectionChange, MatChipsModule, MatChipList} from './index';


describe('Chips', () => {
describe('MatChip', () => {
let fixture: ComponentFixture<any>;
let chipDebugElement: DebugElement;
let chipNativeElement: HTMLElement;
Expand Down Expand Up @@ -214,6 +214,24 @@ describe('Chips', () => {

expect(chipInstance.rippleDisabled).toBe(true, 'Expected chip ripples to be disabled.');
});

it('should return the chip text if value is undefined', () => {
expect(chipInstance.value.trim()).toBe(fixture.componentInstance.name);
});

it('should return the chip value if defined', () => {
fixture.componentInstance.value = 123;
fixture.detectChanges();

expect(chipInstance.value).toBe(123);
});

it('should return the chip value if set to null', () => {
fixture.componentInstance.value = null;
fixture.detectChanges();

expect(chipInstance.value).toBeNull();
});
});

describe('keyboard behavior', () => {
Expand Down Expand Up @@ -396,7 +414,7 @@ describe('Chips', () => {
[color]="color" [selected]="selected" [disabled]="disabled"
(focus)="chipFocus($event)" (destroyed)="chipDestroy($event)"
(selectionChange)="chipSelectionChange($event)"
(removed)="chipRemove($event)">
(removed)="chipRemove($event)" [value]="value">
{{name}}
</mat-chip>
</div>
Expand All @@ -411,6 +429,7 @@ class SingleChip {
selectable: boolean = true;
removable: boolean = true;
shouldShow: boolean = true;
value: any;

chipFocus: (event?: MatChipEvent) => void = () => {};
chipDestroy: (event?: MatChipEvent) => void = () => {};
Expand Down
2 changes: 1 addition & 1 deletion src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
/** The value of the chip. Defaults to the content inside `<mat-chip>` tags. */
@Input()
get value(): any {
return this._value != undefined
return this._value !== undefined
? this._value
: this._elementRef.nativeElement.textContent;
}
Expand Down

0 comments on commit 57998a2

Please sign in to comment.