Skip to content

Commit 5d0fb95

Browse files
crisbetojelbourn
authored andcommitted
fix(selection-model): incorrect initial value when empty array is passed in single-selection mode (#9287)
* Fixes the selection model being initialized to `[undefined]` when an empty array is passed in as the initial value in single-selection mode. * Minor cleanup. Fixes #9273.
1 parent 2bf9ad8 commit 5d0fb95

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/cdk/collections/selection.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,8 @@ describe('SelectionModel', () => {
252252
expect(model.selected.length).toBe(0);
253253
expect(model.isEmpty()).toBe(true);
254254
});
255+
256+
it('should be empty if an empty array is passed for the preselected values', () => {
257+
expect(new SelectionModel(false, []).selected).toEqual([]);
258+
});
255259
});

src/cdk/collections/selection.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export class SelectionModel<T> {
1818
/** Keeps track of the deselected options that haven't been emitted by the change event. */
1919
private _deselectedToEmit: T[] = [];
2020

21-
/** Keeps track of the selected option that haven't been emitted by the change event. */
21+
/** Keeps track of the selected options that haven't been emitted by the change event. */
2222
private _selectedToEmit: T[] = [];
2323

2424
/** Cache for the array value of the selected items. */
2525
private _selected: T[] | null;
2626

27-
/** Selected value(s). */
27+
/** Selected values. */
2828
get selected(): T[] {
2929
if (!this._selected) {
3030
this._selected = Array.from(this._selection.values());
@@ -37,12 +37,12 @@ export class SelectionModel<T> {
3737
onChange: Subject<SelectionChange<T>> | null = this._emitChanges ? new Subject() : null;
3838

3939
constructor(
40-
private _isMulti = false,
40+
private _multiple = false,
4141
initiallySelectedValues?: T[],
4242
private _emitChanges = true) {
4343

44-
if (initiallySelectedValues) {
45-
if (_isMulti) {
44+
if (initiallySelectedValues && initiallySelectedValues.length) {
45+
if (_multiple) {
4646
initiallySelectedValues.forEach(value => this._markSelected(value));
4747
} else {
4848
this._markSelected(initiallySelectedValues[0]);
@@ -111,7 +111,7 @@ export class SelectionModel<T> {
111111
* Sorts the selected values based on a predicate function.
112112
*/
113113
sort(predicate?: (a: T, b: T) => number): void {
114-
if (this._isMulti && this._selected) {
114+
if (this._multiple && this._selected) {
115115
this._selected.sort(predicate);
116116
}
117117
}
@@ -136,7 +136,7 @@ export class SelectionModel<T> {
136136
/** Selects a value. */
137137
private _markSelected(value: T) {
138138
if (!this.isSelected(value)) {
139-
if (!this._isMulti) {
139+
if (!this._multiple) {
140140
this._unmarkAll();
141141
}
142142

@@ -171,7 +171,7 @@ export class SelectionModel<T> {
171171
* including multiple values while the selection model is not supporting multiple values.
172172
*/
173173
private _verifyValueAssignment(values: T[]) {
174-
if (values.length > 1 && !this._isMulti) {
174+
if (values.length > 1 && !this._multiple) {
175175
throw getMultipleValuesInSingleSelectionError();
176176
}
177177
}

0 commit comments

Comments
 (0)