"use client";
import { NumberField } from "@base-ui/react/number-field";
import { useSelectOnFocus } from "@suphark/ui/hooks/use-select-on-focus";
import { cn } from "@suphark/ui/lib/utils";
import { MinusIcon, PlusIcon } from "lucide-react";
import { type ReactNode, type Ref, useCallback, useEffect, useRef } from "react";
import { useUiText } from "../ui-text";
/**
* input ซ่อนของ Base UI (ที่ส่งค่าใน FormData) ได้ attribute step ไปด้วย → browser ปฏิเสธค่าที่ไม่ลง step นับจาก min
* (เช่น 12.50 เมื่อ step=1, 150 เมื่อ min=0 step=100) ทั้งที่ step มีไว้แค่ให้ปุ่ม −/+ เลื่อน — ตั้ง step="any" ให้ input ซ่อน
* (React ไม่เขียน attribute ทับเพราะ prop step ไม่เปลี่ยน) ส่วน min/max ยังตรวจตามเดิม
*/
function relaxHiddenStep(el: HTMLInputElement | null) {
if (el) el.step = "any";
}
// หมายเหตุ: เมื่อ prop step เปลี่ยน React เขียน attribute step ใหม่ → effect ใน NumberInput ตั้ง "any" ซ้ำ
const SIZE = {
// มือถือ ≥16px (iOS ไม่ซูมตอนโฟกัส) — จอกว้างค่อยเล็กลง
sm: { group: "h-7", button: "w-7 [&_svg]:size-3.5", text: "text-base md:text-sm" },
default: { group: "h-8", button: "w-8 [&_svg]:size-4", text: "text-body" },
} as const;
/**
* ช่องตัวเลขพร้อมปุ่ม −/+ (Base UI NumberField) — แทน `` + InputGroup + state เอง
* ลูกศรขึ้น/ลง (Shift = `largeStep`), จัดรูปแบบตาม locale (`format` เช่น ทศนิยม/สกุลเงิน), `prefix`/`suffix` เช่น ฿ หรือ %
* `name` ส่งค่าใน FormData ของ server action ได้ (ค่าดิบไม่มีจุลภาค)
*
*
*
*/
export function NumberInput({
value,
defaultValue,
onValueChange,
min,
max,
step = 1,
largeStep,
format,
locale,
prefix,
suffix,
hideButtons = false,
size = "default",
name,
id,
required,
disabled,
readOnly,
placeholder,
className,
inputClassName,
"aria-label": ariaLabel,
"aria-describedby": ariaDescribedBy,
"aria-invalid": ariaInvalid,
inputRef,
autoFocus,
selectOnFocus = true,
}: {
value?: number | null;
defaultValue?: number;
onValueChange?: (value: number | null) => void;
min?: number;
max?: number;
step?: number;
/** ก้าวเมื่อกด Shift + ลูกศร / PageUp-PageDown (ค่าเริ่มต้น step × 10) */
largeStep?: number;
/** Intl.NumberFormatOptions เช่น `{ maximumFractionDigits: 2 }` หรือ `{ style: "percent" }` */
format?: Intl.NumberFormatOptions;
/** ค่าเริ่มต้น th-TH */
locale?: Intl.LocalesArgument;
/** ข้อความ/ไอคอนหน้าตัวเลข เช่น "฿" */
prefix?: ReactNode;
/** ข้อความ/ไอคอนหลังตัวเลข เช่น "%", "ชิ้น" */
suffix?: ReactNode;
/** ซ่อนปุ่ม −/+ (ยังใช้ลูกศรคีย์บอร์ดได้) */
hideButtons?: boolean;
size?: "sm" | "default";
name?: string;
id?: string;
required?: boolean;
disabled?: boolean;
readOnly?: boolean;
placeholder?: string;
className?: string;
inputClassName?: string;
"aria-label"?: string;
"aria-describedby"?: string;
"aria-invalid"?: boolean | "true" | "false";
/** ref ของช่องที่ผู้ใช้พิมพ์ (เช่น focus() เอง) */
inputRef?: Ref;
autoFocus?: boolean;
/** เลือกตัวเลขทั้งหมดเมื่อคลิก/Tab เข้าช่อง — พิมพ์ทับค่าเดิมได้ทันที; คลิกซ้ำในช่องที่ focus อยู่ = วางเคอร์เซอร์ตามปกติ · ค่าเริ่มต้น true (0.119.0) */
selectOnFocus?: boolean;
}) {
const selectHandlers = useSelectOnFocus(selectOnFocus);
const text = useUiText().number;
const hiddenEl = useRef(null);
const hiddenRef = useCallback((el: HTMLInputElement | null) => {
hiddenEl.current = el;
relaxHiddenStep(el);
}, []);
// step เปลี่ยนหลัง mount → React เขียน attribute step ใหม่ → ตั้ง "any" ซ้ำ
// biome-ignore lint/correctness/useExhaustiveDependencies: step เป็นตัวกระตุ้น — React เขียน attribute step ใหม่ทุกครั้งที่ prop เปลี่ยน
useEffect(() => {
relaxHiddenStep(hiddenEl.current);
}, [step]);
const s = SIZE[size];
const buttonClass = cn(
"flex shrink-0 items-center justify-center text-muted-foreground transition-colors outline-none hover:bg-muted hover:text-foreground focus-visible:bg-muted disabled:pointer-events-none disabled:opacity-50",
s.button,
);
const affix = "flex shrink-0 items-center px-2 text-muted-foreground text-sm select-none";
return (
onValueChange?.(v)}
min={min}
max={max}
step={step}
largeStep={largeStep ?? step * 10}
format={format}
locale={locale ?? "th-TH"}
required={required}
disabled={disabled}
readOnly={readOnly}
inputRef={hiddenRef}
className={cn("w-full", className)}
>
{/* ปุ่มลด/prefix อยู่หลัง input ใน DOM (label ที่ครอบช่องนี้คลิกแล้วเลือก input ไม่ใช่ปุ่มลด) แต่แสดงด้านซ้ายด้วย order */}
{!hideButtons && (
)}
{prefix && (
{prefix}
)}
{suffix && {suffix}}
{!hideButtons && (
)}
);
}