import { Checkbox } from "@suphark/ui/components/ui/checkbox"; import { Field, FieldDescription, FieldError, FieldLabel } from "@suphark/ui/components/ui/field"; import { Label } from "@suphark/ui/components/ui/label"; import { Switch } from "@suphark/ui/components/ui/switch"; import { cn } from "@suphark/ui/lib/utils"; import type { ComponentProps, ReactNode } from "react"; /** * ช่องฟอร์มสำหรับ server action ธรรมดา (ไม่ใช่ react-hook-form): label + control + hint หรือ error * ประกอบจาก shadcn Field/FieldLabel/FieldDescription/FieldError — ยกจาก Pi Auth (ใช้ 42 ไฟล์ คง API เดิม) * ใช้ได้จาก server component (ไม่มี state) * * * * */ export function LabeledField({ label, hint, error, children, className, htmlFor, }: { label: ReactNode; /** ข้อความช่วยใต้ช่อง — ซ่อนเมื่อมี error */ hint?: ReactNode; error?: ReactNode; children: ReactNode; className?: string; /** * id ของ control — ส่งมาแล้ว label จะไม่ครอบ control (ใช้ `htmlFor` แทน) เพื่อให้ accessible name ชัดใน tooling * ที่ไม่คำนวณ label แบบ nested; ไม่ส่ง = label ครอบ control (ถูกต้องตาม HTML คลิก label แล้ว focus ได้) */ htmlFor?: string; }) { if (htmlFor) { return ( {label} {children} {error ? ( {error} ) : ( hint && {hint} )} ); } return ( {label} {children} {error ? ( {error} ) : ( hint && {hint} )} ); } /** * Checkbox ของ shadcn + label บรรทัดเดียว — ใส่ `name` แล้วส่ง "on" ใน FormData ของ server action ได้ * * * */ export function CheckboxField({ label, hint, className, ...props }: ComponentProps & { label: ReactNode; /** คำอธิบายใต้ label (เยื้องตรงกับ label) */ hint?: ReactNode; }) { if (!hint) { return ( ); } return ( ); } /** * สวิตช์ + ป้ายในตัว (คลิกป้ายก็สลับ) — ใช้แทน `` ที่ต้อง biome-ignore เพราะ Switch เป็น span * `size` ส่งต่อให้ Switch · `className` ทับที่ป้ายทั้งก้อน (ใส่สไตล์ hover/ตัวหนังสือได้) · ป้ายใหญ่ขึ้นเป็น 44px บนจอสัมผัส * * */ export function SwitchField({ label, hint, className, labelClassName, ...props }: ComponentProps & { label: ReactNode; /** คำอธิบายใต้ label (เยื้องตรงกับ label) */ hint?: ReactNode; /** ทับสไตล์ข้อความป้าย (ค่าเริ่มต้น `text-sm`) เช่น `text-caption font-semibold` */ labelClassName?: string; }) { return ( ); }