import clsx from 'clsx'; import React, { useEffect, useState } from 'react'; import { FiMinus, FiPlus } from 'react-icons/fi'; import { useTranslation } from '@/hooks/useTranslation'; interface NumberInputProps { className?: string; inputClassName?: string; label: string; iconSize?: number; value: number; min: number; max: number; step?: number; disabled?: boolean; onChange: (value: number) => void; 'data-setting-id'?: string; } const NumberInput: React.FC = ({ className, inputClassName, label, iconSize, value, onChange, min, max, step, disabled, 'data-setting-id': settingId, }) => { const _ = useTranslation(); const [localValue, setLocalValue] = useState(value); const numberStep = step || 1; useEffect(() => { setLocalValue(value); }, [value]); const handleChange = (e: React.ChangeEvent) => { const value = e.target.value; // Allow empty string or valid numbers without leading zeros if (value === '' || /^[1-9]\d*\.?\d*$|^0?\.?\d*$/.test(value)) { const newValue = value === '' ? 0 : parseFloat(value); setLocalValue(newValue); if (!isNaN(newValue)) { const roundedValue = Math.round(newValue * 10) / 10; onChange(Math.max(min, Math.min(max, roundedValue))); } } }; const increment = () => { const newValue = Math.min(max, localValue + numberStep); const roundedValue = Math.round(newValue * 10) / 10; setLocalValue(roundedValue); onChange(roundedValue); }; const decrement = () => { const newValue = Math.max(min, localValue - numberStep); const roundedValue = Math.round(newValue * 10) / 10; setLocalValue(roundedValue); onChange(roundedValue); }; const handleOnBlur = () => { const newValue = Math.max(min, Math.min(max, localValue)); setLocalValue(newValue); onChange(newValue); }; return (
{label} {iconSize && }
e.target.select()} />
); }; export default NumberInput;