-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Description
Bug, feature request, or proposal:
If you create a SelectionModel with isMulti=false
and pass an empty array for initiallySelectedValues?: T[]
it results in undefined
being being added to the selection therefore you get selection.selected.length == 1
instead of the expected 0.
What is the expected behavior?
var selection = new SelectionModel<CustomerSearchResult>(false, []);
//Expect `selection.selected.length == 0`
What is the current behavior?
var selection = new SelectionModel<CustomerSearchResult>(false, []);
// selection.selected.length == 1
What are the steps to reproduce?
Code above is sufficient.
What is the use-case or motivation for changing an existing behavior?
It is confusing to the user, especially given that the method signature is initiallySelectedValues?: T[]
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Material CDK
Is there anything else we should know?
Suggested fix :
Add
&& initiallySelectedValues.length
to the firstif
statement for the constructor.
This would also result in the unnecessary forEach being skipped when isMulti==true
Then if []
is provided selected
will remain an empty array.
Also it would be nice if I could just pass in the value I want selected instead of it needing to be an array. That's another point I think should be addressed.
constructor(
private _isMulti = false,
initiallySelectedValues?: T[],
private _emitChanges = true) {
if (initiallySelectedValues && initiallySelectedValues.length) {
if (_isMulti) {
initiallySelectedValues.forEach(value => this._markSelected(value));
} else {
this._markSelected(initiallySelectedValues[0]);
}
// Clear the array in order to avoid firing the change event for preselected values.
this._selectedToEmit.length = 0;
}
}