"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { IconPicker, type IconPickerCategory, type IconPickerItem, } from "@suphark/ui/components/shared/icon-picker"; import { ResponsivePopover, ResponsivePopoverContent, ResponsivePopoverTrigger, } from "@suphark/ui/components/shared/responsive-popover"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronDownIcon } from "lucide-react"; import { useState } from "react"; import { ICON_CATALOG, ICON_CATEGORIES } from "./icon-catalog"; import { getCatalogIcon, isCatalogIconName, resolveCatalogIconName } from "./icon-catalog-lookup"; export { NamedIcon } from "./named-icon"; // ค้นจากชื่อ/NamedIcon อยู่ในไฟล์ที่ไม่มี "use client" (server ใช้ได้) — re-export ไว้ให้ import จากที่นี่ได้เหมือนเดิม export { getCatalogIcon, isCatalogIconName, resolveCatalogIconName }; export interface IconSelectProps { value: string | null | undefined; onChange: (name: string | null) => void; /** ชุดไอคอน (ค่าเริ่มต้น `ICON_CATALOG` 372 ตัว) */ icons?: ReadonlyArray; /** ปุ่มกรองหมวด — ค่าเริ่มต้น หมวดของชุดมาตรฐาน · false = ไม่แสดง */ categories?: ReadonlyArray | false; /** มีปุ่ม "ไม่ระบุไอคอน" → `onChange(null)` */ clearable?: boolean; placeholder?: string; /** ชื่อ field สำหรับฟอร์ม (input ซ่อน = ชื่อไอคอน) */ name?: string; disabled?: boolean; id?: string; "aria-label"?: string; /** สีของไอคอนที่เลือก (CSS color) เช่น สีของหมวด */ accentColor?: string; /** false = ปุ่มสี่เหลี่ยมแสดงไอคอนอย่างเดียว */ showLabel?: boolean; size?: "sm" | "default"; className?: string; } /** * ช่องเลือกไอคอนสำเร็จรูป: ปุ่มแสดงไอคอน + ชื่อ → เปิดชุดไอคอนมาตรฐานพร้อมช่องค้นหา (ไทย/อังกฤษ) และหมวด * (แผ่นล่างบนมือถือ) — เก็บแค่ชื่อไอคอนในฐานข้อมูล แล้วแสดงด้วย `NamedIcon` * * */ export function IconSelect({ value, onChange, icons = ICON_CATALOG, categories: categoriesProp, clearable = false, placeholder, name, disabled, id, "aria-label": ariaLabel, accentColor, showLabel = true, size = "default", className, }: IconSelectProps) { const text = useUiText(); const t = text.iconSelect; const language = text.lang === "en" ? "en" : "th"; const [open, setOpen] = useState(false); // ค่าเก่าแบบ "Home"/"CreditCard" ก็แสดงเป็นตัวที่เลือกได้ — เลือกใหม่แล้วได้ชื่อในชุด const current = value && !icons.some((i) => i.name === value) ? (resolveCatalogIconName(value) ?? value) : value; const selected = current ? icons.find((i) => i.name === current) : undefined; const title = t?.title ?? "เลือกไอคอน"; const label = selected ? language === "en" ? selected.labelEn : selected.labelTh : (placeholder ?? t?.placeholder ?? "เลือกไอคอน"); const categories = categoriesProp === false ? undefined : (categoriesProp ?? ICON_CATEGORIES.map((c) => ({ value: c.value, label: language === "en" ? c.labelEn : c.labelTh, }))); const pick = (next: string | null) => { onChange(next); setOpen(false); }; return ( <> {selected ? ( {name && } ); }