"use client"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue, } from "@suphark/ui/components/ui/select"; import { cn } from "@suphark/ui/lib/utils"; import * as React from "react"; export interface SimpleSelectOption { value: T; label: React.ReactNode; disabled?: boolean; } export interface SimpleSelectGroup { label?: string; options: SimpleSelectOption[]; } export type SimpleSelectOptions = | SimpleSelectOption[] | SimpleSelectGroup[]; export interface SimpleSelectProps { value?: T; defaultValue?: T; onValueChange?: (value: T) => void; options: SimpleSelectOptions; placeholder?: string; fallbackLabel?: (value: T) => React.ReactNode; disabled?: boolean; size?: "sm" | "default"; className?: string; triggerClassName?: string; contentClassName?: string; ariaLabel?: string; /** ชื่อฟิลด์ใน FormData (server action) — ส่งค่าที่เลือกผ่าน hidden input ของ Base UI */ name?: string; /** บังคับเลือกก่อน submit ฟอร์ม */ required?: boolean; /** * ค่าของ option ที่หมายถึง "ไม่เลือก/ไม่มี" — Base UI ไม่รับค่าว่างเป็น `value` ของ option แอปจึงต้องใช้ * ค่าพิเศษ (เช่น `"__none__"`); ส่ง `emptyValue` มาแล้วคอมโพเนนต์จะส่ง **ค่าว่าง** ใน FormData แทน * ไม่ให้ sentinel ของ UI หลุดไปถึง server action (ขอจาก Pi Config — `SectionSelect` เคยต้องทำ hidden input เอง) * หมายเหตุ: ใช้คู่ `required` ไม่ได้ (hidden input ไม่เข้า constraint validation ของ browser) ให้ตรวจฝั่ง schema */ emptyValue?: string; } function isGroupList( options: SimpleSelectOptions, ): options is SimpleSelectGroup[] { const first = options[0]; return first !== undefined && "options" in first; } export function SimpleSelect({ value, defaultValue, onValueChange, options, placeholder, fallbackLabel, disabled, size = "default", className, triggerClassName, contentClassName, ariaLabel, name, required, emptyValue, }: SimpleSelectProps) { // ส่งค่าเองเมื่อมี emptyValue (ต้องแปลง sentinel → "") — นอกนั้นใช้ hidden input ของ Base UI const ownsHiddenInput = Boolean(name) && emptyValue !== undefined; const [internalValue, setInternalValue] = React.useState(defaultValue); const currentValue = value ?? internalValue; const { flatOptions, groups } = React.useMemo(() => { if (isGroupList(options)) { const flat = options.flatMap((g) => g.options); return { flatOptions: flat, groups: options }; } return { flatOptions: options, groups: [{ options }], }; }, [options]); const items = React.useMemo( () => flatOptions.map((opt) => ({ value: opt.value, label: opt.label, })), [flatOptions], ); const optionsMap = React.useMemo(() => { const map = new Map(); for (const opt of flatOptions) { map.set(opt.value, opt.label); } return map; }, [flatOptions]); const resolveLabel = React.useCallback( (val: unknown) => { if (val === undefined || val === null || val === "") { return placeholder; } const strVal = String(val); if (optionsMap.has(strVal)) { return optionsMap.get(strVal); } if (fallbackLabel) { return fallbackLabel(strVal as T); } return strVal; }, [optionsMap, fallbackLabel, placeholder], ); return ( )} ); }