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-list): external changes to selection model not being reflected #9846

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
22 changes: 22 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ describe('MatSelectionList without forms', () => {
expect(list.selectedOptions.isSelected(listOptions[0].componentInstance)).toBe(true);
expect(list.selectedOptions.isSelected(listOptions[2].componentInstance)).toBe(true);
});

it('should update the item selected state when it is selected via the model', () => {
const list: MatSelectionList = selectionList.componentInstance;
const item: MatListOption = listOptions[0].componentInstance;

expect(item.selected).toBe(false);

list.selectedOptions.select(item);
fixture.detectChanges();

expect(item.selected).toBe(true);
});
});

describe('with list option selected', () => {
Expand Down Expand Up @@ -715,6 +727,16 @@ describe('MatSelectionList with forms', () => {
expect(fixture.componentInstance.selectedOptions).toEqual(['opt2']);
}));

it('should update the model if an option got selected via the model', fakeAsync(() => {
expect(fixture.componentInstance.selectedOptions).toEqual([]);

selectionList.selectedOptions.select(listOptions[0]);
fixture.detectChanges();
tick();

expect(fixture.componentInstance.selectedOptions).toEqual(['opt1']);
}));

});

describe('and formControl', () => {
Expand Down
28 changes: 25 additions & 3 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
mixinTabIndex,
} from '@angular/material/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {Subscription} from 'rxjs/Subscription';


/** @docs-private */
Expand Down Expand Up @@ -112,8 +113,8 @@ export class MatListOption extends _MatListOptionMixinBase
implements AfterContentInit, OnDestroy, OnInit, FocusableOption, CanDisableRipple {

private _lineSetter: MatLineSetter;
private _selected: boolean = false;
private _disabled: boolean = false;
private _selected = false;
private _disabled = false;

/** Whether the option has focus. */
_hasFocus: boolean = false;
Expand Down Expand Up @@ -292,7 +293,7 @@ export class MatListOption extends _MatListOptionMixinBase
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption,
CanDisable, CanDisableRipple, HasTabIndex, AfterContentInit, ControlValueAccessor {
CanDisable, CanDisableRipple, HasTabIndex, AfterContentInit, ControlValueAccessor, OnDestroy {

/** The FocusKeyManager which handles focus. */
_keyManager: FocusKeyManager<MatListOption>;
Expand All @@ -313,6 +314,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
/** Used for storing the values that were assigned before the options were initialized. */
private _tempValues: string[]|null;

private _modelChanges = Subscription.EMPTY;

/** View to model callback that should be called if the list or its options lost focus. */
_onTouched: () => void = () => {};

Expand All @@ -329,6 +332,25 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
this._setOptionsFromValues(this._tempValues);
this._tempValues = null;
}

// Sync external changes to the model back to the options.
this._modelChanges = this.selectedOptions.onChange!.subscribe(event => {
if (event.added) {
for (let item of event.added) {
item.selected = true;
}
}

if (event.removed) {
for (let item of event.removed) {
item.selected = false;
}
}
});
}

ngOnDestroy() {
this._modelChanges.unsubscribe();
}

/** Focus the selection-list. */
Expand Down