"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" import { Checkbox } from "@/components/ui/checkbox" const formSchema = z.object({ title: z.string().min(2).max(50), word_count: z.number(), genres: z.object({ id: z.number(), name: z.string() }).array() }) export default function FancyForm({ genres }) { // 1. Define your form. const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { title: "", word_count: 0, genres: genres }, }) // 2. Define a submit handler. function onSubmit(values: z.infer) { // Do something with the form values. // ✅ This will be type-safe and validated. console.log(values) } return (
( Title )} /> ( Word count )} /> (
Genres genres baby
{genres.map((item) => ( { return ( { return checked ? field.onChange([...field.value, item.id]) : field.onChange( field.value?.filter( (value) => value !== item.id ) ) }} /> {item.name} ) }} /> ))}
)} /> ) }