Using Move action on fieldArray Radio input #11572
-
I am trying to move values from one position to another with the built in function with fieldArrays but the radio field seems to not switch properly. Using my codesandbox below, when I create just two array entries, labeling the first with all "1" type values and the second with "2" values, then attempt to click the "move" button, the other fields swap as expected, but the radio field will only swap the second value and the first value is not seen, as is "null" it seems. What am I missing here? https://codesandbox.io/p/sandbox/react-hook-form-usefieldarray-forked-vmddlh?file=%2Fsrc%2Findex.tsx |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Currently, the radio input is not controlled. ![]() What the document recommends in the
Eventually, you may need to create the controlled radio / radio group component with e.g) codesandbox const RadioButtonGroup = ({
control,
index: number,
}: {
control: UseForm["control"];
index: number;
}) => {
const { field } = useController({
control,
name: `test.${number}.radioEx`,
rules: {
required: true,
},
});
const { value, name, ...methods } = field;
return (
<div>
<p style={{ display: "inline", margin: "0" }}>r-op 1</p>
<input
type="radio"
{...methods}
value="r-op 1"
checked={value === "r-op 1"}
/>
<p style={{ display: "inline", margin: "0" }}>r-op 2</p>
<input
type="radio"
{...methods}
value="r-op 2"
checked={value === "r-op 2"}
/>
</div>
);
}; |
Beta Was this translation helpful? Give feedback.
-
From the
|
Beta Was this translation helpful? Give feedback.
-
Thanks y'all, this has helped me understand this usage a bit more! |
Beta Was this translation helpful? Give feedback.
Currently, the radio input is not controlled.
What the document recommends in the
useFieldArray()
:Eventually, you may need to create the controlled radio / radio group component with
useController
orController
.e.g) codesandbox