Skip to content

fix(selection-list): options not marked as selected if value is assigned too early #9090

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 1 commit into from
Jan 4, 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
16 changes: 16 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,22 @@ describe('MatSelectionList with forms', () => {
expect(listOptions.every(option => !option.selected))
.toBe(true, 'Expected every list option to be unselected.');
});

it('should mark options as selected when the value is set before they are initialized', () => {
fixture.destroy();
fixture = TestBed.createComponent(SelectionListWithFormControl);
selectionListDebug = fixture.debugElement.query(By.directive(MatSelectionList));
selectionList = selectionListDebug.componentInstance;

fixture.componentInstance.formControl.setValue(['opt2', 'opt3']);
fixture.detectChanges();

listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.componentInstance);

expect(listOptions[1].selected).toBe(true, 'Expected second option to be selected.');
expect(listOptions[2].selected).toBe(true, 'Expected third option to be selected.');
});
});
});

Expand Down
10 changes: 10 additions & 0 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
/** View to model callback that should be called whenever the selected options change. */
private _onChange: (value: any) => void = (_: any) => {};

/** Used for storing the values that were assigned before the options were initialized. */
private _tempValues: string[]|null;

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

Expand All @@ -301,6 +304,11 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu

ngAfterContentInit(): void {
this._keyManager = new FocusKeyManager<MatListOption>(this.options).withWrap();

if (this._tempValues) {
this._setOptionsFromValues(this._tempValues);
this._tempValues = null;
}
}

/** Focus the selection-list. */
Expand Down Expand Up @@ -369,6 +377,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
writeValue(values: string[]): void {
if (this.options) {
this._setOptionsFromValues(values || []);
} else {
this._tempValues = values;
}
}

Expand Down