nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - RadioGroup doesn't work with RHF

Open UltraWelfare opened this issue 10 months ago • 6 comments

HeroUI Version

2.7.2

Describe the bug

When using a RadioGroup with Radio components, toggling to other radio options sets the value on the RHF form to null

Your Example Website or App

Code Sandbox

Steps to Reproduce the Bug or Issue

type Input = {
  type: string,
};

export default function IndexPage() {
  const { register, watch } = useForm<Input>();

  const values = watch();

  console.log(values);

  return (
    <DefaultLayout>
      <Form>
        <RadioGroup label="Type" {...register("type")}>
          <Radio value="customer">Customer</Radio>
          <Radio value="employee">Employee</Radio>
        </RadioGroup>
      </Form>
    </DefaultLayout>
  );
}

Toggling to customer, will console.log { type: "customer" } but toggling to employee will console.log { type: null }

Image

Expected behavior

The value should be set instead of null

Screenshots or Videos

No response

Operating System Version

Windows

Browser

Chrome

UltraWelfare avatar Feb 24 '25 11:02 UltraWelfare

Wrapping it in a Controller fixes it as a workaround

<Controller
  control={control}
  name={"type"}
  render={({ field: { onChange, onBlur, value } }) => (
    <RadioGroup
      label="Type"
      value={value}
      onBlur={onBlur}
      onChange={onChange}
    >
      <Radio value="customer">Customer</Radio>
      <Radio value="employee">Employee</Radio>
    </RadioGroup>
  )}
/>

UltraWelfare avatar Feb 24 '25 11:02 UltraWelfare

which RHF version are you using? To speed up the process, you may share a sandbox instead so that we could look into it faster.

wingkwong avatar Feb 24 '25 11:02 wingkwong

which RHF version are you using? To speed up the process, you may share a sandbox instead so that we could look into it faster.

The latest one 7.54.2. I edited my initial post to include the sandbox

UltraWelfare avatar Feb 24 '25 12:02 UltraWelfare

I'd like to work on this issue and submit a fix for the RHF integration problem. Could you please assign this to me? @wingkwong @UltraWelfare

ariansj01 avatar Oct 17 '25 09:10 ariansj01

Hi! I’ve opened PR #5821 to address this issue.

  • Problem: Using RHF register("type") directly on RadioGroup could set the value to null when toggling.
  • Fix: When onChange is provided on RadioGroup, emit a form-like event { target: { name, value } } alongside onValueChange(value) so RHF receives correct updates.
  • Compatibility: No breaking changes; the controlled API (value/onValueChange) remains unchanged.

Thanks!

ariansj01 avatar Oct 17 '25 11:10 ariansj01