InputNumber fix (#2669)

* InputNumber workaround for edit bug

* defaultValue robustness checks
This commit is contained in:
Jaco Greeff 2020-05-01 18:55:55 +02:00 committed by GitHub
parent e2aeb50921
commit 8cdb813255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 8 deletions

View File

@ -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<Props> {
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<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 ? ',' : '')
);
}, [inDefault, isDisabled]);
return (
<InputNumber

View File

@ -5,13 +5,14 @@
import { Props } from '../types';
import BN from 'bn.js';
import React, { useCallback } from 'react';
import React, { useCallback, useState } from 'react';
import { InputBalance } from '@polkadot/react-components';
import Bare from './Bare';
function Balance ({ className, defaultValue: { value }, isDisabled, isError, label, onChange, onEnter, onEscape, style, withLabel }: Props): React.ReactElement<Props> {
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({