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

fix(selection-model): incorrect initial value when empty array is passed in single-selection mode #9287

Merged
merged 1 commit into from
Jan 23, 2018
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
4 changes: 4 additions & 0 deletions src/cdk/collections/selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,8 @@ describe('SelectionModel', () => {
expect(model.selected.length).toBe(0);
expect(model.isEmpty()).toBe(true);
});

it('should be empty if an empty array is passed for the preselected values', () => {
expect(new SelectionModel(false, []).selected).toEqual([]);
});
});
16 changes: 8 additions & 8 deletions src/cdk/collections/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export class SelectionModel<T> {
/** Keeps track of the deselected options that haven't been emitted by the change event. */
private _deselectedToEmit: T[] = [];

/** Keeps track of the selected option that haven't been emitted by the change event. */
/** Keeps track of the selected options that haven't been emitted by the change event. */
private _selectedToEmit: T[] = [];

/** Cache for the array value of the selected items. */
private _selected: T[] | null;

/** Selected value(s). */
/** Selected values. */
get selected(): T[] {
if (!this._selected) {
this._selected = Array.from(this._selection.values());
Expand All @@ -37,12 +37,12 @@ export class SelectionModel<T> {
onChange: Subject<SelectionChange<T>> | null = this._emitChanges ? new Subject() : null;

constructor(
private _isMulti = false,
private _multiple = false,
initiallySelectedValues?: T[],
private _emitChanges = true) {

if (initiallySelectedValues) {
if (_isMulti) {
if (initiallySelectedValues && initiallySelectedValues.length) {
if (_multiple) {
initiallySelectedValues.forEach(value => this._markSelected(value));
} else {
this._markSelected(initiallySelectedValues[0]);
Expand Down Expand Up @@ -111,7 +111,7 @@ export class SelectionModel<T> {
* Sorts the selected values based on a predicate function.
*/
sort(predicate?: (a: T, b: T) => number): void {
if (this._isMulti && this._selected) {
if (this._multiple && this._selected) {
this._selected.sort(predicate);
}
}
Expand All @@ -136,7 +136,7 @@ export class SelectionModel<T> {
/** Selects a value. */
private _markSelected(value: T) {
if (!this.isSelected(value)) {
if (!this._isMulti) {
if (!this._multiple) {
this._unmarkAll();
}

Expand Down Expand Up @@ -171,7 +171,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._isMulti) {
if (values.length > 1 && !this._multiple) {
throw getMultipleValuesInSingleSelectionError();
}
}
Expand Down