/** * shadcn/ui Form Component Integration * * Demonstrates: * - shadcn/ui Form component with React Hook Form + Zod * - FormField, FormItem, FormLabel, FormControl, FormMessage components * - Type-safe form with proper error handling * - Accessible form structure * * Installation: * npx shadcn@latest add form input button */ import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import { z } from 'zod' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Checkbox } from '@/components/ui/checkbox' // Define schema const profileFormSchema = z.object({ username: z.string() .min(2, { message: 'Username must be at least 2 characters.' }) .max(30, { message: 'Username must not be longer than 30 characters.' }), email: z.string() .email({ message: 'Please enter a valid email address.' }), bio: z.string() .max(160, { message: 'Bio must not be longer than 160 characters.' }) .optional(), role: z.enum(['admin', 'user', 'guest'], { required_error: 'Please select a role.', }), notifications: z.boolean().default(false).optional(), }) type ProfileFormValues = z.infer export function ShadcnProfileForm() { const form = useForm({ resolver: zodResolver(profileFormSchema), defaultValues: { username: '', email: '', bio: '', notifications: false, }, }) function onSubmit(data: ProfileFormValues) { console.log('Form submitted:', data) // Make API call } return (

Profile Settings

{/* Username Field */} ( Username This is your public display name. )} /> {/* Email Field */} ( Email We'll never share your email with anyone. )} /> {/* Bio Field (Textarea) */} ( Bio