Radio Field
A radio button group designed to be used with react-hook-form and Zod for validation. It combines a label, radio button options, error handling, and optional description into a single reusable component.
Installation
npx shadcn@latest add https://shuip.xyz/r/radio-field.json
Preview
Less verbose code in forms
<FormFieldcontrol={form.control}name="theme"render={({ field }) => (<FormItem className="space-y-3"><FormLabel>Select a theme</FormLabel><FormControl><RadioGrouponValueChange={field.onChange}defaultValue={field.value}className="flex flex-col space-y-1"><FormItem className="flex items-center space-x-3 space-y-0"><FormControl><RadioGroupItem value="light" /></FormControl><FormLabel className="font-normal">Light</FormLabel></FormItem><FormItem className="flex items-center space-x-3 space-y-0"><FormControl><RadioGroupItem value="dark" /></FormControl><FormLabel className="font-normal">Dark</FormLabel></FormItem><FormItem className="flex items-center space-x-3 space-y-0"><FormControl><RadioGroupItem value="system" /></FormControl><FormLabel className="font-normal">System</FormLabel></FormItem></RadioGroup></FormControl><FormMessage /></FormItem>)}/>
With RadioField
, the same result in a single line:
<RadioFieldregister={form.register('theme')}label="Select a theme"options={['light', 'dark', 'system' ]}/>
Built-in features
- Automatic radio rendering: Pass an array of options and they're rendered as radio buttons
- Clean layout: Pre-configured spacing and alignment for radio groups
- Zod validation: Native integration with react-hook-form and Zod
- Responsive design: Optimized for both desktop and mobile
- Error display: Automatic error message display
react-hook-form integration
The component is specifically designed for react-hook-form:
const form = useForm({defaultValues: { plan: 'basic' },resolver: zodResolver(schema),});<RadioFieldregister={form.register('plan')}label="Choose your plan"options={{ Basic: 'basic', Pro: 'pro', Enterprise: 'enterprise' }}/>
Examples
Default
Props
Name | Type | Description |
---|---|---|
register | UseFormRegisterReturn | react-hook-form register function result |
options | string[] | Array of options to render as radio buttons |
label | string? | Label displayed above the radio group |
description | string? | Optional description below the field |
...props | RadioGroupProps | All shadcn/ui RadioGroup props |