"use client" import React, { ElementRef, forwardRef } from "react" import { Add, Remove } from "../../icons" import { disabledVariants, interactiveVariants } from "../../styles" import { classNames } from "../../utils" import { Input, InputProps } from "../Input" import { UnstyledButton } from "../UnstyledButton" export type QuantityInputProps = Omit< InputProps, "startEnhancer" | "endEnhancer" | "min" | "max" | "value" > & { min?: number max?: number value: number | undefined | null onValueChange: ( value: number | null, source: "increment" | "decrement" | "input", ) => void } export const QuantityInput = forwardRef, QuantityInputProps>( function QuantityInput( { className, disabled, min, max, onChange, onValueChange, value, ...rest }, ref, ) { const effectiveValue = value || 0 const isIncrementDisabled = disabled || Boolean(max !== undefined && effectiveValue >= max) const isDecrementDisabled = disabled || Boolean(min !== undefined && effectiveValue <= min) return ( { const newValue = effectiveValue + 1 onValueChange(Math.min(newValue, max || newValue), "increment") e.stopPropagation() }} > } max={max} min={min} ref={ref} startEnhancer={ { const newValue = effectiveValue - 1 onValueChange(Math.max(newValue, min || newValue), "decrement") e.stopPropagation() }} > } type="number" value={value ?? ""} onChange={e => { onValueChange(e.target.value ? Number(e.target.value) : null, "input") onChange?.(e) }} {...rest} /> ) }, )