Skip to content
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

feat(selection-model): de/select multiple values at the same time #7001

Merged
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
24 changes: 22 additions & 2 deletions src/cdk/collections/selection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SelectionModel} from './selection';
import {getMultipleValuesInSingleSelectionError, SelectionModel} from './selection';


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

it('should throw an error if multiple values are passed to model', () => {
expect(() => model.select(1, 2)).toThrow(getMultipleValuesInSingleSelectionError());
});

it('should only preselect one value', () => {
model = new SelectionModel(false, [1, 2]);

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

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

it('should be able to select multiple options at the same time', () => {
it('should be able to select multiple options', () => {
const onChangeSpy = jasmine.createSpy('onChange spy');

model.onChange!.subscribe(onChangeSpy);
model.select(1);
model.select(2);

expect(model.selected.length).toBe(2);
expect(model.isSelected(1)).toBe(true);
expect(model.isSelected(2)).toBe(true);
expect(onChangeSpy).toHaveBeenCalledTimes(2);
});

it('should be able to select multiple options at the same time', () => {
const onChangeSpy = jasmine.createSpy('onChange spy');

model.onChange!.subscribe(onChangeSpy);
model.select(1, 2);

expect(model.selected.length).toBe(2);
expect(model.isSelected(1)).toBe(true);
expect(model.isSelected(2)).toBe(true);
expect(onChangeSpy).toHaveBeenCalledTimes(1);
});

it('should be able to preselect multiple options', () => {
Expand Down
28 changes: 24 additions & 4 deletions src/cdk/collections/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ export class SelectionModel<T> {
/**
* Selects a value or an array of values.
*/
select(value: T): void {
this._markSelected(value);
select(...values: T[]): void {
this._verifyValueAssignment(values);
values.forEach(value => this._markSelected(value));
this._emitChangeEvent();
}

/**
* Deselects a value or an array of values.
*/
deselect(value: T): void {
this._unmarkSelected(value);
deselect(...values: T[]): void {
this._verifyValueAssignment(values);
values.forEach(value => this._unmarkSelected(value));
this._emitChangeEvent();
}

Expand Down Expand Up @@ -162,6 +164,16 @@ export class SelectionModel<T> {
this._selection.forEach(value => this._unmarkSelected(value));
}
}

/**
* Verifies the value assignment and throws an error if the specified value array is
* including multiple values while the selection model is not supporting multiple values.
*/
private _verifyValueAssignment(values: T[]) {
if (values.length > 1 && !this._isMulti) {
throw getMultipleValuesInSingleSelectionError();
}
}
}

/**
Expand All @@ -171,3 +183,11 @@ export class SelectionModel<T> {
export class SelectionChange<T> {
constructor(public added?: T[], public removed?: T[]) { }
}

/**
* Returns an error that reports that multiple values are passed into a selection model
* with a single value.
*/
export function getMultipleValuesInSingleSelectionError() {
return Error('Cannot pass multiple values into SelectionModel with single-value mode.');
}