Skip to content

feat(collections): add isMultipleSelection function to SelectionModel #11560

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 6 commits into from
Jun 7, 2018
Merged
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions src/cdk/collections/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export class SelectionModel<T> {
return this._selected;
}

/** Whether multiple values can be selected. */
get multiple() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a unit test that runs against this public API

return this._multiple;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a function isMultipleSelection instead of a getter; TypeScript generates more code for a getter, so it's preferrable to use a function if you aren't required to use a getter/setter (like with a directive binding)


/** Event emitted when the value has changed. */
onChange: Subject<SelectionChange<T>> | null = this._emitChanges ? new Subject() : null;

Expand Down Expand Up @@ -111,7 +116,7 @@ export class SelectionModel<T> {
* Sorts the selected values based on a predicate function.
*/
sort(predicate?: (a: T, b: T) => number): void {
if (this._multiple && this._selected) {
if (this.multiple && this._selected) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that for usage of the multiple value within the class that we can continue to just use the internal _multiple reference. This would leave the multiple value as a just a getter

Copy link
Contributor Author

@glenstaes glenstaes May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to revert them? One could also argue that in the future the getter will have a more complex implementation so this change would make it future proof.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, yes, it should reference the internal property. If the behavior changes then it could be updated

this._selected.sort(predicate);
}
}
Expand All @@ -138,7 +143,7 @@ export class SelectionModel<T> {
/** Selects a value. */
private _markSelected(value: T) {
if (!this.isSelected(value)) {
if (!this._multiple) {
if (!this.multiple) {
this._unmarkAll();
}

Expand Down Expand Up @@ -173,7 +178,7 @@ export class SelectionModel<T> {
* including multiple values while the selection model is not supporting multiple values.
*/
private _verifyValueAssignment(values: T[]) {
if (values.length > 1 && !this._multiple) {
if (values.length > 1 && !this.multiple) {
throw getMultipleValuesInSingleSelectionError();
}
}
Expand Down