"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { ColorPicker, type ColorPickerItem } from "@suphark/ui/components/shared/color-picker"; import { ResponsivePopover, ResponsivePopoverContent, ResponsivePopoverTrigger, } from "@suphark/ui/components/shared/responsive-popover"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { isPaletteColor, PALETTE, PALETTE_COLORS, type PaletteColor, paletteClasses, } from "@suphark/ui/lib/palette"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronDownIcon } from "lucide-react"; import { useState } from "react"; /** แปลงชุดสีมาตรฐานเป็นรายการของ `ColorPicker` (ใช้กับ ColorPicker ตรง ๆ ได้) */ export function paletteColorItems( colors: ReadonlyArray = PALETTE_COLORS, language: "th" | "en" = "th", ): ColorPickerItem[] { return colors.map((value) => ({ value, label: language === "en" ? PALETTE[value].labelEn : PALETTE[value].labelTh, swatchClassName: PALETTE[value].dot, })); } export interface ColorSelectProps { value: string | null | undefined; onChange: (color: PaletteColor | null) => void; /** สีที่ให้เลือก (ค่าเริ่มต้น ครบ 20 สี; `PALETTE_BASIC` = 8 สี) */ colors?: ReadonlyArray; /** มีปุ่ม "ไม่ระบุสี" ในตัวเลือก → `onChange(null)` */ clearable?: boolean; placeholder?: string; /** ชื่อ field สำหรับฟอร์ม (input ซ่อน = ชื่อสี) */ name?: string; disabled?: boolean; id?: string; "aria-label"?: string; size?: "sm" | "default"; className?: string; } /** * ช่องเลือกสีจากชุดสีมาตรฐาน — ปุ่มแสดงเม็ดสี + ชื่อสี เปิดกริดสี (แผ่นล่างบนมือถือ) * * * */ export function ColorSelect({ value, onChange, colors = PALETTE_COLORS, clearable = false, placeholder, name, disabled, id, "aria-label": ariaLabel, size = "default", className, }: ColorSelectProps) { const text = useUiText(); const t = text.colorSelect; const language = text.lang === "en" ? "en" : "th"; const [open, setOpen] = useState(false); const selected = isPaletteColor(value) ? value : null; const title = t?.title ?? "เลือกสี"; const label = selected ? language === "en" ? PALETTE[selected].labelEn : PALETTE[selected].labelTh : (placeholder ?? t?.placeholder ?? "เลือกสี"); const pick = (next: PaletteColor | null) => { onChange(next); setOpen(false); }; return ( <> {name && } ); }