import { zodResolver } from "@hookform/resolvers/zod"; import { SubmitHandler, useForm } from "react-hook-form"; import z from "zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "../ui/form"; import { Input } from "../ui/input"; const oneEmoji = z .string() .refine((val) => val.length === 0 || [...val].length === 1, { error: "Please enter a single emoji", }); const gridDimension = (dir: "row" | "column") => z.coerce.number().min(2, { error: `Minimum 2 ${dir}s` }); type FormData = z.infer; const formSchema = z.object({ width: gridDimension("column"), height: gridDimension("row"), variants: z.array(oneEmoji).min(2).max(4), }); interface Props { defaultValues: FormData; onSubmit: SubmitHandler; buttons?: () => React.ReactNode; } export function PreferencesForm({ defaultValues, onSubmit, buttons }: Props) { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues, }); return (
{ formData.variants = formData.variants.filter(Boolean); return onSubmit(formData); })} >
( Columns e.target.select()} {...field} /> )} /> ( Rows e.target.select()} {...field} /> )} /> ( Left e.target.select()} className="text-center text-2xl" /> )} /> ( Top e.target.select()} className="text-center text-2xl" /> )} /> ( Right e.target.select()} className="text-center text-2xl" /> )} /> ( Bottom e.target.select()} className="text-center text-2xl" /> )} />
{buttons?.()}
); }