Form 2

PreviousNext

Form with checkbox controls and alert integration for user selections

Docs
shadcn-studiocomponent

Preview

Loading preview…
registry/new-york/components/form/form-02.tsx
'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 &apos;Join Now&apos;, you accept our Community Guidelines and Privacy Policy. We&apos;re
                excited to have you on board!
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />

        <Button type='submit'>Join Now</Button>
      </form>
    </Form>
  )
}

export default CheckboxFormDemo

Installation

npx shadcn@latest add @shadcn-studio/form-02

Usage

import { Form02 } from "@/components/form-02"
<Form02 />