Skip to content

Commit bea7e7f

Browse files
committed
fix(cdk/collections): SelectionModel setSelection method inconsistent with compareWith
Fix bug in SelectionModel where compareWith function was not consistently respected in setSelection method. Fixes #27425
1 parent c3f2a97 commit bea7e7f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/cdk/collections/selection-model.ts

Lines changed: 1 addition & 1 deletion
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(value))

src/cdk/collections/selection.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,15 @@ describe('SelectionModel', () => {
299299
model.deselect(v2);
300300
expect(model.selected.length).toBe(1);
301301
});
302+
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+
});
302313
});

0 commit comments

Comments
 (0)