Skip to content
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
35 changes: 35 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got a typo here SelectionListWithSelecTedOption

}

@Component({template: `
<mat-selection-list id = "selection-list-4">
<mat-list-option checkboxPosition = "after" class="test-focus" id="123">
Expand Down
10 changes: 8 additions & 2 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
Inject,
Input,
OnDestroy,
OnInit,
Optional,
Output,
QueryList,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -129,6 +129,12 @@ export class MatListOption extends _MatListOptionMixinBase
super();
}

ngOnInit() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the OnInit interface to the class?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (this.selected) {
this.selectionList.selectedOptions.select(this);
}
}

ngAfterContentInit() {
this._lineSetter = new MatLineSetter(this._lines, this._renderer, this._element);

Expand Down