Skip to content

fix(selection-list): preselected options not being added to the model value #9116

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
56 changes: 55 additions & 1 deletion src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ describe('MatSelectionList with forms', () => {
imports: [MatListModule, FormsModule, ReactiveFormsModule],
declarations: [
SelectionListWithModel,
SelectionListWithFormControl
SelectionListWithFormControl,
SelectionListWithPreselectedOption,
SelectionListWithPreselectedOptionAndModel
]
});

Expand Down Expand Up @@ -696,6 +698,34 @@ describe('MatSelectionList with forms', () => {
.toBe(true, 'Expected every list option to be unselected.');
});
});

describe('preselected values', () => {
it('should add preselected options to the model value', fakeAsync(() => {
const fixture = TestBed.createComponent(SelectionListWithPreselectedOption);
const listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.componentInstance);

fixture.detectChanges();
tick();

expect(listOptions[1].selected).toBe(true);
expect(fixture.componentInstance.selectedOptions).toEqual(['opt2']);
}));

it('should handle preselected option both through the model and the view', fakeAsync(() => {
const fixture = TestBed.createComponent(SelectionListWithPreselectedOptionAndModel);
const listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.componentInstance);

fixture.detectChanges();
tick();

expect(listOptions[0].selected).toBe(true);
expect(listOptions[1].selected).toBe(true);
expect(fixture.componentInstance.selectedOptions).toEqual(['opt1', 'opt2']);
}));

});
});


Expand Down Expand Up @@ -819,3 +849,27 @@ class SelectionListWithModel {
class SelectionListWithFormControl {
formControl = new FormControl();
}


@Component({
template: `
<mat-selection-list [(ngModel)]="selectedOptions">
<mat-list-option value="opt1">Option 1</mat-list-option>
<mat-list-option value="opt2" selected>Option 2</mat-list-option>
</mat-selection-list>`
})
class SelectionListWithPreselectedOption {
selectedOptions: string[];
}


@Component({
template: `
<mat-selection-list [(ngModel)]="selectedOptions">
<mat-list-option value="opt1">Option 1</mat-list-option>
<mat-list-option value="opt2" selected>Option 2</mat-list-option>
</mat-selection-list>`
})
class SelectionListWithPreselectedOptionAndModel {
selectedOptions = ['opt1'];
}
4 changes: 2 additions & 2 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ export class MatListOption extends _MatListOptionMixinBase
}

ngOnInit() {
if (this.selected) {
if (this._selected) {
// List options that are selected at initialization can't be reported properly to the form
// control. This is because it takes some time until the selection-list knows about all
// available options. Also it can happen that the ControlValueAccessor has an initial value
// that should be used instead. Deferring the value change report to the next tick ensures
// that the form control value is not being overwritten.
Promise.resolve(() => this.selected && this.selectionList._reportValueChange());
Promise.resolve().then(() => this.selected = true);
}
}

Expand Down