Skip to content

Commit 6a07d0d

Browse files
crisbetommalerba
authored andcommittedFeb 4, 2019
fix(list): selection list not picking up indirect descendants (#15003)
Fixes `MatSelectionList` not picking up list options that aren't direct descendants. **Note:** this approach can have some side-effects if somebody decided to nest selection lists which would result in the parent picking up the options of its children. We can handle that case with some extra code, however I decided not to for now since it doesn't make sense to nest lists. Fixes #15000.
1 parent 5ad5176 commit 6a07d0d

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
 

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ describe('MatSelectionList without forms', () => {
3636
SelectionListWithListOptions,
3737
SelectionListWithCheckboxPositionAfter,
3838
SelectionListWithListDisabled,
39-
SelectionListWithOnlyOneOption
39+
SelectionListWithOnlyOneOption,
40+
SelectionListWithIndirectChildOptions,
4041
],
4142
});
4243

@@ -505,6 +506,21 @@ describe('MatSelectionList without forms', () => {
505506
expect(selectionList.nativeElement.getAttribute('aria-multiselectable')).toBe('true');
506507
});
507508

509+
it('should be able to reach list options that are indirect descendants', () => {
510+
const descendatsFixture = TestBed.createComponent(SelectionListWithIndirectChildOptions);
511+
descendatsFixture.detectChanges();
512+
listOptions = descendatsFixture.debugElement.queryAll(By.directive(MatListOption));
513+
selectionList = descendatsFixture.debugElement.query(By.directive(MatSelectionList));
514+
const list: MatSelectionList = selectionList.componentInstance;
515+
516+
expect(list.options.toArray().every(option => option.selected)).toBe(false);
517+
518+
list.selectAll();
519+
descendatsFixture.detectChanges();
520+
521+
expect(list.options.toArray().every(option => option.selected)).toBe(true);
522+
});
523+
508524
});
509525

510526
describe('with list option selected', () => {
@@ -1280,3 +1296,18 @@ class SelectionListWithAvatar {
12801296
})
12811297
class SelectionListWithIcon {
12821298
}
1299+
1300+
1301+
@Component({
1302+
// Note the blank `ngSwitch` which we need in order to hit the bug that we're testing.
1303+
template: `
1304+
<mat-selection-list>
1305+
<ng-container [ngSwitch]="true">
1306+
<mat-list-option [value]="1">One</mat-list-option>
1307+
<mat-list-option [value]="2">Two</mat-list-option>
1308+
</ng-container>
1309+
</mat-selection-list>`
1310+
})
1311+
class SelectionListWithIndirectChildOptions {
1312+
@ViewChildren(MatListOption) optionInstances: QueryList<MatListOption>;
1313+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
305305
_keyManager: FocusKeyManager<MatListOption>;
306306

307307
/** The option components contained within this selection-list. */
308-
@ContentChildren(MatListOption) options: QueryList<MatListOption>;
308+
@ContentChildren(MatListOption, {descendants: true}) options: QueryList<MatListOption>;
309309

310310
/** Emits a change event whenever the selected state of an option changes. */
311311
@Output() readonly selectionChange: EventEmitter<MatSelectionListChange> =

0 commit comments

Comments
 (0)
Please sign in to comment.