Skip to content

Commit

Permalink
Set selected state of list option item in selection list OnInit. (#7189)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott authored and andrewseguin committed Sep 29, 2017
1 parent 217840c commit f19f19c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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 {
}

@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() {
if (this.selected) {
this.selectionList.selectedOptions.select(this);
}
}

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

Expand Down

0 comments on commit f19f19c

Please sign in to comment.