"use client"; import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@suphark/ui/components/ui/form"; import { Input } from "@suphark/ui/components/ui/input"; import { useSelectOnFocus } from "@suphark/ui/hooks/use-select-on-focus"; import type * as React from "react"; import type { Control, FieldValues, Path } from "react-hook-form"; // Removed unused imports import { type NumberFormatValues, NumericFormat, type NumericFormatProps, type SourceInfo, } from "react-number-format"; // Get Input props type correctly type ShadcnInputProps = React.ComponentPropsWithoutRef; // Define props based on NumericFormatProps and RHF needs interface FormFieldFormattedNumberProps extends Omit< NumericFormatProps, | "value" | "defaultValue" | "name" | "form" | "ref" | "customInput" | "getInputRef" | "onValueChange" | "onBlur" | "type" | "onChange" | "valueIsNumericString" > { name: Path; control: Control; label?: string; description?: string; required?: boolean; inputClassName?: string; formItemClassName?: string; /** * เลือกตัวเลขทั้งหมดเมื่อคลิก/Tab เข้าช่อง — พิมพ์ทับค่าเดิม (เช่น 0) ได้ทันที; คลิกซ้ำในช่องที่ focus อยู่ = วางเคอร์เซอร์ตามปกติ * ค่าเริ่มต้น true (0.119.0) — `false` = พฤติกรรมเดิมของ browser */ selectOnFocus?: boolean; } export function FormFieldFormattedNumber({ name, control, label, description, required, thousandSeparator = true, decimalScale, allowNegative = false, prefix, suffix, inputClassName, formItemClassName, selectOnFocus = true, onFocus, onMouseUp, onPointerDown, ...numericFormatProps // Rest are valid NumericFormatProps passed down }: FormFieldFormattedNumberProps) { const selectHandlers = useSelectOnFocus(selectOnFocus, { onFocus, onMouseUp, onPointerDown }); return ( ( {label && ( {label} {required && *} )} { // รับเฉพาะที่ผู้ใช้พิมพ์ — ค่าที่เปลี่ยนจาก prop (reset/setValue) ห้ามส่งค่าเดิมกลับไปทับ if (sourceInfo.source !== "event") return; onChange(values.floatValue ?? undefined); }} onBlur={onBlur} thousandSeparator={thousandSeparator} decimalScale={decimalScale} allowNegative={allowNegative} prefix={prefix} suffix={suffix} {...numericFormatProps} {...selectHandlers} customInput={Input} className={inputClassName} valueIsNumericString={false} allowLeadingZeros={false} /> {description && {description}} )} /> ); }