Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onValueChange function assignment on RadioGroup gives type error when using Form and zodResolver #735

Closed
ImPrankster opened this issue Jun 27, 2023 · 10 comments
Labels

Comments

@ImPrankster
Copy link

I'm using the Form+RHF component with zodResolver in this typescript project, and uses the way shown in the example to use a enum on the formSchema and a RadioGroup. But when trying to assign the form's onChange function to the RadioGroups' onValueChange it gives a type error.

const formSchema = z.object({
  age: z.enum(["teenager", "adult"]),
  humourous: z.boolean(),
  dataRich: z.boolean(),
  imaginative: z.boolean(),
});

const UserSettingsFrom = (props: z.infer<typeof formSchema>) => {
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: props,
  });

  <Form {...form}>
    <form className="space-y-8">
      <FormField
        control={form.control}
        name="age"
        render={({ field }) => (
          <FormItem>
            <FormLabel>I&apos;M</FormLabel>
            <FormControl>
              <RadioGroup
                onValueChange={field.onChange}
                defaultValue={field.value}
                className="space-x-2y flex"
              >
                <FormItem className="flex items-center space-x-2">
                  <FormControl>
                    <RadioGroupItem value="teenager" />
                  </FormControl>
                  <FormLabel htmlFor="r1" className="text-lg">
                    Teenager
                  </FormLabel>
                </FormItem>
                <FormItem className="flex items-center space-x-2">
                  <FormControl>
                    <RadioGroupItem value="adult" />
                  </FormControl>
                  <FormLabel htmlFor="r1" className="text-lg">
                    Adult
                  </FormLabel>
                </FormItem>
              </RadioGroup>
            </FormControl>
            <FormMessage />
          </FormItem>
        )}
      />
    </form>
  </Form>;
};

The error:

Type '(event: "teenager" | "adult" | ChangeEvent<Element>) => void' is not assignable to type '(value: string) => void'.
  Types of parameters 'event' and 'value' are incompatible.
    Type 'string' is not assignable to type '"teenager" | "adult" | ChangeEvent<Element>'.ts(2322)
@marcelblijleven
Copy link

Running into similar errors, it seems a recent release in react-hook-form caused this (react-hook-form/react-hook-form#10342). Reverting react-hook-form to 7.44.3 helps for now

@dan5py
Copy link
Contributor

dan5py commented Jun 29, 2023

As said by @marcelblijleven, it's a problem related to react-hook-form v7.45.0, which implemented a stricter type for the onChange callback. Version 7.45.1 reverted this feature so it should work normally now, also in the latest release.

@TinsFox
Copy link

TinsFox commented Jul 8, 2023

As said by @marcelblijleven, it's a problem related to react-hook-form v7.45.0, which implemented a stricter type for the onChange callback. Version 7.45.1 reverted this feature so it should work normally now, also in the latest release.

Hey, @dan5py
version 7.45.1 does not solve the problem

I just rolled back from version 7.45.1 to version 7.44.2

image

The latest product type is as follows

image

Is there any other solution?

@rafalzawadzki
Copy link

rafalzawadzki commented Jul 23, 2023

As said by @marcelblijleven, it's a problem related to react-hook-form v7.45.0, which implemented a stricter type for the onChange callback. Version 7.45.1 reverted this feature so it should work normally now, also in the latest release.

Hey, @dan5py version 7.45.1 does not solve the problem

I just rolled back from version 7.45.1 to version 7.44.2

Is there any other solution?

Update to 7.45.2

@tielushko
Copy link

Can confirm the issue is resolved with 7.45.2

@OsamaQureshi147
Copy link

I'm still getting the same TS error after upgrading to 7.46.1. Tried 7.45.2 also!

@junaid-1013
Copy link

junaid-1013 commented Oct 1, 2023

this is my radio group I dont want to use it with form as I have many more components
what is the problem in my code it is not working as I wanted

const [selectedJobType, setSelectedJobType] = useState("Fertilizing");

const handleRadioChange = (event) => {
setSelectedJobType(event.target.value);
};

          <RadioGroup
            value={selectedJobType}
            onChange={handleRadioChange}
            defaultValue="Fertilizing"
          >
            <div className="flex items-center space-x-2">
              <RadioGroupItem value="Fertilizing" id="Fertilizing" />
              <Label htmlFor="Fertilizing">Fertilizing</Label>
            </div>
            <div className="flex items-center space-x-2">
              <RadioGroupItem value="Spraying" id="Spraying" />
              <Label htmlFor="Spraying">Spraying</Label>
            </div>
          </RadioGroup>

@coded58
Copy link

coded58 commented Dec 17, 2023

has this been resolved?? getting same error

@fimhoff
Copy link

fimhoff commented May 24, 2024

Replace:
<RadioGroup value={selectedJobType} onChange={handleRadioChange} defaultValue="Fertilizing" >
by:
<RadioGroup value={selectedJobType} onValueChange={handleRadioChange} defaultValue="Fertilizing" >

And

const handleRadioChange = (event) => {
    // setSelectedJobType(event.target.value);
    setSelectedJobType(event);
};

@shadcn shadcn added the Stale label Jun 14, 2024
@shadcn
Copy link
Collaborator

shadcn commented Jun 21, 2024

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

@shadcn shadcn closed this as completed Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests