Skip to content

Commit 1c2c9d9

Browse files
iladovimmalerba
authored andcommittedFeb 20, 2025·
fix(cdk/collections): SelectionModel setSelection method inconsistent with compareWith (#27460)
Fix bug in SelectionModel where compareWith function was not consistently respected in setSelection method. Fixes #27425 (cherry picked from commit 9494ff2)
1 parent fd09003 commit 1c2c9d9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed
 

‎src/cdk/collections/selection-model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class SelectionModel<T> {
9191
setSelection(...values: T[]): boolean | void {
9292
this._verifyValueAssignment(values);
9393
const oldValues = this.selected;
94-
const newSelectedSet = new Set(values);
94+
const newSelectedSet = new Set(values.map(value => this._getConcreteValue(value)));
9595
values.forEach(value => this._markSelected(value));
9696
oldValues
9797
.filter(value => !newSelectedSet.has(this._getConcreteValue(value, newSelectedSet)))

‎src/cdk/collections/selection.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ describe('SelectionModel', () => {
300300
expect(model.selected.length).toBe(1);
301301
});
302302

303+
it('should not empty selection when caling setSelection twice with comparable', () => {
304+
type Item = {id: number};
305+
const compareFn = (x: Item, y: Item) => x.id === y.id;
306+
const model = new SelectionModel<Item>(false, [], false, compareFn);
307+
model.setSelection({id: 1});
308+
expect(model.selected).toEqual([{id: 1}]);
309+
310+
model.setSelection({id: 1});
311+
expect(model.selected).toEqual([{id: 1}]);
312+
});
313+
303314
describe('setSelection', () => {
304315
it('should not deselect an already selected value', () => {
305316
type Item = {key: number; value: string};

0 commit comments

Comments
 (0)
Please sign in to comment.