registry/patterns/select/select-12.tsx
'use client'
import { zodResolver } from '@hookform/resolvers/zod'
import { Controller, useForm } from 'react-hook-form'
import { toast } from 'sonner'
import * as z from 'zod'
import { Button } from '@/components/ui/button'
import {
Field,
FieldError,
FieldGroup,
FieldLabel,
} from '@/components/ui/field'
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
const departments = [
{ value: 'design', label: 'Design' },
{ value: 'engineering', label: 'Engineering' },
{ value: 'marketing', label: 'Marketing' },
{ value: 'support', label: 'Support' },
] as const
const formSchema = z.object({
department: z.string().min(1, 'Select a department.'),
})
type FormValues = z.infer<typeof formSchema>
export function Select12() {
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
department: '',
},
})
function onSubmit(data: FormValues) {
const label = departments.find(
(item) => item.value === data.department,
)?.label
toast.success('Submitted', {
description: label ?? data.department,
})
}
return (
<form
id="select-rhf-form"
onSubmit={form.handleSubmit(onSubmit)}
className="flex w-full max-w-sm flex-col gap-4"
>
<FieldGroup>
<Controller
name="department"
control={form.control}
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor="select-rhf-department">
Department
</FieldLabel>
<Select value={field.value} onValueChange={field.onChange}>
<SelectTrigger
id="select-rhf-department"
className="w-full"
aria-invalid={fieldState.invalid}
>
<SelectValue placeholder="Select department" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{departments.map((item) => (
<SelectItem key={item.value} value={item.value}>
{item.label}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
{fieldState.invalid && <FieldError errors={[fieldState.error]} />}
</Field>
)}
/>
</FieldGroup>
<Button type="submit" size="sm">
Submit
</Button>
</form>
)
}





























