Skip to content

Commit 95f2180

Browse files
authored
fix(radio-input): do not use htmlFor if no id is passed (#817)
1 parent 0ae4207 commit 95f2180

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/components/inputs/radio-input/radio-group.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Group extends React.PureComponent {
5858
// NOTE: Allowing to intersperse other elements than `Option`.
5959
if (child && child.type.displayName === Option.displayName) {
6060
return React.cloneElement(child, {
61-
id: `${this.props.id}-${index}`,
61+
id: this.props.id && `${this.props.id}-${index}`,
6262
name: this.props.name,
6363
isChecked: this.props.value === child.props.value,
6464
isDisabled: child.props.isDisabled || this.props.isDisabled,

src/components/inputs/radio-input/radio-option.js

+42-39
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,49 @@ const Input = styled.input`
1414
}
1515
`;
1616

17-
const Option = props => (
18-
<label
19-
css={getLabelStyles(props)}
20-
role="radio"
21-
aria-checked={props.isChecked}
22-
onFocus={props.onFocus}
23-
onBlur={props.onBlur}
24-
htmlFor={props.id}
25-
>
26-
<Input
27-
css={accessibleHiddenInputStyles}
28-
id={props.id}
29-
name={props.name}
30-
value={props.value}
31-
onChange={props.isReadOnly ? undefined : props.onChange}
32-
disabled={props.isDisabled}
33-
checked={props.isChecked}
34-
type="radio"
35-
readOnly={props.isReadOnly}
36-
aria-readonly={props.isReadOnly}
37-
{...filterDataAttributes(props)}
38-
/>
39-
<div css={getContainerStyles(props)}>
40-
{props.isChecked ? <Icons.Checked /> : <Icons.Default />}
41-
</div>
42-
<div
43-
css={css`
44-
width: 100%;
45-
margin-left: ${vars.spacingS};
46-
font-size: 1rem;
47-
font-family: ${vars.fontFamilyDefault};
48-
color: ${props.isDisabled
49-
? vars.fontColorForInputWhenDisabled
50-
: vars.fontColorForInput};
51-
`}
17+
const Option = props => {
18+
const labelProps = props.id ? { htmlFor: props.id } : {};
19+
return (
20+
<label
21+
css={getLabelStyles(props)}
22+
role="radio"
23+
aria-checked={props.isChecked}
24+
onFocus={props.onFocus}
25+
onBlur={props.onBlur}
26+
{...labelProps}
5227
>
53-
{props.children}
54-
</div>
55-
</label>
56-
);
28+
<Input
29+
css={accessibleHiddenInputStyles}
30+
id={props.id}
31+
name={props.name}
32+
value={props.value}
33+
onChange={props.isReadOnly ? undefined : props.onChange}
34+
disabled={props.isDisabled}
35+
checked={props.isChecked}
36+
type="radio"
37+
readOnly={props.isReadOnly}
38+
aria-readonly={props.isReadOnly}
39+
{...filterDataAttributes(props)}
40+
/>
41+
<div css={getContainerStyles(props)}>
42+
{props.isChecked ? <Icons.Checked /> : <Icons.Default />}
43+
</div>
44+
<div
45+
css={css`
46+
width: 100%;
47+
margin-left: ${vars.spacingS};
48+
font-size: 1rem;
49+
font-family: ${vars.fontFamilyDefault};
50+
color: ${props.isDisabled
51+
? vars.fontColorForInputWhenDisabled
52+
: vars.fontColorForInput};
53+
`}
54+
>
55+
{props.children}
56+
</div>
57+
</label>
58+
);
59+
};
5760
Option.displayName = 'RadioOption';
5861
Option.propTypes = {
5962
// Direct props

0 commit comments

Comments
 (0)