"use client"; import { Field, FieldDescription, FieldLabel } from "@suphark/ui/components/ui/field"; import { Input } from "@suphark/ui/components/ui/input"; import { type ReactNode, useId, useState } from "react"; /** รูปแบบตัวระบุที่ derive จากชื่อ: kebab (slug/code/key), camel (attribute key), upper (รหัสตำแหน่ง) */ export type IdentifierStyle = "kebab" | "camel" | "upper"; /** * แปลงชื่อเป็นตัวระบุมาตรฐาน (เหมือน Clerk: พิมพ์ชื่อแล้ว slug ขึ้นให้เอง) — ตัดเครื่องหมายกำกับ, เก็บเฉพาะ a-z 0-9, * แบ่งคำที่ช่องว่าง/เครื่องหมาย; ตัวอักษรที่ไม่ใช่ละติน (เช่นไทย) ตัดทิ้ง → อาจได้ค่าว่าง = ให้ผู้ใช้ตั้งเอง */ export function toIdentifier(name: string, style: IdentifierStyle, maxLength = 64): string { const words = name .normalize("NFKD") .replace(/[\u0300-\u036f]/g, "") .replace(/[^A-Za-z0-9]+/g, " ") .trim() .split(/\s+/) .filter(Boolean) .map((w) => w.toLowerCase()); if (words.length === 0) return ""; let out: string; if (style === "camel") out = words.map((w, i) => (i === 0 ? w : w[0]!.toUpperCase() + w.slice(1))).join(""); else if (style === "upper") out = words.join("_").toUpperCase(); else out = words.join("-"); // camel ต้องขึ้นต้นด้วยตัวอักษร; kebab/upper ห้ามขึ้นต้น/ลงท้ายด้วยเครื่องหมาย (ตัดความยาวแล้วอาจเหลือ "-" ท้าย) out = out.slice(0, maxLength).replace(/[-_]+$/, ""); if (style === "camel" && /^[0-9]/.test(out)) out = `x${out}`; return out; } /** * คู่ช่อง "ชื่อ" + ตัวระบุ (slug/code/key) แบบ Clerk: พิมพ์ชื่อแล้วตัวระบุขึ้นให้อัตโนมัติ แก้เองได้ * เมื่อแก้เองแล้วจะไม่ถูกทับอีก (ลบจนว่าง = กลับไป auto); มีปุ่ม "ใช้ตามชื่อ" เพื่อกลับไปค่า derive * ส่งค่าเป็น input ธรรมดาชื่อ `nameField`/`idField` — ใช้ในฟอร์ม server action ได้เลย */ export function NameIdFields({ nameField = "name", idField, style = "kebab", nameLabel, namePlaceholder, idLabel, idHint, idPlaceholder, idPattern, idMaxLength = 64, resetLabel, autoFocus = true, defaultName = "", defaultId = "", }: { nameField?: string; idField: string; style?: IdentifierStyle; nameLabel: ReactNode; namePlaceholder?: string; idLabel: ReactNode; idHint?: ReactNode; idPlaceholder?: string; /** regex สำหรับ native validation ของ browser (ตรงกับ zod ฝั่ง server) */ idPattern?: string; idMaxLength?: number; /** ข้อความปุ่มกลับไปใช้ค่าจากชื่อ (แสดงเมื่อแก้เอง) */ resetLabel: ReactNode; autoFocus?: boolean; defaultName?: string; defaultId?: string; }) { const [name, setName] = useState(defaultName); const [id, setId] = useState(defaultId || toIdentifier(defaultName, style, idMaxLength)); const [manual, setManual] = useState(Boolean(defaultId)); const derived = toIdentifier(name, style, idMaxLength); const nameId = useId(); const idId = useId(); return ( <> {nameLabel} { setName(e.target.value); if (!manual) setId(toIdentifier(e.target.value, style, idMaxLength)); }} /> {idLabel} { setId(e.target.value); // ลบจนว่าง = กลับไปโหมด auto (จะเติมให้ใหม่เมื่อแก้ชื่อครั้งถัดไป) setManual(e.target.value !== ""); }} /> {(idHint || (manual && derived && derived !== id)) && ( {idHint} {manual && derived && derived !== id && ( )} )} ); }