import type { FC } from 'react' import { RiArrowDownSLine, RiArrowUpSLine } from '@remixicon/react' import Input, { type InputProps } from '../input' import classNames from '@/utils/classnames' export type InputNumberProps = { unit?: string value?: number onChange: (value?: number) => void amount?: number size?: 'sm' | 'md' max?: number min?: number defaultValue?: number } & Omit export const InputNumber: FC = (props) => { const { unit, className, onChange, amount = 1, value, size = 'md', max, min, defaultValue, ...rest } = props const isValidValue = (v: number) => { if (max && v > max) return false if (min && v < min) return false return true } const inc = () => { if (value === undefined) { onChange(defaultValue) return } const newValue = value + amount if (!isValidValue(newValue)) return onChange(newValue) } const dec = () => { if (value === undefined) { onChange(defaultValue) return } const newValue = value - amount if (!isValidValue(newValue)) return onChange(newValue) } return
{ if (e.target.value === '') onChange(undefined) const parsed = Number(e.target.value) if (Number.isNaN(parsed)) return if (!isValidValue(parsed)) return onChange(parsed) }} unit={unit} />
}