"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
export const title = "Signup Form";
const formSchema = z.object({
fullName: z.string().min(2, {
message: "Name must be at least 2 characters.",
}),
email: z
.email({
error: "Please enter a valid email address.",
})
.min(1, {
error: "Email is required.",
}),
password: z
.string()
.min(8, {
message: "Password must be at least 8 characters.",
})
.regex(/[A-Z]/, "Password must contain at least one uppercase letter")
.regex(/[a-z]/, "Password must contain at least one lowercase letter")
.regex(/[0-9]/, "Password must contain at least one number"),
terms: z.boolean().refine((val) => val === true, {
message: "You must accept the terms and conditions.",
}),
});
const Example = () => {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
fullName: "",
email: "",
password: "",
terms: false,
},
});
function onSubmit(values: z.infer<typeof formSchema>) {
console.log(values);
}
return (
<div className="w-full max-w-sm">
<Form {...form}>
<form className="space-y-4" onSubmit={form.handleSubmit(onSubmit)}>
<div className="space-y-2 text-center">
<h1 className="text-2xl font-bold">Create an account</h1>
<p className="text-sm text-muted-foreground">
Sign up to get started with our platform
</p>
</div>
<FormField
control={form.control}
name="fullName"
render={({ field }) => (
<FormItem>
<FormLabel>Full Name</FormLabel>
<FormControl>
<Input
className="bg-background"
placeholder="John Doe"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
className="bg-background"
placeholder="you@example.com"
type="email"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
className="bg-background"
placeholder="Create a strong password"
type="password"
{...field}
/>
</FormControl>
<FormDescription className="text-xs">
Must contain uppercase, lowercase, and number
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="terms"
render={({ field }) => (
<FormItem className="flex flex-row items-start space-y-0 space-x-3">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
<div className="space-y-1 leading-none">
<FormLabel className="text-sm font-normal">
I agree to the{" "}
<a className="hover:underline" href="#">
terms and conditions
</a>
</FormLabel>
<FormMessage />
</div>
</FormItem>
)}
/>
<Button className="w-full" type="submit">
Create Account
</Button>
<p className="text-center text-sm text-muted-foreground">
Already have an account?{" "}
<a className="hover:underline" href="#">
Sign in
</a>
</p>
</form>
</Form>
</div>
);
};
export default Example;