"use client" import { z } from "zod" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { Plus } from "lucide-react" import { Button } from "@/shared/components/ui/button" import { FieldGroup } from "@/shared/components/ui/field" import { Rhform, RhfTextField } from "@/shared/components/form" import { toast } from "sonner" import type { InlineCreateFormProps } from "./rhf-async-select-field" const schema = z.object({ title: z.string().min(1, "Required") }) type FormValues = z.infer export type SimpleTitleFormProps = InlineCreateFormProps & { placeholder?: string submitLabel?: string createFn: (title: string) => Promise mapResult?: (data: any) => { value: string; label: string } } const defaultMapResult = (data: any) => { const item = data?.data ?? data return { value: String(item.id), label: item.name ?? item.title ?? String(item.id), } } export function SimpleTitleForm({ onSuccess, placeholder = "Enter name", submitLabel = "Create", createFn, mapResult = defaultMapResult, }: SimpleTitleFormProps) { const form = useForm({ resolver: zodResolver(schema), defaultValues: { title: "" }, }) const handleSubmit = async (values: FormValues) => { try { const result = await createFn(values.title) toast.success(`${submitLabel}d successfully`) form.reset() onSuccess(mapResult(result)) } catch { toast.error(`Failed to ${submitLabel.toLowerCase()}`) } } return ( ) }