Skip to content

Commit b93d851

Browse files
committed
Set selected state of list option item in selection list OnInit.
1 parent 244c906 commit b93d851

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,34 @@ describe('MdSelectionList', () => {
214214
});
215215
});
216216

217+
describe('with list option selected', () => {
218+
let fixture: ComponentFixture<SelectionListWithListOptions>;
219+
let listItemEl: DebugElement;
220+
let selectionList: DebugElement;
221+
222+
beforeEach(async(() => {
223+
TestBed.configureTestingModule({
224+
imports: [MdListModule],
225+
declarations: [SelectionListWithSelecedOption],
226+
});
227+
228+
TestBed.compileComponents();
229+
}));
230+
231+
beforeEach(async(() => {
232+
fixture = TestBed.createComponent(SelectionListWithSelecedOption);
233+
listItemEl = fixture.debugElement.query(By.directive(MdListOption));
234+
selectionList = fixture.debugElement.query(By.directive(MdSelectionList));
235+
fixture.detectChanges();
236+
}));
237+
238+
it('should set its initial selected state in the selectedOptions', () => {
239+
let optionEl = listItemEl.injector.get(MdListOption);
240+
let selectedOptions = selectionList.componentInstance.selectedOptions;
241+
expect(selectedOptions.isSelected(optionEl)).toBeTruthy();
242+
});
243+
});
244+
217245
describe('with single option', () => {
218246
let fixture: ComponentFixture<SelectionListWithOnlyOneOption>;
219247
let listOption: DebugElement;
@@ -450,6 +478,13 @@ class SelectionListWithDisabledOption {
450478
disableItem: boolean = false;
451479
}
452480

481+
@Component({template: `
482+
<mat-selection-list>
483+
<md-list-option [selected]="true">Item</md-list-option>
484+
</mat-selection-list>`})
485+
class SelectionListWithSelecedOption {
486+
}
487+
453488
@Component({template: `
454489
<mat-selection-list id = "selection-list-4">
455490
<md-list-option checkboxPosition = "after" class="test-focus" id="123">

src/lib/list/selection-list.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Inject,
2222
Input,
2323
OnDestroy,
24+
OnInit,
2425
Optional,
2526
Output,
2627
QueryList,
@@ -85,7 +86,7 @@ const FOCUSED_STYLE: string = 'mat-list-item-focus';
8586
providers: [{provide: MATERIAL_COMPATIBILITY_MODE, useValue: false}],
8687
})
8788
export class MdListOption extends _MdListOptionMixinBase
88-
implements AfterContentInit, OnDestroy, FocusableOption, CanDisableRipple {
89+
implements AfterContentInit, OnInit, OnDestroy, FocusableOption, CanDisableRipple {
8990

9091
private _lineSetter: MdLineSetter;
9192
private _selected: boolean = false;
@@ -132,6 +133,12 @@ export class MdListOption extends _MdListOptionMixinBase
132133
super();
133134
}
134135

136+
ngOnInit() {
137+
if (this.selected) {
138+
this.selectionList.selectedOptions.select(this);
139+
}
140+
}
141+
135142
ngAfterContentInit() {
136143
this._lineSetter = new MdLineSetter(this._lines, this._renderer, this._element);
137144

0 commit comments

Comments
 (0)