'use client' import type { NumberInputProps } from 'rntwsc/components/input/number-input' import { NumberInput } from 'rntwsc/components/input/number-input' import { useIsRtl } from 'rntwsc/i18n/use-is-rtl' import { Minus } from 'rntwsc/icons/minus' import { Plus } from 'rntwsc/icons/plus' import { Pressable } from 'rntwsc/tw/components/pressable' import { useControllableState } from 'rntwsc/libs/hooks' type QuantityInputProps = NumberInputProps & { step?: number min?: number max?: number } export const QuantityInput = async ({ value, defaultValue, onChangeText, step = 1, min = 1, max = Infinity, ...props }: QuantityInputProps) => { const rtl = await useIsRtl() const [state, setState] = useControllableState({ value, defaultValue, onChange: onChangeText, }) const clamp = (val: number) => Math.max(min, Math.min(val, max)).toString() const onPress = (by: number) => { setState(prev => clamp(+prev + by)) } return ( { setState(prev => clamp(+prev)) props.onBlur?.(e) }} className={[ 'border-gray-200 text-center focus:border-gray-200', props.className, ]} prefix={className => ( onPress(-step)} > )} suffix={className => ( onPress(step)} > )} /> ) }