RadioGroupField
A group of radio buttons integrated with React Hook Form.
RadioGroupField renders a labeled set of radio options with consistent spacing and error display.
Preview
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm, FormProvider } from "react-hook-form";
import { z } from "zod";
import RadioGroupField from "@/components/ui/RadioGroupField";
export const schema = z.object({
plan: z.string().min(1, { message: "Please select a plan." }),
});
export function RadioGroupFieldBasicExample() {
const methods = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
defaultValues: { plan: "" },
});
const onSubmit = (data: z.infer<typeof schema>) => {
console.log("Form data:", data);
alert("Check the console for the form data!");
};
return (
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)} className="space-y-4 w-80">
<RadioGroupField
name="plan"
label="Choose a plan"
options={[
{ label: "Basic", value: "basic" },
{ label: "Pro", value: "pro" },
{ label: "Enterprise", value: "enterprise" },
]}
/>
<button type="submit" className="form-button form-button-primary w-full">Submit</button>
</form>
</FormProvider>
);
}
Props
| Name | Type | Default | Description |
|---|---|---|---|
| name* | string | - | Form field name. |
| label* | string | - | Label shown above the group. |
| options | Array<{ label: string; value: string }> | - | Radio options to render. |
| className | string | - | Additional CSS classes. |