Skip to content

Commit fe0f0e8

Browse files
committed
fix(material/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 5ab25d0 commit fe0f0e8

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

src/material/legacy-radio/radio.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ describe('MatRadio', () => {
6868
}
6969
});
7070

71+
it('should clear the name attribute from the radio group host node', () => {
72+
expect(groupInstance.name).toBeTruthy();
73+
expect(groupDebugElement.nativeElement.getAttribute('name')).toBeFalsy();
74+
});
75+
7176
it('should coerce the disabled binding on the radio group', () => {
7277
(groupInstance as any).disabled = '';
7378
fixture.detectChanges();

src/material/legacy-radio/radio.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class MatLegacyRadioGroup extends _MatRadioGroupBase<MatLegacyRadioButton
8787
'[attr.aria-label]': 'null',
8888
'[attr.aria-labelledby]': 'null',
8989
'[attr.aria-describedby]': 'null',
90+
'[attr.name]': 'null',
9091
// Note: under normal conditions focus shouldn't land on this element, however it may be
9192
// programmatically set, for example inside of a focus trap, in this case we want to forward
9293
// the focus to the native element.

src/material/radio/radio.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ describe('MDC-based MatRadio', () => {
7676
}
7777
});
7878

79+
it('should clear the name attribute from the radio group host node', () => {
80+
expect(groupInstance.name).toBeTruthy();
81+
expect(groupDebugElement.nativeElement.getAttribute('name')).toBeFalsy();
82+
});
83+
7984
it('should coerce the disabled binding on the radio group', () => {
8085
(groupInstance as any).disabled = '';
8186
fixture.detectChanges();
@@ -821,6 +826,10 @@ describe('MDC-based MatRadio', () => {
821826
it('should default the radio color to `accent`', () => {
822827
expect(seasonRadioInstances.every(radio => radio.color === 'accent')).toBe(true);
823828
});
829+
830+
it('should clear the name attribute from the radio host node', () => {
831+
expect(radioDebugElements.every(el => !el.nativeElement.getAttribute('name'))).toBe(true);
832+
});
824833
});
825834

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

src/material/radio/radio.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ export class MatRadioGroup extends _MatRadioGroupBase<MatRadioButton> {
685685
'[attr.aria-label]': 'null',
686686
'[attr.aria-labelledby]': 'null',
687687
'[attr.aria-describedby]': 'null',
688+
'[attr.name]': 'null',
688689
// Note: under normal conditions focus shouldn't land on this element, however it may be
689690
// programmatically set, for example inside of a focus trap, in this case we want to forward
690691
// the focus to the native element.

0 commit comments

Comments
 (0)