176 lines
4.2 KiB
TypeScript
176 lines
4.2 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"
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
import { toast } from "@/components/ui/use-toast"
|
|
|
|
|
|
import {
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
} from "@/components/ui/popover"
|
|
import { cn } from "@/lib/utils"
|
|
import { Badge } from "lucide-react"
|
|
|
|
const formSchema = z.object({
|
|
title: z.string().min(2).max(50),
|
|
word_count: z.coerce.number(),
|
|
genres: z.array(z.number())
|
|
})
|
|
|
|
export default function StoryForm({ genres }) {
|
|
// 1. Define your form.
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: {
|
|
title: "",
|
|
word_count: 0,
|
|
genres: []
|
|
},
|
|
})
|
|
// 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.
|
|
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(values, null, 2)}</code>
|
|
</pre>
|
|
),
|
|
})
|
|
console.log(values)
|
|
}
|
|
|
|
|
|
|
|
function onErrors(errors) {
|
|
toast({
|
|
title: "You have errors",
|
|
description: (
|
|
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
|
|
<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
|
|
</pre>
|
|
),
|
|
})
|
|
console.log(JSON.stringify(errors))
|
|
}
|
|
|
|
|
|
return (
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} 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>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="genres"
|
|
render={({ field }) => (
|
|
<FormItem className="flex flex-col">
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
variant={"outline"}
|
|
className={cn(
|
|
"w-[240px] pl-3 text-left font-normal",
|
|
!field.value && "text-muted-foreground"
|
|
)}
|
|
>
|
|
{field.value.length !== 0 ? (
|
|
field.value.map((e,i)=>genres.find(f=>e===f.id).name+
|
|
( i<field.value.length-1?', ':'' ))
|
|
) : (
|
|
<FormLabel>Genres</FormLabel>
|
|
)}
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent>
|
|
{genres.map((item) => (
|
|
<FormField
|
|
key={item.id}
|
|
control={form.control}
|
|
name="genres"
|
|
render={({ field }) => {
|
|
return (
|
|
<FormItem
|
|
key={item.id}
|
|
className="flex flex-row items-start space-x-3 space-y-0"
|
|
>
|
|
<FormControl>
|
|
<Checkbox
|
|
checked={field.value?.includes(item.id)}
|
|
onCheckedChange={(checked) => {
|
|
return checked
|
|
? field.onChange([...field.value, item.id])
|
|
: field.onChange(
|
|
field.value?.filter(
|
|
(value) => value !== item.id
|
|
)
|
|
)
|
|
}}
|
|
/>
|
|
</FormControl>
|
|
<FormLabel className="text-sm font-normal">
|
|
{item.name}
|
|
</FormLabel>
|
|
</FormItem>
|
|
)
|
|
}}
|
|
/>
|
|
))}
|
|
</PopoverContent>
|
|
</Popover>
|
|
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<Button type="submit">Submit</Button>
|
|
</form>
|
|
</Form>
|
|
)
|
|
}
|