import * as React from "react" import { Minus, Plus } from "lucide-react" import { Input } from "@/components/ui/input" import { cn } from "@/lib/utils" interface NumberInputProps extends Omit, "type" | "onChange" | "value"> { value?: number defaultValue?: number min?: number max?: number step?: number onChange?: (value: number) => void } const NumberInput = React.forwardRef( ( { className, value: controlledValue, defaultValue = 0, min = -Infinity, max = Infinity, step = 1, onChange, disabled, readOnly, ...props }, ref ) => { const [internalValue, setInternalValue] = React.useState(defaultValue) const isControlled = controlledValue !== undefined const currentValue = isControlled ? controlledValue : internalValue const updateValue = (newValue: number) => { const clamped = Math.min(max, Math.max(min, newValue)) if (!isControlled) { setInternalValue(clamped) } onChange?.(clamped) } const handleIncrement = () => { if (!disabled && !readOnly) { updateValue(currentValue + step) } } const handleDecrement = () => { if (!disabled && !readOnly) { updateValue(currentValue - step) } } const handleInputChange = (e: React.ChangeEvent) => { const parsed = parseFloat(e.target.value) if (!isNaN(parsed)) { updateValue(parsed) } else if (e.target.value === "" || e.target.value === "-") { if (!isControlled) { setInternalValue(0) } } } return (
) } ) NumberInput.displayName = "NumberInput" export { NumberInput }