'use client'
import { CheckCheckIcon } from 'lucide-react'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import { z } from 'zod'
import { Alert, AlertTitle } from '@/registry/new-york/ui/alert'
import { Button } from '@/registry/new-york/ui/button'
import { Checkbox } from '@/registry/new-york/ui/checkbox'
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage
} from '@/registry/new-york/ui/form'
const CheckboxFormDemo = () => {
const FormSchema = z.object({
acceptTerms: z.boolean().refine(val => val === true, {
message: 'You must accept the terms and conditions.'
})
})
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: { acceptTerms: false }
})
function onSubmit() {
toast.custom(() => (
<Alert className='border-green-600 text-green-600 dark:border-green-400 dark:text-green-400'>
<CheckCheckIcon />
<AlertTitle>Welcome to the community!</AlertTitle>
</Alert>
))
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className='w-full max-w-xs space-y-6'>
<FormField
control={form.control}
name='acceptTerms'
render={({ field }) => (
<FormItem>
<div className='flex items-center space-x-2'>
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<FormLabel>Agree to Join the Community</FormLabel>
</div>
<FormDescription>
By clicking 'Join Now', you accept our Community Guidelines and Privacy Policy. We're
excited to have you on board!
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type='submit'>Join Now</Button>
</form>
</Form>
)
}
export default CheckboxFormDemo