Skip to content

Commit e52beeb

Browse files
devversionandrewseguin
authored andcommitted
feat(selection-model): de/select multiple values at the same time (#7001)
* feat(selection-model): de/select multiple values at the same time Adds support for passing multiple values to the SelectionModel at the same time. This feature is described in the JSDoc already, but just wasn't implemented yet. This functionality gives us more control about the `onChange` event, which will otherwise fire every time if for example multiple values need to be selected. * Address comments
1 parent 5da9e64 commit e52beeb

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/cdk/collections/selection.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {SelectionModel} from './selection';
1+
import {getMultipleValuesInSingleSelectionError, SelectionModel} from './selection';
22

33

44
describe('SelectionModel', () => {
@@ -22,6 +22,10 @@ describe('SelectionModel', () => {
2222
expect(model.isSelected(2)).toBe(true);
2323
});
2424

25+
it('should throw an error if multiple values are passed to model', () => {
26+
expect(() => model.select(1, 2)).toThrow(getMultipleValuesInSingleSelectionError());
27+
});
28+
2529
it('should only preselect one value', () => {
2630
model = new SelectionModel(false, [1, 2]);
2731

@@ -36,13 +40,29 @@ describe('SelectionModel', () => {
3640

3741
beforeEach(() => model = new SelectionModel(true));
3842

39-
it('should be able to select multiple options at the same time', () => {
43+
it('should be able to select multiple options', () => {
44+
const onChangeSpy = jasmine.createSpy('onChange spy');
45+
46+
model.onChange!.subscribe(onChangeSpy);
4047
model.select(1);
4148
model.select(2);
4249

4350
expect(model.selected.length).toBe(2);
4451
expect(model.isSelected(1)).toBe(true);
4552
expect(model.isSelected(2)).toBe(true);
53+
expect(onChangeSpy).toHaveBeenCalledTimes(2);
54+
});
55+
56+
it('should be able to select multiple options at the same time', () => {
57+
const onChangeSpy = jasmine.createSpy('onChange spy');
58+
59+
model.onChange!.subscribe(onChangeSpy);
60+
model.select(1, 2);
61+
62+
expect(model.selected.length).toBe(2);
63+
expect(model.isSelected(1)).toBe(true);
64+
expect(model.isSelected(2)).toBe(true);
65+
expect(onChangeSpy).toHaveBeenCalledTimes(1);
4666
});
4767

4868
it('should be able to preselect multiple options', () => {

src/cdk/collections/selection.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ export class SelectionModel<T> {
5656
/**
5757
* Selects a value or an array of values.
5858
*/
59-
select(value: T): void {
60-
this._markSelected(value);
59+
select(...values: T[]): void {
60+
this._verifyValueAssignment(values);
61+
values.forEach(value => this._markSelected(value));
6162
this._emitChangeEvent();
6263
}
6364

6465
/**
6566
* Deselects a value or an array of values.
6667
*/
67-
deselect(value: T): void {
68-
this._unmarkSelected(value);
68+
deselect(...values: T[]): void {
69+
this._verifyValueAssignment(values);
70+
values.forEach(value => this._unmarkSelected(value));
6971
this._emitChangeEvent();
7072
}
7173

@@ -162,6 +164,16 @@ export class SelectionModel<T> {
162164
this._selection.forEach(value => this._unmarkSelected(value));
163165
}
164166
}
167+
168+
/**
169+
* Verifies the value assignment and throws an error if the specified value array is
170+
* including multiple values while the selection model is not supporting multiple values.
171+
*/
172+
private _verifyValueAssignment(values: T[]) {
173+
if (values.length > 1 && !this._isMulti) {
174+
throw getMultipleValuesInSingleSelectionError();
175+
}
176+
}
165177
}
166178

167179
/**
@@ -171,3 +183,11 @@ export class SelectionModel<T> {
171183
export class SelectionChange<T> {
172184
constructor(public added?: T[], public removed?: T[]) { }
173185
}
186+
187+
/**
188+
* Returns an error that reports that multiple values are passed into a selection model
189+
* with a single value.
190+
*/
191+
export function getMultipleValuesInSingleSelectionError() {
192+
return Error('Cannot pass multiple values into SelectionModel with single-value mode.');
193+
}

0 commit comments

Comments
 (0)