InputBalance default formatting with 3 decimals (#2766)
* InputBalance default formatting with 3 decimals * Cleanup reformatting
This commit is contained in:
parent
c38faec51e
commit
deeb8b11de
@ -34,15 +34,34 @@ interface Props extends BareProps {
|
||||
|
||||
const DEFAULT_BITLENGTH = BitLengthOption.CHAIN_SPEC as BitLength;
|
||||
const TEN = new BN(10);
|
||||
const THOUSAND = new BN(1000);
|
||||
|
||||
function reformat (value: string | BN, isDisabled?: boolean): string {
|
||||
if (isBn(value)) {
|
||||
let fmt = (value.mul(THOUSAND).div(TEN.pow(new BN(formatBalance.getDefaults().decimals))).toNumber() / 1000).toFixed(3);
|
||||
|
||||
while (fmt.length !== 1 && ['.', '0'].includes(fmt[fmt.length - 1])) {
|
||||
const isLast = fmt.endsWith('.');
|
||||
|
||||
fmt = fmt.substr(0, fmt.length - 1);
|
||||
|
||||
if (isLast) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return fmt;
|
||||
}
|
||||
|
||||
return formatBalance(value, { forceUnit: '-', withSi: false }).replace(',', isDisabled ? ',' : '');
|
||||
}
|
||||
|
||||
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<Props> {
|
||||
const [defaultValue, setDefaultValue] = useState<string | undefined>();
|
||||
|
||||
useEffect((): void => {
|
||||
inDefault && setDefaultValue(
|
||||
isBn(inDefault)
|
||||
? inDefault.div(TEN.pow(new BN(formatBalance.getDefaults().decimals))).toString()
|
||||
: formatBalance(inDefault, { forceUnit: '-', withSi: false }).replace(',', isDisabled ? ',' : '')
|
||||
reformat(inDefault, isDisabled)
|
||||
);
|
||||
}, [inDefault, isDisabled]);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user