"use client"; import { FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@suphark/ui/components/ui/form"; import { RadioGroup, RadioGroupItem } from "@suphark/ui/components/ui/radio-group"; import type { Control, FieldValues, Path } from "react-hook-form"; type RadioOption = { value: string; label: string; }; interface FormFieldRadioGroupProps { control: Control; name: Path; label: string; options: RadioOption[]; disabled?: boolean; } const BOOLEAN_VALUES = new Set(["true", "false", "null"]); /** * ตัวเลือกแบบปุ่มกลม — ค่าในฟอร์มเป็นค่า `value` ของตัวเลือก (string) * ยกเว้นตัวเลือกเป็น "true"/"false"/"null" ทั้งหมด → เก็บเป็น boolean/null (แบบเดิม) */ export function FormFieldRadioGroup({ control, name, label, options, disabled, }: FormFieldRadioGroupProps) { const booleanMode = options.length > 0 && options.every((o) => BOOLEAN_VALUES.has(o.value)); const toField = (value: string) => booleanMode ? (value === "null" ? null : value === "true") : value; const fromField = (value: unknown) => { if (booleanMode) return value === undefined ? null : String(value); return value === undefined || value === null ? null : String(value); }; return ( ( {label} field.onChange(toField(String(value)))} value={fromField(field.value)} disabled={disabled} className="flex items-center gap-4" > {options.map((option) => ( {option.label} ))} )} /> ); }