Skip to content

Commit 5c2c31b

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 c2562fb commit 5c2c31b

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();
@@ -733,6 +738,10 @@ describe('MatRadio', () => {
733738
expect(seasonRadioInstances.every(radio => radio.color === 'accent')).toBe(true);
734739
});
735740

741+
it('should clear the name attribute from the radio host node', () => {
742+
expect(radioDebugElements.every(el => !el.nativeElement.getAttribute('name'))).toBe(true);
743+
});
744+
736745
});
737746

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

src/material/radio/radio.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export class MatRadioChange {
9393
host: {
9494
'role': 'radiogroup',
9595
'class': 'mat-radio-group',
96+
'[attr.name]': 'null',
9697
},
9798
})
9899
export class MatRadioGroup implements AfterContentInit, ControlValueAccessor {
@@ -349,6 +350,7 @@ const _MatRadioButtonMixinBase:
349350
'[attr.aria-label]': 'null',
350351
'[attr.aria-labelledby]': 'null',
351352
'[attr.aria-describedby]': 'null',
353+
'[attr.name]': 'null',
352354
// Note: under normal conditions focus shouldn't land on this element, however it may be
353355
// programmatically set, for example inside of a focus trap, in this case we want to forward
354356
// the focus to the native element.

0 commit comments

Comments
 (0)