Skip to content

Commit

Permalink
fix(selection-model): incorrect initial value when empty array is pas…
Browse files Browse the repository at this point in the history
…sed in single-selection mode (angular#9287)

* Fixes the selection model being initialized to `[undefined]` when an empty array is passed in as the initial value in single-selection mode.
* Minor cleanup.

Fixes angular#9273.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 29, 2018
1 parent 2bf9ad8 commit 5d0fb95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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

0 comments on commit 5d0fb95

Please sign in to comment.