Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Integrate and customize shadcn/ui components—discovery, installation, theming, and Radix/Base UI best practices
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
examples/form-pattern.tsx
1// Example: Form Pattern with shadcn/ui components2// Demonstrates: Form building, validation, and composition34"use client"56import { Button } from "@/components/ui/button"7import {8Form,9FormControl,10FormDescription,11FormField,12FormItem,13FormLabel,14FormMessage,15} from "@/components/ui/form"16import { Input } from "@/components/ui/input"17import {18Select,19SelectContent,20SelectItem,21SelectTrigger,22SelectValue,23} from "@/components/ui/select"24import { Textarea } from "@/components/ui/textarea"25import { toast } from "@/components/ui/use-toast"26import { zodResolver } from "@hookform/resolvers/zod"27import { useForm } from "react-hook-form"28import * as z from "zod"2930// Define form schema with zod31const formSchema = z.object({32username: z.string().min(2, {33message: "Username must be at least 2 characters.",34}),35email: z.string().email({36message: "Please enter a valid email address.",37}),38role: z.enum(["admin", "user", "viewer"], {39required_error: "Please select a role.",40}),41bio: z.string().max(160, {42message: "Bio must not be longer than 160 characters.",43}).optional(),44})4546type FormValues = z.infer<typeof formSchema>4748export function UserProfileForm() {49// Initialize form with react-hook-form and zod validation50const form = useForm<FormValues>({51resolver: zodResolver(formSchema),52defaultValues: {53username: "",54email: "",55bio: "",56},57})5859// Handle form submission60function onSubmit(values: FormValues) {61// In a real app, send to API62console.log(values)6364toast({65title: "Profile updated",66description: "Your profile has been successfully updated.",67})68}6970return (71<Form {...form}>72<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">73<FormField74control={form.control}75name="username"76render={({ field }) => (77<FormItem>78<FormLabel>Username</FormLabel>79<FormControl>80<Input placeholder="johndoe" {...field} />81</FormControl>82<FormDescription>83This is your public display name.84</FormDescription>85<FormMessage />86</FormItem>87)}88/>8990<FormField91control={form.control}92name="email"93render={({ field }) => (94<FormItem>95<FormLabel>Email</FormLabel>96<FormControl>97<Input type="email" placeholder="[email protected]" {...field} />98</FormControl>99<FormMessage />100</FormItem>101)}102/>103104<FormField105control={form.control}106name="role"107render={({ field }) => (108<FormItem>109<FormLabel>Role</FormLabel>110<Select onValueChange={field.onChange} defaultValue={field.value}>111<FormControl>112<SelectTrigger>113<SelectValue placeholder="Select a role" />114</SelectTrigger>115</FormControl>116<SelectContent>117<SelectItem value="admin">Admin</SelectItem>118<SelectItem value="user">User</SelectItem>119<SelectItem value="viewer">Viewer</SelectItem>120</SelectContent>121</Select>122<FormDescription>123Your role determines your access level.124</FormDescription>125<FormMessage />126</FormItem>127)}128/>129130<FormField131control={form.control}132name="bio"133render={({ field }) => (134<FormItem>135<FormLabel>Bio</FormLabel>136<FormControl>137<Textarea138placeholder="Tell us about yourself"139className="resize-none"140{...field}141/>142</FormControl>143<FormDescription>144Optional. Maximum 160 characters.145</FormDescription>146<FormMessage />147</FormItem>148)}149/>150151<Button type="submit">Update profile</Button>152</form>153</Form>154)155}156157/**158* Key Patterns Demonstrated:159*160* 1. Form Composition: Using shadcn/ui's Form components with react-hook-form161* 2. Validation: Zod schema for type-safe validation162* 3. Error Handling: Automatic error messages via FormMessage163* 4. Accessibility: All fields properly labeled with descriptions164* 5. Type Safety: TypeScript types inferred from Zod schema165*166* Required Dependencies:167* - react-hook-form168* - @hookform/resolvers169* - zod170*171* Installation:172* npx shadcn@latest add form173* npx shadcn@latest add input174* npx shadcn@latest add select175* npx shadcn@latest add textarea176* npx shadcn@latest add button177*/178