Set selected state of list option item in selection list OnInit. (#7189)

This commit is contained in:
Joey Perrott
2017-09-29 13:08:09 -07:00
committed by Andrew Seguin
parent 217840c5b9
commit f19f19cf63
2 changed files with 43 additions and 2 deletions
+35
View File
@@ -219,6 +219,34 @@ describe('MatSelectionList', () => {
});
});
describe('with list option selected', () => {
let fixture: ComponentFixture<SelectionListWithListOptions>;
let listItemEl: DebugElement;
let selectionList: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdListModule],
declarations: [SelectionListWithSelecedOption],
});
TestBed.compileComponents();
}));
beforeEach(async(() => {
fixture = TestBed.createComponent(SelectionListWithSelecedOption);
listItemEl = fixture.debugElement.query(By.directive(MdListOption));
selectionList = fixture.debugElement.query(By.directive(MdSelectionList));
fixture.detectChanges();
}));
it('should set its initial selected state in the selectedOptions', () => {
let optionEl = listItemEl.injector.get(MdListOption);
let selectedOptions = selectionList.componentInstance.selectedOptions;
expect(selectedOptions.isSelected(optionEl)).toBeTruthy();
});
});
describe('with single option', () => {
let fixture: ComponentFixture<SelectionListWithOnlyOneOption>;
let listOption: DebugElement;
@@ -456,6 +484,13 @@ class SelectionListWithDisabledOption {
disableItem: boolean = false;
}
@Component({template: `
<mat-selection-list>
<md-list-option [selected]="true">Item</md-list-option>
</mat-selection-list>`})
class SelectionListWithSelecedOption {
}
@Component({template: `
<mat-selection-list id = "selection-list-4">
<mat-list-option checkboxPosition = "after" class="test-focus" id="123">
+8 -2
View File
@@ -23,6 +23,7 @@ import {
Inject,
Input,
OnDestroy,
OnInit,
Optional,
Output,
QueryList,
@@ -82,8 +83,7 @@ const FOCUSED_STYLE: string = 'mat-list-item-focus';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatListOption extends _MatListOptionMixinBase
implements AfterContentInit, OnDestroy, FocusableOption, CanDisableRipple {
implements AfterContentInit, OnInit, OnDestroy, FocusableOption, CanDisableRipple {
private _lineSetter: MatLineSetter;
private _selected: boolean = false;
private _disabled: boolean = false;
@@ -129,6 +129,12 @@ export class MatListOption extends _MatListOptionMixinBase
super();
}
ngOnInit() {
if (this.selected) {
this.selectionList.selectedOptions.select(this);
}
}
ngAfterContentInit() {
this._lineSetter = new MatLineSetter(this._lines, this._renderer, this._element);