| 1 |
import { MinusCircleIcon } from '@heroicons/react/24/outline'; |
| 2 |
import { PlusCircleIcon } from '@heroicons/react/24/solid'; |
| 3 |
import { useState } from 'react'; |
| 4 |
|
| 5 |
const CustomNumberInput = ({ |
| 6 |
placeholder = 'Size', |
| 7 |
initialValue = 1, |
| 8 |
min = 1, |
| 9 |
max = 99, |
| 10 |
onChange, |
| 11 |
className = '', |
| 12 |
disabled = false, |
| 13 |
}) => { |
| 14 |
const [value, setValue] = useState(initialValue); |
| 15 |
|
| 16 |
const handleIncrement = () => { |
| 17 |
if (disabled) return; |
| 18 |
if (value < max) { |
| 19 |
const newValue = value + 1; |
| 20 |
setValue(newValue); |
| 21 |
onChange && onChange(newValue); |
| 22 |
} |
| 23 |
}; |
| 24 |
|
| 25 |
const handleDecrement = () => { |
| 26 |
if (disabled) return; |
| 27 |
if (value > min) { |
| 28 |
const newValue = value - 1; |
| 29 |
setValue(newValue); |
| 30 |
onChange && onChange(newValue); |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
return ( |
| 35 |
<div |
| 36 |
className={`disco-border disco-border-green-400 disco-bg-white disco-rounded-md disco-px-2 disco-flex disco-justify-between disco-items-center ${className} ${disabled ? 'disco-opacity-50 disco-cursor-not-allowed' : ''}`} |
| 37 |
> |
| 38 |
<span className="disco-text-black disco-text-sm"> |
| 39 |
{placeholder} |
| 40 |
</span> |
| 41 |
<div className="disco-flex disco-space-x-1"> |
| 42 |
<MinusCircleIcon |
| 43 |
className="disco-w-5 disco-h-5 disco-text-primary disco-text-sm" |
| 44 |
onClick={handleDecrement} |
| 45 |
/> |
| 46 |
<span className="disco-text-sm">{initialValue}</span> |
| 47 |
<PlusCircleIcon |
| 48 |
className="disco-w-5 disco-h-5 disco-text-primary" |
| 49 |
onClick={handleIncrement} |
| 50 |
/> |
| 51 |
</div> |
| 52 |
</div> |
| 53 |
); |
| 54 |
}; |
| 55 |
|
| 56 |
export default CustomNumberInput; |
| 57 |
|