Skip to content

Commit f7e96d2

Browse files
committed
fix(radio): clear names from host nodes
Along the same lines as #15368. Clears the `name` from the host node of the radio button and radio group, because they end up being forwarded to the underlying `input` and can cause double results when using something like `getElementsByName`.
1 parent c05a07e commit f7e96d2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/material/radio/radio.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ describe('MatRadio', () => {
6666
}
6767
});
6868

69+
it('should clear the name attribute from the radio group host node', () => {
70+
expect(groupInstance.name).toBeTruthy();
71+
expect(groupDebugElement.nativeElement.getAttribute('name')).toBeFalsy();
72+
});
73+
6974
it('should coerce the disabled binding on the radio group', () => {
7075
(groupInstance as any).disabled = '';
7176
fixture.detectChanges();
@@ -741,6 +746,10 @@ describe('MatRadio', () => {
741746
expect(seasonRadioInstances.every(radio => radio.color === 'accent')).toBe(true);
742747
});
743748

749+
it('should clear the name attribute from the radio host node', () => {
750+
expect(radioDebugElements.every(el => !el.nativeElement.getAttribute('name'))).toBe(true);
751+
});
752+
744753
});
745754

746755
describe('with tabindex', () => {

src/material/radio/radio.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ export abstract class _MatRadioGroupBase<T extends _MatRadioButtonBase> implemen
315315
host: {
316316
'role': 'radiogroup',
317317
'class': 'mat-radio-group',
318+
'[attr.name]': 'null',
318319
},
319320
})
320321
export class MatRadioGroup extends _MatRadioGroupBase<MatRadioButton> {
@@ -618,6 +619,7 @@ export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase imple
618619
'[attr.aria-label]': 'null',
619620
'[attr.aria-labelledby]': 'null',
620621
'[attr.aria-describedby]': 'null',
622+
'[attr.name]': 'null',
621623
// Note: under normal conditions focus shouldn't land on this element, however it may be
622624
// programmatically set, for example inside of a focus trap, in this case we want to forward
623625
// the focus to the native element.

0 commit comments

Comments
 (0)