import React, { useEffect } from 'react' import classNames from 'classnames' import { FieldValues, UseFormRegister, UseFormReturn, UseFormSetValue, UseFormTrigger } from 'react-hook-form' import { TokenIcon } from '../Icons/TokenIcon' import { ThemedClipSpinner } from '../Loading/ThemedClipSpinner' import WalletIcon from '../../assets/Misc/icon-wallet.svg' import { Amount, i18nTranslate, Token } from '../../types' interface TokenAmountInputProps { inputKey: string form: UseFormReturn chainId: number token: Token balance: Amount validate?: { [key: string]: (value: string) => boolean | string } isWalletConnected: boolean i18nKey: string t: i18nTranslate className?: string widthClassName?: string bgClassName?: string autoComplete?: string } /** * For use in conjunction with react-hook-form. * @param props * @returns */ export const TokenAmountInput: React.FC = (props) => { const { className, widthClassName, bgClassName, form, inputKey, token, balance, isWalletConnected, chainId, t, validate, i18nKey, autoComplete } = props const { register, setValue, trigger } = form return (
) } TokenAmountInput.defaultProps = { widthClassName: 'w-full', bgClassName: 'bg-tertiary' } interface InputHeaderProps { inputKey: string balance: Amount token: Token isWalletConnected: boolean setValue: UseFormSetValue trigger: UseFormTrigger i18nKey?: string t: i18nTranslate } const InputHeader = (props: InputHeaderProps) => { const { inputKey, setValue, trigger, isWalletConnected, balance, token, i18nKey, t } = props // If the user input a larger amount than their wallet balance before connecting a wallet useEffect(() => { trigger(inputKey) }, [isWalletConnected, balance]) const isBalanceFetched = isWalletConnected && Boolean(balance) return (
{(i18nKey && t?.(i18nKey)) || 'Amount'} {isWalletConnected && ( )}
) } const InputToken = (props: { chainId: number; token: Token }) => { const { chainId, token } = props if (!token) { return null } return (
{token.symbol}
) } interface InputProps { autoComplete?: string inputKey: string register: UseFormRegister validate: { [key: string]: (value: string) => boolean | string } t: i18nTranslate } const Input = (props: InputProps) => { const { autoComplete, register, inputKey, validate, t } = props const pattern = { value: /^\d*\.?\d*$/, message: t?.('pleaseEnterAPositiveNumber') || 'Please enter a positive number' } return ( ) }