Skip to content

Commit 38fddfe

Browse files
crisbetommalerba
authored andcommitted
fix(select): options inside option group not being rendered when wrapped with ng-container (#9769)
Fixes the `mat-option` instances inside of a `mat-optgroup` not being rendered if they are wrapped inside of a `ng-container`. Fixes #9736.
1 parent be68679 commit 38fddfe

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/lib/core/option/optgroup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<label class="mat-optgroup-label" [id]="_labelId">{{ label }}</label>
2-
<ng-content select="mat-option"></ng-content>
2+
<ng-content select="mat-option, ng-container"></ng-content>

src/lib/select/select.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ describe('MatSelect', () => {
123123
BasicSelect,
124124
MultiSelect,
125125
SelectWithGroups,
126+
SelectWithGroupsAndNgContainer,
126127
]);
127128
}));
128129

@@ -991,6 +992,19 @@ describe('MatSelect', () => {
991992

992993
expect(option.querySelectorAll('.mat-ripple-element').length).toBe(0);
993994
}));
995+
996+
it('should be able to render options inside groups with an ng-container', fakeAsync(() => {
997+
fixture.destroy();
998+
999+
const groupFixture = TestBed.createComponent(SelectWithGroupsAndNgContainer);
1000+
groupFixture.detectChanges();
1001+
trigger = groupFixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
1002+
trigger.click();
1003+
groupFixture.detectChanges();
1004+
1005+
expect(document.querySelectorAll('.cdk-overlay-container mat-option').length)
1006+
.toBeGreaterThan(0, 'Expected at least one option to be rendered.');
1007+
}));
9941008
});
9951009

9961010
describe('selection logic', () => {
@@ -4200,6 +4214,30 @@ class SelectWithGroups {
42004214
@ViewChildren(MatOption) options: QueryList<MatOption>;
42014215
}
42024216

4217+
@Component({
4218+
selector: 'select-with-groups',
4219+
template: `
4220+
<mat-form-field>
4221+
<mat-select placeholder="Pokemon" [formControl]="control">
4222+
<mat-optgroup *ngFor="let group of pokemonTypes" [label]="group.name">
4223+
<ng-container *ngFor="let pokemon of group.pokemon">
4224+
<mat-option [value]="pokemon.value">{{ pokemon.viewValue }}</mat-option>
4225+
</ng-container>
4226+
</mat-optgroup>
4227+
</mat-select>
4228+
</mat-form-field>
4229+
`
4230+
})
4231+
class SelectWithGroupsAndNgContainer {
4232+
control = new FormControl();
4233+
pokemonTypes = [
4234+
{
4235+
name: 'Grass',
4236+
pokemon: [{ value: 'bulbasaur-0', viewValue: 'Bulbasaur' }]
4237+
}
4238+
];
4239+
}
4240+
42034241
@Component({
42044242
template: `
42054243
<form>

0 commit comments

Comments
 (0)