Skip to content

Commit a20f6a3

Browse files
Added ref support in Radio.Item
1 parent 87678a7 commit a20f6a3

2 files changed

Lines changed: 51 additions & 38 deletions

File tree

src/components/Radio/Item.jsx

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { forwardRef } from "react";
22

33
import classnames from "classnames";
44
import PropTypes from "prop-types";
@@ -7,37 +7,42 @@ import Label from "components/Label";
77
import { useId } from "hooks";
88
import { hyphenize } from "utils";
99

10-
const Item = ({
11-
name = "",
12-
label = "",
13-
className = "",
14-
labelProps,
15-
dataCy = "",
16-
...otherProps
17-
}) => {
18-
const id = useId(otherProps.id);
10+
const Item = forwardRef(
11+
(
12+
{
13+
name = "",
14+
label = "",
15+
className = "",
16+
labelProps,
17+
dataCy = "",
18+
...otherProps
19+
},
20+
ref
21+
) => {
22+
const id = useId(otherProps.id);
1923

20-
return (
21-
<div className={classnames(["neeto-ui-radio__item", className])}>
22-
<input
23-
{...{ id, name }}
24-
className="neeto-ui-radio"
25-
data-cy={dataCy || `${hyphenize(label)}-radio-input`}
26-
type="radio"
27-
{...otherProps}
28-
/>
29-
{label && (
30-
<Label
31-
data-cy={dataCy || `${hyphenize(label)}-radio-label`}
32-
htmlFor={id}
33-
{...labelProps}
34-
>
35-
{label}
36-
</Label>
37-
)}
38-
</div>
39-
);
40-
};
24+
return (
25+
<div className={classnames(["neeto-ui-radio__item", className])}>
26+
<input
27+
{...{ id, name, ref }}
28+
className="neeto-ui-radio"
29+
data-cy={dataCy || `${hyphenize(label)}-radio-input`}
30+
type="radio"
31+
{...otherProps}
32+
/>
33+
{label && (
34+
<Label
35+
data-cy={dataCy || `${hyphenize(label)}-radio-label`}
36+
htmlFor={id}
37+
{...labelProps}
38+
>
39+
{label}
40+
</Label>
41+
)}
42+
</div>
43+
);
44+
}
45+
);
4146

4247
Item.displayName = "Radio.Item";
4348

stories/Components/Radio.stories.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useRef, useState } from "react";
22

33
import { Formik, Form } from "formik";
44

@@ -24,12 +24,20 @@ const metadata = {
2424
},
2525
};
2626

27-
const Options = args => (
28-
<Radio {...args}>
29-
<Radio.Item label="Option 1" name="options" value="Option1" />
30-
<Radio.Item label="Option 2" name="options" value="Option2" />
31-
</Radio>
32-
);
27+
const Options = args => {
28+
const ref = useRef(null);
29+
30+
useEffect(() => {
31+
ref.current?.focus();
32+
}, [ref.current]);
33+
34+
return (
35+
<Radio {...{ ...args, ref }}>
36+
<Radio.Item label="Option 1" name="options" value="Option1" />
37+
<Radio.Item label="Option 2" name="options" value="Option2" />
38+
</Radio>
39+
);
40+
};
3341

3442
Options.args = { label: "Radio options" };
3543

0 commit comments

Comments
 (0)