diff --git a/packages/react-components/src/InputBalance.tsx b/packages/react-components/src/InputBalance.tsx index 32fd2157e1..35b86188f7 100644 --- a/packages/react-components/src/InputBalance.tsx +++ b/packages/react-components/src/InputBalance.tsx @@ -5,7 +5,7 @@ import { BareProps, BitLength } from './types'; import BN from 'bn.js'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import { BitLengthOption } from '@polkadot/react-components/constants'; import { formatBalance, isBn } from '@polkadot/util'; @@ -36,11 +36,15 @@ const DEFAULT_BITLENGTH = BitLengthOption.CHAIN_SPEC as BitLength; const TEN = new BN(10); function InputBalance ({ autoFocus, className, defaultValue: inDefault, help, isDisabled, isError, isFull, isZeroable, label, labelExtra, maxValue, onChange, onEnter, onEscape, placeholder, style, value, withEllipsis, withLabel, withMax }: Props): React.ReactElement { - const defaultValue = inDefault - ? isBn(inDefault) - ? inDefault.div(TEN.pow(new BN(formatBalance.getDefaults().decimals))).toString() - : formatBalance(inDefault, { forceUnit: '-', withSi: false }).replace(',', isDisabled ? ',' : '') - : inDefault; + const [defaultValue, setDefaultValue] = useState(); + + useEffect((): void => { + inDefault && setDefaultValue( + isBn(inDefault) + ? inDefault.div(TEN.pow(new BN(formatBalance.getDefaults().decimals))).toString() + : formatBalance(inDefault, { forceUnit: '-', withSi: false }).replace(',', isDisabled ? ',' : '') + ); + }, [inDefault, isDisabled]); return ( { - const defaultValue = new BN((value as BN || '0').toString()).toString(10); + const [defaultValue] = useState(new BN((value as BN || '0').toString()).toString(10)); + const _onChange = useCallback( (value?: BN): void => onChange && onChange({