import clsx from "clsx" import { useEffect, useState } from "react" import AmountField from "react-currency-input-field" import { currencies } from "../../../utils/currencies" import InputError from "../../atoms/input-error" import InputHeader from "../../fundamentals/input-header" type Props = { currencyCode: string value?: number | null onChange: (amount?: number | null) => void errors?: { [x: string]: unknown } name?: string label?: string } const AmountInput = ({ name, label, currencyCode, errors, value, onChange, }: Props) => { const { symbol_native, decimal_digits } = currencies[currencyCode.toUpperCase()] const getFormattedValue = (value: number) => { return `${value / 10 ** decimal_digits}` } const [formattedValue, setFormattedValue] = useState( value !== undefined && value !== null ? getFormattedValue(value) : undefined ) useEffect(() => { if (value) { setFormattedValue(getFormattedValue(value)) } }, [value, decimal_digits]) const onAmountChange = (value?: string, floatValue?: number | null) => { if (floatValue) { const numericalValue = Math.round(floatValue * 10 ** decimal_digits) onChange(numericalValue) } else { onChange(0) } setFormattedValue(value) } const step = 10 ** -decimal_digits return (
{label && }

{currencyCode.toUpperCase()}

onAmountChange(value, values?.float) } allowNegativeValue={false} placeholder="-" decimalScale={decimal_digits} className="remove-number-spinner leading-base text-grey-90 caret-violet-60 placeholder-grey-40 w-full bg-transparent text-right font-normal outline-none outline-0" />

{symbol_native}

) } export default AmountInput