fix(radio) detect non-direct mat-radio descendants of mat-radio-group (#7842)

Fixes #7166
This commit is contained in:
Rafał Krzewski
2017-11-02 04:48:21 +01:00
committed by Andrew Seguin
parent dff97274f7
commit 90fdacf6ad
2 changed files with 52 additions and 1 deletions
+50
View File
@@ -18,6 +18,8 @@ describe('MatRadio', () => {
RadioGroupWithNgModel,
RadioGroupWithFormControl,
StandaloneRadioButtons,
InterleavedRadioGroup,
TranscludingWrapper
],
providers: [
{provide: ViewportRuler, useClass: FakeViewportRuler}
@@ -666,6 +668,28 @@ describe('MatRadio', () => {
expect(document.activeElement).toBe(inputEl);
});
});
describe('group interspersed with other tags', () => {
let fixture: ComponentFixture<InterleavedRadioGroup>;
let groupDebugElement: DebugElement;
let groupInstance: MatRadioGroup;
let radioDebugElements: DebugElement[];
let radioInstances: MatRadioButton[];
beforeEach(async(() => {
fixture = TestBed.createComponent(InterleavedRadioGroup);
fixture.detectChanges();
groupDebugElement = fixture.debugElement.query(By.directive(MatRadioGroup));
groupInstance = groupDebugElement.injector.get<MatRadioGroup>(MatRadioGroup);
radioDebugElements = fixture.debugElement.queryAll(By.directive(MatRadioButton));
radioInstances = radioDebugElements.map(debugEl => debugEl.componentInstance);
}));
it('should initialize selection of radios based on model value', () => {
expect(groupInstance.selected).toBe(radioInstances[2]);
});
});
});
@@ -760,3 +784,29 @@ class RadioGroupWithFormControl {
template: `<mat-radio-button tabindex="-1"></mat-radio-button>`
})
class FocusableRadioButton {}
@Component({
template: `
<mat-radio-group name="group" [(ngModel)]="modelValue">
<transcluding-wrapper *ngFor="let option of options">
<mat-radio-button [value]="option.value">{{option.label}}</mat-radio-button>
</transcluding-wrapper>
</mat-radio-group>
`
})
class InterleavedRadioGroup {
modelValue = 'strawberry';
options = [
{label: 'Vanilla', value: 'vanilla'},
{label: 'Chocolate', value: 'chocolate'},
{label: 'Strawberry', value: 'strawberry'},
];
}
@Component({
selector: 'transcluding-wrapper',
template: `
<div><ng-content></ng-content></div>
`
})
class TranscludingWrapper {}
+2 -1
View File
@@ -128,7 +128,8 @@ export class MatRadioGroup extends _MatRadioGroupMixinBase
@Output() change: EventEmitter<MatRadioChange> = new EventEmitter<MatRadioChange>();
/** Child radio buttons. */
@ContentChildren(forwardRef(() => MatRadioButton)) _radios: QueryList<MatRadioButton>;
@ContentChildren(forwardRef(() => MatRadioButton), { descendants: true })
_radios: QueryList<MatRadioButton>;
/** Name of the radio button group. All radio buttons inside this group will use this name. */
@Input()