Skip to content

Commit 2235239

Browse files
crisbetojelbourn
authored andcommitted
fix(selection-list): preselected options not being added to the model value (angular#9116)
Fixes preselected list options not being added to the model due to: * The `Promise.resolve` inside the `ngOnInit` not actually doing anything due to a missing `.then`. * The option not being added to the selection model when the value is emitted, which means that it won't be a part of the model.
1 parent f64a857 commit 2235239

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/lib/list/selection-list.spec.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ describe('MatSelectionList with forms', () => {
556556
imports: [MatListModule, FormsModule, ReactiveFormsModule],
557557
declarations: [
558558
SelectionListWithModel,
559-
SelectionListWithFormControl
559+
SelectionListWithFormControl,
560+
SelectionListWithPreselectedOption,
561+
SelectionListWithPreselectedOptionAndModel
560562
]
561563
});
562564

@@ -719,6 +721,34 @@ describe('MatSelectionList with forms', () => {
719721
expect(listOptions[2].selected).toBe(true, 'Expected third option to be selected.');
720722
});
721723
});
724+
725+
describe('preselected values', () => {
726+
it('should add preselected options to the model value', fakeAsync(() => {
727+
const fixture = TestBed.createComponent(SelectionListWithPreselectedOption);
728+
const listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
729+
.map(optionDebugEl => optionDebugEl.componentInstance);
730+
731+
fixture.detectChanges();
732+
tick();
733+
734+
expect(listOptions[1].selected).toBe(true);
735+
expect(fixture.componentInstance.selectedOptions).toEqual(['opt2']);
736+
}));
737+
738+
it('should handle preselected option both through the model and the view', fakeAsync(() => {
739+
const fixture = TestBed.createComponent(SelectionListWithPreselectedOptionAndModel);
740+
const listOptions = fixture.debugElement.queryAll(By.directive(MatListOption))
741+
.map(optionDebugEl => optionDebugEl.componentInstance);
742+
743+
fixture.detectChanges();
744+
tick();
745+
746+
expect(listOptions[0].selected).toBe(true);
747+
expect(listOptions[1].selected).toBe(true);
748+
expect(fixture.componentInstance.selectedOptions).toEqual(['opt1', 'opt2']);
749+
}));
750+
751+
});
722752
});
723753

724754

@@ -842,3 +872,27 @@ class SelectionListWithModel {
842872
class SelectionListWithFormControl {
843873
formControl = new FormControl();
844874
}
875+
876+
877+
@Component({
878+
template: `
879+
<mat-selection-list [(ngModel)]="selectedOptions">
880+
<mat-list-option value="opt1">Option 1</mat-list-option>
881+
<mat-list-option value="opt2" selected>Option 2</mat-list-option>
882+
</mat-selection-list>`
883+
})
884+
class SelectionListWithPreselectedOption {
885+
selectedOptions: string[];
886+
}
887+
888+
889+
@Component({
890+
template: `
891+
<mat-selection-list [(ngModel)]="selectedOptions">
892+
<mat-list-option value="opt1">Option 1</mat-list-option>
893+
<mat-list-option value="opt2" selected>Option 2</mat-list-option>
894+
</mat-selection-list>`
895+
})
896+
class SelectionListWithPreselectedOptionAndModel {
897+
selectedOptions = ['opt1'];
898+
}

src/lib/list/selection-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ export class MatListOption extends _MatListOptionMixinBase
163163
}
164164

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

0 commit comments

Comments
 (0)