Form
Building forms with React Hook Form and Zod.
Installation
›pnpm dlx shadcn@latest add @evolphin/form
Usage
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";const formSchema = z.object({
username: z.string().min(2, {
message: "Username must be at least 2 characters.",
}),
});
export function ProfileForm() {
// 1. Define your form.
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
username: "",
},
});
// 2. Define a submit handler.
function onSubmit(values: z.infer<typeof formSchema>) {
// Do something with the form values.
// ✅ This will be type-safe and validated.
console.log(values);
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>
This is your public display name.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
);
}Examples
Default Form
See the Usage section above for a complete example with React Hook Form and Zod validation.
Forms require React Hook Form and Zod setup which cannot be demonstrated in a static preview. Copy the usage example to get started.
API Reference
Form
The Form component is a wrapper around react-hook-form's FormProvider.
FormField
| Prop | Type | Default | Description |
|---|---|---|---|
control | Control | - | The control object from useForm. |
name | string | - | The name of the field. |
render | Function | - | A render prop that receives the field state. |
FormItem
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
FormLabel
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
FormControl
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
FormDescription
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
FormMessage
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |