"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { Card, CardContent } from "@suphark/ui/components/ui/card"; import { errorMessage, isNavigationError } from "@suphark/ui/lib/action-result"; import { cn } from "@suphark/ui/lib/utils"; import { type ComponentProps, type ReactNode, startTransition, useActionState } from "react"; import { useFormInvalidNotice } from "./form/form-invalid-notice"; import { type FormAction, FormMessage, type FormRenderContext, type FormState, formRenderContext, } from "./form-message"; import { useUiText } from "./ui-text"; /** * ฟอร์ม inline คู่กับ `FormDialog` (อันนั้นอยู่ในกล่องโต้ตอบ อันนี้อยู่บนหน้า): `useActionState` + `FormMessage` + ปุ่มส่ง * ไม่ใช้ `action=` บน
— React 19 รีเซ็ตฟอร์ม uncontrolled หลัง action จบทุกครั้ง (แม้คืน error) * ค่าที่พิมพ์จะหาย; เรียก action ใน transition เองแทน ฟอร์มจึงคงค่าไว้ให้แก้ต่อ (แบบเดียวกับ FormDialog) * ไม่พึ่ง next (ไม่ refresh เอง — ให้ server action `revalidatePath` หรือส่ง `onSuccess`) * * * * * // error รายช่อง: action คืน { ok: false, error: "…", errors: { email: "รูปแบบไม่ถูกต้อง" } } * {({ fieldError }) => …} * // payload เมื่อสำเร็จ: action คืน { ok: true, data: { key } } → onSuccess={(s) => setKey(s.data?.key)}> */ export function ActionForm({ action, title, submitLabel, children, compact = false, className, onSuccess, hideSuccessMessage = false, submitDisabled = false, submitProps, }: { action: FormAction; title?: ReactNode; submitLabel?: ReactNode; /** เนื้อฟอร์ม — เป็นฟังก์ชันได้เพื่อรับ `{ state, pending, errors, fieldError }` (error รายช่องจาก `FormState.errors`) */ children: ReactNode | ((ctx: FormRenderContext) => ReactNode); /** ฟอร์มเล็กในแถวตาราง — ไม่มีกรอบ Card, ปุ่ม size sm, จัดแนวนอน wrap */ compact?: boolean; className?: string; /** เรียกเมื่อ action คืน `ok: true` (เช่น router.refresh, toast) — `state.data` คือ payload ของ `FormState` */ onSuccess?: (state: Extract, { ok: true }>) => void; /** ไม่แสดงข้อความสำเร็จใต้ฟอร์ม (แอปแจ้งผลด้วย toast/Notice เอง — ใน compact ข้อความจะดันปุ่มตกบรรทัด) ยังแสดง error */ hideSuccessMessage?: boolean; /** ปิดปุ่มส่งตามเงื่อนไขของแอป (เช่น ยังไม่มีรายการให้เลือก) — รวมกับ pending */ submitDisabled?: boolean; /** props เพิ่มของปุ่มส่ง (variant, size, className, …) */ submitProps?: Omit, "type" | "disabled" | "children">; }) { const text = useUiText(); const invalid = useFormInvalidNotice(); const [state, formAction, pending] = useActionState, FormData>( async (prev, fd) => { let next: FormState; try { next = await action(prev, fd); } catch (err) { // throw (เน็ตหลุด / server error) = ข้อความใต้ฟอร์ม ไม่ใช่หน้าพังทั้งหน้า — redirect() ปล่อยผ่าน if (isNavigationError(err)) throw err; return { ok: false, error: errorMessage(err, text.actionFailed ?? "ดำเนินการไม่สำเร็จ") }; } if (next?.ok) onSuccess?.(next); return next; }, null, ); const form = ( { e.preventDefault(); const fd = new FormData(e.currentTarget); startTransition(() => formAction(fd)); }} className={cn( compact ? "flex flex-wrap items-end gap-2" : "flex flex-col gap-3", compact && className, )} > {title && !compact &&

{title}

} {typeof children === "function" ? children(formRenderContext(state, pending)) : children} {invalid.notice &&
{invalid.notice}
} {!(hideSuccessMessage && state?.ok) && ( )} ); if (compact) return form; return ( {form} ); }