Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/material/chips/chip-listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,26 @@ describe('MatChipListbox', () => {
.toEqual('steak-0');
});

it('should propagate null, not undefined, when the selected chip is deselected', () => {
dispatchKeyboardEvent(primaryActions[0], 'keydown', SPACE);
fixture.detectChanges();

expect(fixture.componentInstance.control.value)
.withContext(`Expected control's value to be set to the new option.`)
.toEqual('steak-0');

dispatchKeyboardEvent(primaryActions[0], 'keydown', SPACE);
fixture.detectChanges();

expect(fixture.componentInstance.control.value)
.withContext(
`Expected control's value to be null after deselecting the only selected ` +
`chip, not undefined - consumers (e.g. Angular Signal Forms) treat a value of ` +
`undefined as "this field no longer exists" rather than "this field is empty".`,
)
.toEqual(null);
});

it('should clear the selection when a nonexistent option value is selected', () => {
const array = chips.toArray();

Expand Down
2 changes: 1 addition & 1 deletion src/material/chips/chip-listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export class MatChipListbox
if (Array.isArray(this.selected)) {
valueToEmit = this.selected.map(chip => chip.value);
} else {
valueToEmit = this.selected ? this.selected.value : undefined;
valueToEmit = this.selected ? this.selected.value : null;
}
this._value = valueToEmit;
this.change.emit(new MatChipListboxChange(this, valueToEmit));
Expand Down