Skip to content

Commit 4d5c5d5

Browse files
crisbetommalerba
authored andcommittedFeb 4, 2019
fix(radio): not updating DOM node name if group name changes (#14950)
Fixes the `name` of the underlying radio button DOM nodes not being updated, if the name of the parent group changes.
1 parent 2ec9dff commit 4d5c5d5

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎src/lib/radio/radio.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ describe('MatRadio', () => {
409409
}
410410
});
411411

412+
it('should update the name of radio DOM elements if the name of the group changes', () => {
413+
const nodes: HTMLInputElement[] = innerRadios.map(radio => radio.nativeElement);
414+
415+
expect(nodes.every(radio => radio.getAttribute('name') === groupInstance.name))
416+
.toBe(true, 'Expected all radios to have the initial name.');
417+
418+
fixture.componentInstance.groupName = 'changed-name';
419+
fixture.detectChanges();
420+
421+
expect(groupInstance.name).toBe('changed-name');
422+
expect(nodes.every(radio => radio.getAttribute('name') === groupInstance.name))
423+
.toBe(true, 'Expected all radios to have the new name.');
424+
});
425+
412426
it('should check the corresponding radio button on group value change', () => {
413427
expect(groupInstance.value).toBeFalsy();
414428
for (const radio of radioInstances) {
@@ -814,7 +828,7 @@ class StandaloneRadioButtons {
814828

815829
@Component({
816830
template: `
817-
<mat-radio-group [(ngModel)]="modelValue" (change)="lastEvent = $event">
831+
<mat-radio-group [name]="groupName" [(ngModel)]="modelValue" (change)="lastEvent = $event">
818832
<mat-radio-button *ngFor="let option of options" [value]="option.value">
819833
{{option.label}}
820834
</mat-radio-button>
@@ -823,6 +837,7 @@ class StandaloneRadioButtons {
823837
})
824838
class RadioGroupWithNgModel {
825839
modelValue: string;
840+
groupName = 'radio-group';
826841
options = [
827842
{label: 'Vanilla', value: 'vanilla'},
828843
{label: 'Chocolate', value: 'chocolate'},

‎src/lib/radio/radio.ts

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export class MatRadioGroup implements AfterContentInit, ControlValueAccessor {
219219
if (this._radios) {
220220
this._radios.forEach(radio => {
221221
radio.name = this.name;
222+
radio._markForCheck();
222223
});
223224
}
224225
}

0 commit comments

Comments
 (0)
Please sign in to comment.