Skip to content

Commit 90fdacf

Browse files
rkrzewskiandrewseguin
authored andcommitted
fix(radio) detect non-direct mat-radio descendants of mat-radio-group (#7842)
Fixes #7166
1 parent dff9727 commit 90fdacf

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/lib/radio/radio.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe('MatRadio', () => {
1818
RadioGroupWithNgModel,
1919
RadioGroupWithFormControl,
2020
StandaloneRadioButtons,
21+
InterleavedRadioGroup,
22+
TranscludingWrapper
2123
],
2224
providers: [
2325
{provide: ViewportRuler, useClass: FakeViewportRuler}
@@ -666,6 +668,28 @@ describe('MatRadio', () => {
666668
expect(document.activeElement).toBe(inputEl);
667669
});
668670
});
671+
672+
describe('group interspersed with other tags', () => {
673+
let fixture: ComponentFixture<InterleavedRadioGroup>;
674+
let groupDebugElement: DebugElement;
675+
let groupInstance: MatRadioGroup;
676+
let radioDebugElements: DebugElement[];
677+
let radioInstances: MatRadioButton[];
678+
679+
beforeEach(async(() => {
680+
fixture = TestBed.createComponent(InterleavedRadioGroup);
681+
fixture.detectChanges();
682+
683+
groupDebugElement = fixture.debugElement.query(By.directive(MatRadioGroup));
684+
groupInstance = groupDebugElement.injector.get<MatRadioGroup>(MatRadioGroup);
685+
radioDebugElements = fixture.debugElement.queryAll(By.directive(MatRadioButton));
686+
radioInstances = radioDebugElements.map(debugEl => debugEl.componentInstance);
687+
}));
688+
689+
it('should initialize selection of radios based on model value', () => {
690+
expect(groupInstance.selected).toBe(radioInstances[2]);
691+
});
692+
});
669693
});
670694

671695

@@ -760,3 +784,29 @@ class RadioGroupWithFormControl {
760784
template: `<mat-radio-button tabindex="-1"></mat-radio-button>`
761785
})
762786
class FocusableRadioButton {}
787+
788+
@Component({
789+
template: `
790+
<mat-radio-group name="group" [(ngModel)]="modelValue">
791+
<transcluding-wrapper *ngFor="let option of options">
792+
<mat-radio-button [value]="option.value">{{option.label}}</mat-radio-button>
793+
</transcluding-wrapper>
794+
</mat-radio-group>
795+
`
796+
})
797+
class InterleavedRadioGroup {
798+
modelValue = 'strawberry';
799+
options = [
800+
{label: 'Vanilla', value: 'vanilla'},
801+
{label: 'Chocolate', value: 'chocolate'},
802+
{label: 'Strawberry', value: 'strawberry'},
803+
];
804+
}
805+
806+
@Component({
807+
selector: 'transcluding-wrapper',
808+
template: `
809+
<div><ng-content></ng-content></div>
810+
`
811+
})
812+
class TranscludingWrapper {}

src/lib/radio/radio.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ export class MatRadioGroup extends _MatRadioGroupMixinBase
128128
@Output() change: EventEmitter<MatRadioChange> = new EventEmitter<MatRadioChange>();
129129

130130
/** Child radio buttons. */
131-
@ContentChildren(forwardRef(() => MatRadioButton)) _radios: QueryList<MatRadioButton>;
131+
@ContentChildren(forwardRef(() => MatRadioButton), { descendants: true })
132+
_radios: QueryList<MatRadioButton>;
132133

133134
/** Name of the radio button group. All radio buttons inside this group will use this name. */
134135
@Input()

0 commit comments

Comments
 (0)