Skip to content

Bug in SelectionModel single selection mode #10051

@wendy-w-wang

Description

@wendy-w-wang

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Single selection mode with empty initial selection should work

What is the current behavior?

Single selection mode doesn't handle empty selection

What are the steps to reproduce?

Source code that constructs a single-select model:
initialSelection = [];
allowMultiSelect = false;
selection = new SelectionModel(
this.allowMultiSelect, this.initialSelection);

What is the use-case or motivation for changing an existing behavior?

This model doesn't handle the scenario where there's no selection at initialization (e.g. empty table) when it's single selection mode.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Is there anything else we should know?

The problematic code is in SelectionModel's constructor...
constructor(
private _multiple = false,
initiallySelectedValues?: T[],
private _emitChanges = true) {

if (initiallySelectedValues && initiallySelectedValues.length) {
  if (_multiple) {
    initiallySelectedValues.forEach(value => this._markSelected(value));
  } else {
    this._markSelected(initiallySelectedValues[0]);  // BUG - we need to handle initiallySelectedValues being empty....
  }

  // Clear the array in order to avoid firing the change event for preselected values.
  this._selectedToEmit.length = 0;
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions