import type { Token } from '@lifi/sdk'; import type { CardProps } from '@mui/material'; import type { ChangeEvent, ReactNode } from 'react'; import { useLayoutEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { useToken } from '../../hooks/useToken.js'; import { useWidgetConfig } from '../../providers/WidgetProvider/WidgetProvider.js'; import { FormKeyHelper, type FormTypeProps } from '../../stores/form/types.js'; import { useFieldController } from '../../stores/form/useFieldController.js'; import { useFieldValues } from '../../stores/form/useFieldValues.js'; import { DisabledUI } from '../../types/widget.js'; import { formatInputAmount } from '../../utils/format.js'; import { fitInputText } from '../../utils/input.js'; import { CardTitle } from '../Card/CardTitle.js'; import { InputCard } from '../Card/InputCard.js'; import { FormContainer, FormControl, Input, maxInputFontSize, minInputFontSize, } from './AmountInput.style.js'; import { AmountInputEndAdornment } from './AmountInputEndAdornment.js'; import { AmountInputStartAdornment } from './AmountInputStartAdornment.js'; import { PriceFormHelperText } from './PriceFormHelperText.js'; export const AmountInput: React.FC = ({ formType, ...props }) => { const { disabledUI } = useWidgetConfig(); const [chainId, tokenAddress] = useFieldValues( FormKeyHelper.getChainKey(formType), FormKeyHelper.getTokenKey(formType), ); const { token } = useToken(chainId, tokenAddress); const disabled = disabledUI?.includes(DisabledUI.FromAmount); return ( : undefined } bottomAdornment={} disabled={disabled} {...props} /> ); }; export const AmountInputBase: React.FC< FormTypeProps & CardProps & { token?: Token; startAdornment?: ReactNode; endAdornment?: ReactNode; bottomAdornment?: ReactNode; disabled?: boolean; } > = ({ formType, token, startAdornment, endAdornment, bottomAdornment, disabled, ...props }) => { const { t } = useTranslation(); const ref = useRef(null); const amountKey = FormKeyHelper.getAmountKey(formType); const { onChange, onBlur, value } = useFieldController({ name: amountKey }); const handleChange = ( event: ChangeEvent, ) => { const { value } = event.target; const formattedAmount = formatInputAmount(value, token?.decimals, true); onChange(formattedAmount); }; const handleBlur = ( event: ChangeEvent, ) => { const { value } = event.target; const formattedAmount = formatInputAmount(value, token?.decimals); onChange(formattedAmount); onBlur(); }; useLayoutEffect(() => { if (ref.current) { fitInputText(maxInputFontSize, minInputFontSize, ref.current); } }, [value, ref]); return ( {t('header.send')} {bottomAdornment} ); };