Skip to content

Commit

Permalink
fix(radio): clear names from host nodes
Browse files Browse the repository at this point in the history
Along the same lines as angular#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`.
  • Loading branch information
crisbeto committed Jul 1, 2019
1 parent d29df38 commit a6b2616
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/material/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ describe('MatRadio', () => {
}
});

it('should clear the name attribute from the radio group host node', () => {
expect(groupInstance.name).toBeTruthy();
expect(groupDebugElement.nativeElement.getAttribute('name')).toBeFalsy();
});

it('should coerce the disabled binding on the radio group', () => {
(groupInstance as any).disabled = '';
fixture.detectChanges();
Expand Down Expand Up @@ -727,6 +732,10 @@ describe('MatRadio', () => {
expect(seasonRadioInstances.every(radio => radio.color === 'accent')).toBe(true);
});

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

});

describe('with tabindex', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/material/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class MatRadioChange {
host: {
'role': 'radiogroup',
'class': 'mat-radio-group',
'[attr.name]': 'null',
},
})
export class MatRadioGroup implements AfterContentInit, ControlValueAccessor {
Expand Down Expand Up @@ -346,6 +347,7 @@ const _MatRadioButtonMixinBase:
// Needs to be -1 so the `focus` event still fires.
'[attr.tabindex]': '-1',
'[attr.id]': 'id',
'[attr.name]': 'null',
// Note: under normal conditions focus shouldn't land on this element, however it may be
// programmatically set, for example inside of a focus trap, in this case we want to forward
// the focus to the native element.
Expand Down

0 comments on commit a6b2616

Please sign in to comment.