Files
2024-07-29 23:10:38 +08:00

77 lines
1.9 KiB
TypeScript

"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
const FormSchema = z.object({
email: z.string().email({
message: "Enter a valid email.",
}),
});
export function NewsletterForm() {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
email: "",
},
});
function onSubmit(data: z.infer<typeof FormSchema>) {
// form.reset();
toast.warning(`Work in progress...`);
// toast({
// title: "You submitted the following values:",
// description: (
// <pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
// <code className="text-white">{JSON.stringify(data, null, 2)}</code>
// </pre>
// ),
// });
}
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="w-full space-y-2 sm:max-w-sm"
>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Subscribe to our newsletter</FormLabel>
<FormControl>
<Input
type="email"
className="rounded-lg px-4"
placeholder="oiov@example.com"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" size="sm" rounded="lg" className="px-4">
Subscribe
</Button>
</form>
</Form>
);
}