Skip to content

bug(selection-model): setSelection in SelectionModel does not respect compareWith #27425

Closed
@iladovi

Description

@iladovi

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When using compareWith, setSelection method may deselect comparable items.
Cause is usage of Set and has method

const newSelectedSet = new Set(values);
values.forEach(value => this._markSelected(value));
oldValues
.filter(value => !newSelectedSet.has(value))
.forEach(value => this._unmarkSelected(value));

This line
const newSelectedSet = new Set(values);
could be replaced by
const newSelectedSet = new Set(values.map(value => this._getConcreteValue(value)));
to use compareWith

Reproduction

StackBlitz link: https://stackblitz.com/edit/angular-jfhnli?file=src%2Fmain.ts
Steps to reproduce:

  1. Instanciate a SelectionModel of object and non trivial compareWith function
  2. call setSelection method to select one object o1, selectionModel.selection should be [o1]
  3. call setSelection again with a copy o2 of previous object (o2 must be comparable to o1 according compareWith, ie compareWith(o1,o2) is true)

Expected Behavior

SelectionModel selection should be one of the selected objects, either [o1] or [o2]

Actual Behavior

SelectionModel selection is empty

Environment

  • Angular: 16
  • CDK/Material: 16
  • Browser(s): no browser needed
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: material/core

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions