Skip to content

Commit dd8c807

Browse files
crisbetojelbourn
authored andcommitted
fix(select): handle null values in multi-select (#11792)
Currently toggling an option with a `null` value in a multi-select will deselect all other options while toggling the current one. These changes switch to allowing for `null` value to be selected in `multiple` mode.
1 parent 255c10a commit dd8c807

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/lib/select/select.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,28 @@ describe('MatSelect', () => {
38473847
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(2);
38483848
}));
38493849

3850+
it('should be to select an option with a `null` value', fakeAsync(() => {
3851+
fixture.componentInstance.foods = [
3852+
{ value: null, viewValue: 'Steak' },
3853+
{ value: 'pizza-1', viewValue: 'Pizza' },
3854+
{ value: null, viewValue: 'Tacos' },
3855+
];
3856+
3857+
fixture.detectChanges();
3858+
trigger.click();
3859+
fixture.detectChanges();
3860+
3861+
const options = overlayContainerElement.querySelectorAll('mat-option') as
3862+
NodeListOf<HTMLElement>;
3863+
3864+
options[0].click();
3865+
options[1].click();
3866+
options[2].click();
3867+
fixture.detectChanges();
3868+
3869+
expect(testInstance.control.value).toEqual([null, 'pizza-1', null]);
3870+
}));
3871+
38503872
});
38513873
});
38523874

src/lib/select/select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,18 +870,18 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
870870
private _onSelect(option: MatOption, isUserInput: boolean): void {
871871
const wasSelected = this._selectionModel.isSelected(option);
872872

873-
if (option.value == null) {
873+
if (option.value == null && !this._multiple) {
874874
this._selectionModel.clear();
875875
this._propagateChanges(option.value);
876876
} else {
877877
option.selected ? this._selectionModel.select(option) : this._selectionModel.deselect(option);
878878

879-
// TODO(crisbeto): handle blank/null options inside multi-select.
880879
if (this.multiple) {
881880
this._sortValues();
882881

883882
if (isUserInput) {
884883
this._keyManager.setActiveItem(option);
884+
885885
// In case the user selected the option with their mouse, we
886886
// want to restore focus back to the trigger, in order to
887887
// prevent the select keyboard controls from clashing with

0 commit comments

Comments
 (0)