"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { MultiSelectCombobox } from "@suphark/ui/components/shared/multi-select-combobox"; import { SwitcherChevron } from "@suphark/ui/components/shared/switcher-chevron"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@suphark/ui/components/ui/command"; import { FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@suphark/ui/components/ui/form"; import { Popover, PopoverContent, PopoverTrigger } from "@suphark/ui/components/ui/popover"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@suphark/ui/components/ui/select"; import { commandContainsFilter } from "@suphark/ui/lib/command-filter"; import { cn } from "@suphark/ui/lib/utils"; import { Check } from "lucide-react"; import * as React from "react"; import type { ControllerRenderProps, FieldValues, Path, PathValue } from "react-hook-form"; import { RequiredHint } from "./required-hint"; // Unified Option Type export type SelectOption = { value: string; label: string; // Combobox specific fields (optional) subLabel?: string; superLabel?: string; searchableValue?: string; }; // Base Props type BaseProps = { control: any; // Using any to bypass deep generic reconciliation errors with Control in RHF 7 name: Path; label: string; /** * ชื่อของช่องสำหรับโปรแกรมอ่านจอเมื่อไม่มีป้าย (`label=""`) เช่น ช่องหน่วยข้างช่องตัวเลข — ไม่ส่ง = ใช้ `label` */ ariaLabel?: string; options: SelectOption[]; placeholder?: string; disabled?: boolean; required?: boolean; formItemClassName?: string; description?: string; }; // Discriminated Union Props type FormFieldSelectProps = BaseProps & ( | { variant?: "select"; // Default onValueChange?: (value: string) => void; } | { variant: "combobox"; searchPlaceholder?: string; searchable?: boolean; modal?: boolean; emptyContent?: React.ReactNode; } | { variant: "multiselect"; searchPlaceholder?: string; creatable?: boolean; modal?: boolean; } ); /** ค่าให้ cmdk ระบุ/ค้นหารายการ — ต่อ value ท้าย label กัน label ซ้ำแล้วรายการชนกัน */ function commandValue(option: SelectOption): string { return `${option.searchableValue || option.label} ${option.value}`; } // --- Combobox Internal Logic --- function ComboboxContent({ field, options, disabled, placeholder, searchPlaceholder, searchable = true, modal, emptyContent, required, ariaLabel, }: { field: ControllerRenderProps>; options: SelectOption[]; disabled?: boolean; placeholder?: string; searchPlaceholder?: string; searchable?: boolean; modal?: boolean; emptyContent?: React.ReactNode; required?: boolean; ariaLabel: string; }) { const text = useUiText(); const [open, setOpen] = React.useState(false); // Fix: Handle null/undefined field.value safely const fieldVal = field.value ?? ""; const selectedOption = options.find((option) => option.value === fieldVal); const selectedLabel = selectedOption?.label || placeholder; const selectedCommandValue = selectedOption ? commandValue(selectedOption) : undefined; return ( }> {searchable && ( )} {searchable ? ( {emptyContent ?? text.combobox?.empty ?? "ไม่พบข้อมูล"} ) : null} {options.map((option) => ( { // คลิกตัวที่เลือกอยู่ = คงค่าเดิม (เดิมล้างค่า — ช่องบังคับกลายเป็นว่างโดยไม่ตั้งใจ) field.onChange(option.value as PathValue>); setOpen(false); }} >
{option.superLabel && (

{option.superLabel}

)} {option.label} {option.subLabel && (

{option.subLabel}

)}
))}
); } // --- Multiselect Combobox Internal Logic --- function MultiSelectComboboxContent({ field, options, disabled, placeholder, searchPlaceholder, creatable = false, modal, ariaLabel, }: { field: ControllerRenderProps>; options: SelectOption[]; disabled?: boolean; placeholder?: string; searchPlaceholder?: string; creatable?: boolean; modal?: boolean; ariaLabel: string; }) { const rawValue = field.value || []; const value = Array.isArray(rawValue) ? rawValue : []; // FormControl ครอบแบบมีลูก (clone ส่ง id/aria-describedby/aria-invalid ให้ปุ่มของ MultiSelect) // เดิม triggerRender={} ไม่มีลูก = ปุ่มกลายเป็น
(ไม่มี role combobox + Base UI เตือน nativeButton) return ( field.onChange(nextVal as PathValue>)} disabled={disabled} placeholder={placeholder} searchPlaceholder={searchPlaceholder} creatable={creatable} modal={modal} ariaLabel={ariaLabel} /> ); } export function FormFieldSelect({ control, name, label, ariaLabel, options, placeholder, formItemClassName, description, disabled, required, ...props }: FormFieldSelectProps) { // Safeguard: Deduplicate options by value to prevent React key warnings const uniqueOptions = React.useMemo(() => { const seen = new Set(); return options.filter((option) => { if (!option.value || seen.has(option.value)) return false; seen.add(option.value); return true; }); }, [options]); return ( ( {/* label="" (ช่องหน่วยข้างช่องตัวเลข ฯลฯ) = ไม่มีป้ายว่างค้าง ชื่อมาจาก ariaLabel แทน */} {label ? ( {label}{" "} {required ? ( ) : null} {required ? : null} ) : null} {props.variant === "combobox" ? ( ) : props.variant === "multiselect" ? ( ) : ( )} {description &&

{description}

}
)} /> ); }