import { Minus, Plus } from "lucide-react"; import * as React from "react"; import { cn } from "../utils"; type Props = { value?: number; min?: number; max?: number; onChange?: (value: number) => void; onBlur?: () => void; onFocus?: () => void; className?: string; step?: number; placeholder?: string; }; export function QuantityInput({ value = 0, min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY, onChange, onBlur, onFocus, className, step = 0.1, placeholder = "0", }: Props) { const inputRef = React.useRef(null); const [rawValue, setRawValue] = React.useState(String(value)); const handleInput = ({ currentTarget: el, }: { currentTarget: HTMLInputElement; }) => { const input = el.value; setRawValue(input); // Check if input can be parsed as a valid number const num = Number.parseFloat(input); if (!Number.isNaN(num) && min <= num && num <= max) { onChange?.(num); } }; const handlePointerDown = (diff: number) => (event: React.PointerEvent) => { if (event.pointerType === "mouse") { event.preventDefault(); inputRef.current?.focus(); } const newVal = Math.min(Math.max(value + diff, min), max); onChange?.(newVal); setRawValue(String(newVal)); }; return (
); }