Select Field
A select form field designed to be used with react-hook-form and Zod for validation. It combines a label, select dropdown, error handling, and optional description into a single reusable component.
Installation
npx shadcn@latest add https://shuip.xyz/r/select-field.json
Preview
Less verbose code in forms
<FormFieldcontrol={form.control}name="country"render={({ field }) => (<FormItem><FormLabel>Country</FormLabel><Select onValueChange={field.onChange} defaultValue={field.value}><FormControl><SelectTrigger><SelectValue placeholder="Select a country" /></SelectTrigger></FormControl><SelectContent><SelectItem value="us">United States</SelectItem><SelectItem value="ca">Canada</SelectItem><SelectItem value="fr">France</SelectItem></SelectContent></Select><FormDescription>Choose your country</FormDescription><FormMessage /></FormItem>)}/>
With SelectField
, the same result in a single line:
<SelectFieldregister={form.register('country')}label="Country"placeholder="Select a country"description="Choose your country"options={{ 'United States': 'us', 'Canada': 'ca', 'France': 'fr' }}/>
react-hook-form integration
The component is specifically designed for react-hook-form:
const form = useForm({defaultValues: { category: '' },resolver: zodResolver(schema),});const options = {'Technology': 'tech','Design': 'design','Business': 'business',};// Direct usage with form register<SelectFieldregister={form.register('category')}label="Category"options={options}/>
Examples
Default
Props
Name | Type | Description |
---|---|---|
register | UseFormRegisterReturn | react-hook-form register function result |
options | Record<string, string> | Object where keys are labels and values are form values |
label | string? | Label displayed above the field |
placeholder | string? | Placeholder text in the select |
description | string? | Optional description below the field |
defaultValue | TFieldValues[FieldPath<TFieldValues>]? | Default selected value |
...props | SelectProps | All Radix Select props |