85 lines
1.8 KiB
TypeScript
85 lines
1.8 KiB
TypeScript
|
"use client"
|
||
|
|
||
|
import { z } from "zod"
|
||
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||
|
import { useForm } from "react-hook-form"
|
||
|
import { Genre } from "@prisma/client"
|
||
|
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({
|
||
|
title: z.string().min(2).max(50),
|
||
|
word_count: z.number(),
|
||
|
// genres: z.array()
|
||
|
})
|
||
|
|
||
|
export default function FancyForm() {
|
||
|
// 1. Define your form.
|
||
|
const form = useForm<z.infer<typeof formSchema>>({
|
||
|
resolver: zodResolver(formSchema),
|
||
|
defaultValues: {
|
||
|
title: "",
|
||
|
word_count: 500,
|
||
|
},
|
||
|
})
|
||
|
|
||
|
// 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="title"
|
||
|
render={({ field }) => (
|
||
|
<FormItem>
|
||
|
<FormLabel>Title</FormLabel>
|
||
|
<FormControl>
|
||
|
<Input placeholder="title goes here..." {...field} />
|
||
|
</FormControl>
|
||
|
<FormMessage />
|
||
|
</FormItem>
|
||
|
)}
|
||
|
/>
|
||
|
<FormField
|
||
|
control={form.control}
|
||
|
name="word_count"
|
||
|
render={({ field }) => (
|
||
|
<FormItem>
|
||
|
<FormLabel>Word count</FormLabel>
|
||
|
<FormControl>
|
||
|
<Input type="number" step={500} min={0} {...field}></Input>
|
||
|
</FormControl>
|
||
|
</FormItem>
|
||
|
)}
|
||
|
/>
|
||
|
<FormField
|
||
|
control={form.control}
|
||
|
name="genres"
|
||
|
render={({ field }) => (
|
||
|
<FormItem>
|
||
|
|
||
|
</FormItem>
|
||
|
|
||
|
)}
|
||
|
|
||
|
/>
|
||
|
<Button type="submit">Submit</Button>
|
||
|
</form>
|
||
|
</Form>
|
||
|
)
|
||
|
}
|