Skip to content

fix(cdk/collections): SelectionModel setSelection method inconsistent… #27460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cdk/collections/selection-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class SelectionModel<T> {
setSelection(...values: T[]): boolean | void {
this._verifyValueAssignment(values);
const oldValues = this.selected;
const newSelectedSet = new Set(values);
const newSelectedSet = new Set(values.map(value => this._getConcreteValue(value)));
values.forEach(value => this._markSelected(value));
oldValues
.filter(value => !newSelectedSet.has(this._getConcreteValue(value, newSelectedSet)))
Expand Down
11 changes: 11 additions & 0 deletions src/cdk/collections/selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ describe('SelectionModel', () => {
expect(model.selected.length).toBe(1);
});

it('should not empty selection when caling setSelection twice with comparable', () => {
type Item = {id: number};
const compareFn = (x: Item, y: Item) => x.id === y.id;
const model = new SelectionModel<Item>(false, [], false, compareFn);
model.setSelection({id: 1});
expect(model.selected).toEqual([{id: 1}]);

model.setSelection({id: 1});
expect(model.selected).toEqual([{id: 1}]);
});

describe('setSelection', () => {
it('should not deselect an already selected value', () => {
type Item = {key: number; value: string};
Expand Down
Loading