Functional InputNumber with fixed bugs (#1820)
* Functional InputNumber * linting * linting * remove max button * linting * linting * version * version * combine values * fix * Re-enable SI dropdown * KSM formats * Formatting tweaks * Mini fixes * Adjust postfix opacity * Linting * More linting * Remove unneeded list override * Misc small adjustments * Vertical alignment
This commit is contained in:
parent
76e116a3ff
commit
7e65066d18
@ -84,7 +84,7 @@ function Transfer ({ className, onClose, recipientId: propRecipientId, senderId:
|
||||
}
|
||||
}, [amount, recipientId, senderId]);
|
||||
|
||||
const transferrable = <span className='label'>{t('transferrable ')}</span>;
|
||||
const transferrable = <span className='label'>{t('transferrable')}</span>;
|
||||
|
||||
return (
|
||||
<Modal
|
||||
|
||||
@ -10,7 +10,7 @@ import React, { useContext, useEffect, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { ApiContext } from '@polkadot/react-api';
|
||||
import { Button, Card } from '@polkadot/react-components';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import translate from './translate';
|
||||
import { addrToChecksum } from './util';
|
||||
@ -63,7 +63,7 @@ function Claim ({ button, className, ethereumAddress, t }: Props): React.ReactEl
|
||||
? (
|
||||
<>
|
||||
{t('has a valid claim for')}
|
||||
<h2>{formatBalance(claimValue)}</h2>
|
||||
<h2><FormatBalance value={claimValue} /></h2>
|
||||
<Button.Group>{button}</Button.Group>
|
||||
</>
|
||||
)
|
||||
|
||||
@ -14,7 +14,7 @@ import styled from 'styled-components';
|
||||
import CopyToClipboard from 'react-copy-to-clipboard';
|
||||
import { withApi, withMulti } from '@polkadot/react-api';
|
||||
import { Button, Card, Columar, Column, InputAddress, Tooltip } from '@polkadot/react-components';
|
||||
import { InputNumber } from '@polkadot/react-components/InputNumber';
|
||||
import { TokenUnit } from '@polkadot/react-components/InputNumber';
|
||||
import TxModal, { TxModalState, TxModalProps } from '@polkadot/react-components/TxModal';
|
||||
import { u8aToHex, u8aToString } from '@polkadot/util';
|
||||
import { decodeAddress } from '@polkadot/util-crypto';
|
||||
@ -112,7 +112,7 @@ class App extends TxModal<Props, State> {
|
||||
<main>
|
||||
<header />
|
||||
<h1>
|
||||
<Trans>claim your <em>{InputNumber.units}</em> tokens</Trans>
|
||||
<Trans>claim your <em>{TokenUnit.abbr}</em> tokens</Trans>
|
||||
</h1>
|
||||
<Columar>
|
||||
<Column>
|
||||
|
||||
@ -167,6 +167,7 @@ function Call (props: Props): React.ReactElement<Props> | null {
|
||||
help={t('The allotted value for this contract, i.e. the amount transferred to the contract as part of this call.')}
|
||||
isDisabled={isBusy}
|
||||
isError={!isEndowmentValid}
|
||||
isZeroable
|
||||
label={t('value')}
|
||||
onChange={_onChangeEndowment}
|
||||
value={endowment}
|
||||
|
||||
@ -21,7 +21,7 @@ import ContractModal, { ContractModalProps, ContractModalState } from './Modal';
|
||||
import Params from './Params';
|
||||
import store from './store';
|
||||
import translate from './translate';
|
||||
import { GAS_LIMIT } from './constants';
|
||||
import { ENDOWMENT, GAS_LIMIT } from './constants';
|
||||
|
||||
type ConstructOptions = { key: string; text: React.ReactNode; value: string }[];
|
||||
|
||||
@ -51,7 +51,7 @@ class Deploy extends ContractModal<Props, State> {
|
||||
...this.defaultState,
|
||||
constructorIndex: -1,
|
||||
constructOptions: [],
|
||||
endowment: new BN(0),
|
||||
endowment: new BN(ENDOWMENT),
|
||||
gasLimit: new BN(GAS_LIMIT),
|
||||
isHashValid: false,
|
||||
params: [],
|
||||
@ -182,7 +182,7 @@ class Deploy extends ContractModal<Props, State> {
|
||||
<Dropdown
|
||||
help={t('The deployment constructor information for this contract, as provided by the ABI.')}
|
||||
isDisabled={contractAbi.abi.contract.constructors.length <= 1}
|
||||
label={t('constructor ')}
|
||||
label={t('constructor')}
|
||||
onChange={this.onChangeConstructorIndex}
|
||||
options={constructOptions}
|
||||
style={{ fontFamily: 'monospace' }}
|
||||
|
||||
@ -29,7 +29,7 @@ function VoteValue ({ accountId, onChange, t }: Props): React.ReactElement<Props
|
||||
<InputBalance
|
||||
help={t('The amount that is associated with this vote. This value is is locked for the duration of the vote.')}
|
||||
label={t('vote value')}
|
||||
labelExtra={<BalanceVoting label={t('voting balance ')} params={accountId} />}
|
||||
labelExtra={<BalanceVoting label={<label>{t('voting balance')}</label>} params={accountId} />}
|
||||
onChange={_setVoteValue}
|
||||
/>
|
||||
);
|
||||
|
||||
@ -11,7 +11,7 @@ import React from 'react';
|
||||
import { Option, Tuple, Vec } from '@polkadot/types';
|
||||
import { ActionItem, InputAddress, Labelled, Static } from '@polkadot/react-components';
|
||||
import { withCalls, withMulti } from '@polkadot/react-api';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import translate from '../translate';
|
||||
import Seconding from './Seconding';
|
||||
@ -42,7 +42,7 @@ function renderProposal ({ democracy_depositOf, t }: Props): React.ReactNode {
|
||||
))}
|
||||
</Labelled>
|
||||
<Static label={t('balance')}>
|
||||
{formatBalance(balance)}
|
||||
<FormatBalance value={balance} />
|
||||
</Static>
|
||||
</div>
|
||||
);
|
||||
|
||||
@ -104,12 +104,12 @@ function Referendum ({ chain_bestNumber, className, democracy_enactmentPeriod, d
|
||||
values={[
|
||||
{
|
||||
colors: COLORS_AYE,
|
||||
label: `Aye, ${formatBalance(votedAye)} (${formatNumber(voteCountAye)})`,
|
||||
label: `Aye, ${formatBalance(votedAye, { forceUnit: '-' })} (${formatNumber(voteCountAye)})`,
|
||||
value: votedAye.muln(10000).div(votedTotal).toNumber() / 100
|
||||
},
|
||||
{
|
||||
colors: COLORS_NAY,
|
||||
label: `Nay, ${formatBalance(votedNay)} (${formatNumber(voteCountNay)})`,
|
||||
label: `Nay, ${formatBalance(votedNay, { forceUnit: '-' })} (${formatNumber(voteCountNay)})`,
|
||||
value: votedNay.muln(10000).div(votedTotal).toNumber() / 100
|
||||
}
|
||||
]}
|
||||
|
||||
@ -63,7 +63,7 @@ function Transfer ({ assets, className, onClose, recipientId: propRecipientId, s
|
||||
}
|
||||
};
|
||||
|
||||
const transferrable = <span className='label'>{t('transferrable ')}</span>;
|
||||
const transferrable = <span className='label'>{t('transferrable')}</span>;
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
||||
@ -100,7 +100,7 @@ class BondExtra extends TxComponent<Props, State> {
|
||||
private renderContent (): React.ReactNode {
|
||||
const { stashId, systemChain, t } = this.props;
|
||||
const { amountError, maxAdditional, maxBalance } = this.state;
|
||||
const transferrable = <span className='label'>{t('transferrable ')}</span>;
|
||||
const transferrable = <span className='label'>{t('transferrable')}</span>;
|
||||
const isUnsafeChain = detectUnsafe(systemChain);
|
||||
|
||||
return (
|
||||
|
||||
@ -11,7 +11,7 @@ import ApiPromise from '@polkadot/api/promise';
|
||||
import { isWeb3Injected, web3Accounts, web3Enable } from '@polkadot/extension-dapp';
|
||||
import defaults from '@polkadot/rpc-provider/defaults';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { InputNumber } from '@polkadot/react-components/InputNumber';
|
||||
import { TokenUnit } from '@polkadot/react-components/InputNumber';
|
||||
import keyring from '@polkadot/ui-keyring';
|
||||
import uiSettings from '@polkadot/ui-settings';
|
||||
import ApiSigner from '@polkadot/react-signer/ApiSigner';
|
||||
@ -152,7 +152,7 @@ export default class Api extends React.PureComponent<Props, State> {
|
||||
decimals: tokenDecimals,
|
||||
unit: tokenSymbol
|
||||
});
|
||||
InputNumber.setUnit(tokenSymbol);
|
||||
TokenUnit.setAbbr(tokenSymbol);
|
||||
|
||||
// finally load the keyring
|
||||
keyring.loadAll({
|
||||
|
||||
@ -12,6 +12,7 @@ import styled from 'styled-components';
|
||||
import { formatBalance, formatNumber, isObject } from '@polkadot/util';
|
||||
import { Icon, Tooltip, TxButton } from '@polkadot/react-components';
|
||||
import { withCalls, withMulti } from '@polkadot/react-api';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import CryptoType from './CryptoType';
|
||||
import Label from './Label';
|
||||
@ -165,7 +166,7 @@ function renderUnlocking ({ stakingInfo, t }: Props): React.ReactNode {
|
||||
|
||||
return (
|
||||
<div>
|
||||
{formatBalance(total)}
|
||||
<FormatBalance value={total} />
|
||||
<Icon
|
||||
name='info circle'
|
||||
data-tip
|
||||
@ -177,7 +178,7 @@ function renderUnlocking ({ stakingInfo, t }: Props): React.ReactNode {
|
||||
{t('{{value}}, {{remaining}} blocks left', {
|
||||
replace: {
|
||||
remaining: formatNumber(remainingBlocks),
|
||||
value: formatBalance(value)
|
||||
value: formatBalance(value, { forceUnit: '-' })
|
||||
}
|
||||
})}
|
||||
</div>
|
||||
@ -211,9 +212,10 @@ function renderValidatorPrefs ({ stakingInfo, t, withValidatorPrefs = false }: P
|
||||
{validatorPrefsDisplay.validatorPayment && stakingInfo.validatorPrefs.validatorPayment && (
|
||||
<>
|
||||
<Label label={t('commission')} />
|
||||
<div className='result'>{
|
||||
formatBalance(stakingInfo.validatorPrefs.validatorPayment)
|
||||
}</div>
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={stakingInfo.validatorPrefs.validatorPayment}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
@ -237,40 +239,61 @@ function renderBalances (props: Props): React.ReactNode {
|
||||
{balancesAll && balanceDisplay.total && (
|
||||
<>
|
||||
<Label label={t('total')} />
|
||||
<div className='result'>{formatBalance(balancesAll.votingBalance)}</div>
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={balancesAll.votingBalance}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{balancesAll && balanceDisplay.available && (
|
||||
<>
|
||||
<Label label={t('transferrable')} />
|
||||
<div className='result'>{formatBalance(balancesAll.availableBalance)}</div>
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={balancesAll.availableBalance}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{balancesAll && balanceDisplay.locked && balancesAll.lockedBalance && balancesAll.lockedBalance.gtn(0) && (
|
||||
<>
|
||||
<Label label={t('locked')} />
|
||||
<div className='result'>{formatBalance(balancesAll.lockedBalance)}</div>
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={balancesAll.lockedBalance}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{balancesAll && balanceDisplay.reserved && balancesAll.reservedBalance && balancesAll.reservedBalance.gtn(0) && (
|
||||
<>
|
||||
<Label label={t('reserved')} />
|
||||
<div className='result'>{formatBalance(balancesAll.reservedBalance)}</div>
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={balancesAll.reservedBalance}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{balanceDisplay.bonded && (ownBonded.gtn(0) || otherBonded.length !== 0) && (
|
||||
<>
|
||||
<Label label={t('bonded')} />
|
||||
<div className='result'>{formatBalance(ownBonded)}{otherBonded.length !== 0 && (
|
||||
` (+${otherBonded.map((bonded): string => formatBalance(bonded)).join(', ')})`
|
||||
)}</div>
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={ownBonded}
|
||||
>
|
||||
{otherBonded.length !== 0 && (
|
||||
<> (+{otherBonded.map((bonded, index): React.ReactNode =>
|
||||
<FormatBalance key={index} value={bonded} />
|
||||
)})</>
|
||||
)}
|
||||
</FormatBalance>
|
||||
</>
|
||||
)}
|
||||
{balanceDisplay.redeemable && stakingInfo && stakingInfo.redeemable && stakingInfo.redeemable.gtn(0) && (
|
||||
<>
|
||||
<Label label={t('redeemable')} />
|
||||
<div className='result'>
|
||||
{formatBalance(stakingInfo.redeemable)}
|
||||
<FormatBalance
|
||||
className='result'
|
||||
value={stakingInfo.redeemable}
|
||||
>
|
||||
{stakingInfo.controllerId && (
|
||||
<TxButton
|
||||
accountId={stakingInfo.controllerId.toString()}
|
||||
@ -284,7 +307,7 @@ function renderBalances (props: Props): React.ReactNode {
|
||||
tx='staking.withdrawUnbonded'
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</FormatBalance>
|
||||
</>
|
||||
)}
|
||||
{balanceDisplay.unlocking && stakingInfo && stakingInfo.unlocking && (
|
||||
@ -357,9 +380,10 @@ export default withMulti(
|
||||
opacity: 1;
|
||||
|
||||
label {
|
||||
grid-column: 1;
|
||||
grid-column: 1;
|
||||
padding-right: 0.5rem;
|
||||
text-align: right;
|
||||
vertical-align: middle;
|
||||
|
||||
.help.circle.icon {
|
||||
display: none;
|
||||
@ -367,7 +391,7 @@ export default withMulti(
|
||||
}
|
||||
|
||||
.result {
|
||||
grid-column: 2;
|
||||
grid-column: 2;
|
||||
|
||||
.icon {
|
||||
margin-left: .3em;
|
||||
|
||||
@ -114,6 +114,7 @@ export default styled(AddressMini)`
|
||||
.ui--AddressMini-balances {
|
||||
display: grid;
|
||||
|
||||
.ui--Balance,
|
||||
.ui--Bonded,
|
||||
.ui--LockedVote {
|
||||
font-size: 0.75rem;
|
||||
|
||||
@ -7,11 +7,16 @@ import { BareProps } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { Balance } from '@polkadot/react-query';
|
||||
import { Balance, FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import { classes } from './util';
|
||||
|
||||
export interface RenderProps extends BareProps {
|
||||
className?: string;
|
||||
label?: React.ReactNode;
|
||||
value?: BN | BN[];
|
||||
}
|
||||
|
||||
export interface Props extends BareProps {
|
||||
balance?: BN | BN[];
|
||||
label?: React.ReactNode;
|
||||
@ -19,25 +24,30 @@ export interface Props extends BareProps {
|
||||
withLabel?: boolean;
|
||||
}
|
||||
|
||||
function renderProvided ({ balance, className, label, style }: Props): React.ReactNode {
|
||||
let value = `${formatBalance(Array.isArray(balance) ? balance[0] : balance)}`;
|
||||
export function renderProvided ({ className, label, value }: RenderProps): React.ReactNode {
|
||||
let others: undefined | React.ReactNode;
|
||||
|
||||
if (Array.isArray(balance)) {
|
||||
const totals = balance.filter((_, index): boolean => index !== 0);
|
||||
const total = totals.reduce((total, value): BN => total.add(value), new BN(0)).gtn(0)
|
||||
? `(+${totals.map((balance): string => formatBalance(balance)).join(', ')})`
|
||||
: '';
|
||||
if (Array.isArray(value)) {
|
||||
const totals = value.filter((_, index): boolean => index !== 0);
|
||||
const total = totals.reduce((total, value): BN => total.add(value), new BN(0)).gtn(0);
|
||||
|
||||
value = `${value} ${total}`;
|
||||
if (total) {
|
||||
others = totals.map((balance, index): React.ReactNode =>
|
||||
<FormatBalance key={index} value={balance} />
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
<FormatBalance
|
||||
className={classes('ui--Balance', className)}
|
||||
style={style}
|
||||
label={label}
|
||||
value={Array.isArray(value) ? value[0] : value}
|
||||
>
|
||||
{label}{value}
|
||||
</div>
|
||||
{others && (
|
||||
<span> (+{others})</span>
|
||||
)}
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
@ -51,7 +61,7 @@ export default function BalanceDisplay (props: Props): React.ReactElement<Props>
|
||||
return balance
|
||||
? (
|
||||
<>
|
||||
{renderProvided(props)}
|
||||
{renderProvided({ className, label, value: balance })}
|
||||
</>
|
||||
)
|
||||
: (
|
||||
|
||||
@ -7,9 +7,9 @@ import { BareProps } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { Bonded } from '@polkadot/react-query';
|
||||
|
||||
import { renderProvided } from './Balance';
|
||||
import { classes } from './util';
|
||||
|
||||
export interface Props extends BareProps {
|
||||
@ -19,28 +19,6 @@ export interface Props extends BareProps {
|
||||
withLabel?: boolean;
|
||||
}
|
||||
|
||||
function renderProvided ({ bonded, className, label, style }: Props): React.ReactNode {
|
||||
let value = `${formatBalance(Array.isArray(bonded) ? bonded[0] : bonded)}`;
|
||||
|
||||
if (Array.isArray(bonded)) {
|
||||
const totals = bonded.filter((_, index): boolean => index !== 0);
|
||||
const total = totals.reduce((total, value): BN => total.add(value), new BN(0)).gtn(0)
|
||||
? `(+${totals.map((bonded): string => formatBalance(bonded)).join(', ')})`
|
||||
: '';
|
||||
|
||||
value = `${value} ${total}`;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classes('ui--Bonded', className)}
|
||||
style={style}
|
||||
>
|
||||
{label}{value}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function BondedDisplay (props: Props): React.ReactElement<Props> | null {
|
||||
const { bonded, params, className, label, style } = props;
|
||||
|
||||
@ -51,7 +29,7 @@ export default function BondedDisplay (props: Props): React.ReactElement<Props>
|
||||
return bonded
|
||||
? (
|
||||
<>
|
||||
{renderProvided(props)}
|
||||
{renderProvided({ className, label, value: bonded })}
|
||||
</>
|
||||
)
|
||||
: (
|
||||
|
||||
@ -10,7 +10,7 @@ import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { GenericCall, getTypeDef } from '@polkadot/types';
|
||||
import Params from '@polkadot/react-params';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import Static from './Static';
|
||||
import { classes } from './util';
|
||||
@ -64,7 +64,7 @@ function Call ({ children, className, style, mortality, tip, value, withHash, t
|
||||
className='tip'
|
||||
label={t('tip')}
|
||||
>
|
||||
{formatBalance(tip)}
|
||||
<FormatBalance value={tip} />
|
||||
</Static>
|
||||
)}
|
||||
<Params
|
||||
|
||||
@ -9,6 +9,7 @@ import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { BitLengthOption } from '@polkadot/react-components/constants';
|
||||
import { InputNumber } from '@polkadot/react-components';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
interface Props extends BareProps {
|
||||
autoFocus?: boolean;
|
||||
@ -16,6 +17,7 @@ interface Props extends BareProps {
|
||||
help?: React.ReactNode;
|
||||
isDisabled?: boolean;
|
||||
isError?: boolean;
|
||||
isZeroable?: boolean;
|
||||
label?: React.ReactNode;
|
||||
labelExtra?: React.ReactNode;
|
||||
maxValue?: BN;
|
||||
@ -30,16 +32,17 @@ interface Props extends BareProps {
|
||||
|
||||
const DEFAULT_BITLENGTH = BitLengthOption.CHAIN_SPEC as BitLength;
|
||||
|
||||
function InputBalance ({ autoFocus, className, defaultValue, help, isDisabled, isError, label, labelExtra, maxValue, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax }: Props): React.ReactElement<Props> {
|
||||
function InputBalance ({ autoFocus, className, defaultValue, help, isDisabled, isError, isZeroable, label, labelExtra, maxValue, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<InputNumber
|
||||
autoFocus={autoFocus}
|
||||
className={`ui--InputBalance ${className}`}
|
||||
bitLength={DEFAULT_BITLENGTH}
|
||||
defaultValue={defaultValue}
|
||||
defaultValue={defaultValue ? formatBalance(defaultValue, { forceUnit: '-', withSi: false }) : undefined}
|
||||
help={help}
|
||||
isDisabled={isDisabled}
|
||||
isError={isError}
|
||||
isZeroable={isZeroable}
|
||||
isSi
|
||||
label={label}
|
||||
labelExtra={labelExtra}
|
||||
@ -60,4 +63,9 @@ export default styled(InputBalance)`
|
||||
&&:not(.label-small) .labelExtra {
|
||||
right: 6.5rem;
|
||||
}
|
||||
|
||||
.ui.action.input.ui--Input .ui.primary.buttons .ui.disabled.button.compact.floating.selection.dropdown.ui--SiDropdown {
|
||||
border-style: solid;
|
||||
opacity: 1 !important;
|
||||
}
|
||||
`;
|
||||
|
||||
@ -28,6 +28,7 @@ interface Props extends BareProps, ApiProps {
|
||||
help?: React.ReactNode;
|
||||
isDisabled?: boolean;
|
||||
isError?: boolean;
|
||||
isZeroable?: boolean;
|
||||
label?: any;
|
||||
onChange?: (value?: BN) => void;
|
||||
onEnter?: () => void;
|
||||
@ -60,7 +61,7 @@ class InputBalanceBonded extends React.PureComponent<Props, State> {
|
||||
}
|
||||
|
||||
public render (): React.ReactNode {
|
||||
const { autoFocus, className, defaultValue, help, isDisabled, isError, label, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax } = this.props;
|
||||
const { autoFocus, className, defaultValue, help, isDisabled, isError, isZeroable, label, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax } = this.props;
|
||||
const { maxBalance } = this.state;
|
||||
|
||||
return (
|
||||
@ -73,6 +74,7 @@ class InputBalanceBonded extends React.PureComponent<Props, State> {
|
||||
isDisabled={isDisabled}
|
||||
isError={isError}
|
||||
isSi
|
||||
isZeroable={isZeroable}
|
||||
label={label}
|
||||
maxValue={maxBalance}
|
||||
onChange={onChange}
|
||||
|
||||
@ -2,20 +2,21 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { SiDef } from '@polkadot/util/types';
|
||||
import { BareProps, BitLength, I18nProps } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { formatBalance, isUndefined } from '@polkadot/util';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
import { classes } from './util';
|
||||
import { BitLengthOption } from './constants';
|
||||
import Button from './Button';
|
||||
// import Button from './Button';
|
||||
import Dropdown from './Dropdown';
|
||||
import Input, { KEYS, KEYS_PRE } from './Input';
|
||||
import translate from './translate';
|
||||
|
||||
const ALLOW_MAX = false;
|
||||
// const ALLOW_MAX = false;
|
||||
|
||||
interface Props extends BareProps, I18nProps {
|
||||
autoFocus?: boolean;
|
||||
@ -26,6 +27,7 @@ interface Props extends BareProps, I18nProps {
|
||||
isError?: boolean;
|
||||
isSi?: boolean;
|
||||
isDecimal?: boolean;
|
||||
isZeroable?: boolean;
|
||||
label?: React.ReactNode;
|
||||
labelExtra?: React.ReactNode;
|
||||
maxLength?: number;
|
||||
@ -39,182 +41,182 @@ interface Props extends BareProps, I18nProps {
|
||||
withMax?: boolean;
|
||||
}
|
||||
|
||||
interface State {
|
||||
isPreKeyDown: boolean;
|
||||
isValid: boolean;
|
||||
siOptions: { value: string; text: string }[];
|
||||
siUnit: string;
|
||||
value: string;
|
||||
valueBN: BN;
|
||||
}
|
||||
|
||||
const DEFAULT_BITLENGTH = BitLengthOption.NORMAL_NUMBERS as BitLength;
|
||||
const ZERO = new BN(0);
|
||||
const TEN = new BN(10);
|
||||
|
||||
class InputNumber extends React.PureComponent<Props, State> {
|
||||
constructor (props: Props) {
|
||||
super(props);
|
||||
export class TokenUnit {
|
||||
public static abbr = 'Unit';
|
||||
|
||||
const { defaultValue, isSi, value } = this.props;
|
||||
const valueBN = new BN(value || 0);
|
||||
const si = formatBalance.findSi('-');
|
||||
public static setAbbr (abbr: string = TokenUnit.abbr): void {
|
||||
TokenUnit.abbr = abbr;
|
||||
}
|
||||
}
|
||||
|
||||
this.state = {
|
||||
value: isSi
|
||||
? new BN(defaultValue || valueBN).div(new BN(10).pow(new BN(si.power))).toString()
|
||||
: (defaultValue || valueBN).toString(),
|
||||
isPreKeyDown: false,
|
||||
isValid: !isUndefined(value),
|
||||
siOptions: formatBalance.getOptions().map(({ power, text, value }): { text: string; value: string } => ({
|
||||
value,
|
||||
text: power === 0
|
||||
? InputNumber.units
|
||||
: text
|
||||
})),
|
||||
siUnit: si.value,
|
||||
valueBN
|
||||
};
|
||||
function getGlobalMaxValue (bitLength?: number): BN {
|
||||
return new BN(2).pow(new BN(bitLength || DEFAULT_BITLENGTH)).subn(1);
|
||||
}
|
||||
|
||||
function getRegex (isDecimal: boolean): RegExp {
|
||||
return new RegExp(
|
||||
isDecimal
|
||||
? `^(0|[1-9]\\d*)(\\${KEYS.DECIMAL}\\d*)?$`
|
||||
: '^(0|[1-9]\\d*)$'
|
||||
);
|
||||
}
|
||||
|
||||
function getSiOptions (): { text: string; value: string }[] {
|
||||
return formatBalance.getOptions()
|
||||
.map(({ power, text, value }): { text: string; value: string } => ({
|
||||
value,
|
||||
text: power === 0
|
||||
? TokenUnit.abbr
|
||||
: text
|
||||
}));
|
||||
}
|
||||
|
||||
function getSiPowers (si: SiDef | null): [BN, number, number] {
|
||||
if (!si) {
|
||||
return [ZERO, 0, 0];
|
||||
}
|
||||
|
||||
public static units = 'Unit';
|
||||
const basePower = formatBalance.getDefaults().decimals;
|
||||
|
||||
public static setUnit (units: string = InputNumber.units): void {
|
||||
InputNumber.units = units;
|
||||
return [new BN(basePower + si.power), basePower, si.power];
|
||||
}
|
||||
|
||||
function isValidNumber (bn: BN, { bitLength = DEFAULT_BITLENGTH, isZeroable, maxValue }: Props): boolean {
|
||||
if (
|
||||
bn.lt(ZERO) ||
|
||||
!bn.lt(getGlobalMaxValue(bitLength)) ||
|
||||
(!isZeroable && bn.eq(ZERO)) ||
|
||||
bn.bitLength() > (bitLength || DEFAULT_BITLENGTH) ||
|
||||
(maxValue && maxValue.gtn(0) && bn.gt(maxValue))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static getDerivedStateFromProps ({ isDisabled, isSi, defaultValue = '0' }: Props): Partial<State> | null {
|
||||
if (!isDisabled || !isSi) {
|
||||
return null;
|
||||
return true;
|
||||
}
|
||||
|
||||
function inputToBn (input: string, si: SiDef | null, props: Props): [BN, boolean] {
|
||||
const [siPower, basePower, siUnitPower] = getSiPowers(si);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
|
||||
const isDecimalValue = input.match(/^(\d+)\.(\d+)$/);
|
||||
|
||||
let result;
|
||||
|
||||
if (isDecimalValue) {
|
||||
if (siUnitPower - isDecimalValue[2].length < -basePower) {
|
||||
result = new BN(-1);
|
||||
}
|
||||
|
||||
return {
|
||||
value: formatBalance(defaultValue, false),
|
||||
siUnit: formatBalance.calcSi(defaultValue.toString(), formatBalance.getDefaults().decimals).value
|
||||
};
|
||||
const div = new BN(input.replace(/\.\d*$/, ''));
|
||||
const modString = input.replace(/^\d+\./, '');
|
||||
const mod = new BN(modString);
|
||||
|
||||
result = div
|
||||
.mul(TEN.pow(siPower))
|
||||
.add(mod.mul(TEN.pow(new BN(basePower + siUnitPower - modString.length))));
|
||||
} else {
|
||||
result = new BN(input.replace(/[^\d]/g, ''))
|
||||
.mul(TEN.pow(siPower));
|
||||
}
|
||||
|
||||
public render (): React.ReactNode {
|
||||
const { bitLength = DEFAULT_BITLENGTH, className, help, isSi, isDisabled, isError = false, maxLength, maxValue, onEnter, placeholder, style, withMax, t } = this.props;
|
||||
const { isValid, value } = this.state;
|
||||
const maxValueLength = this.maxValue(bitLength).toString().length - 1;
|
||||
return [
|
||||
result,
|
||||
isValidNumber(result, props)
|
||||
];
|
||||
}
|
||||
|
||||
return (
|
||||
<Input
|
||||
{...this.props}
|
||||
className={classes('ui--InputNumber', className)}
|
||||
help={help}
|
||||
isAction={isSi}
|
||||
isDisabled={isDisabled}
|
||||
isError={!isValid || isError}
|
||||
maxLength={maxLength || maxValueLength}
|
||||
onChange={this.onChange}
|
||||
onEnter={onEnter}
|
||||
onKeyDown={this.onKeyDown}
|
||||
onKeyUp={this.onKeyUp}
|
||||
onPaste={this.onPaste}
|
||||
placeholder={placeholder || t('Positive number')}
|
||||
style={style}
|
||||
type='text'
|
||||
value={value}
|
||||
>
|
||||
{(ALLOW_MAX && withMax && !!maxValue) && this.renderMaxButton()}
|
||||
{isSi && this.renderSiDropdown()}
|
||||
</Input>
|
||||
);
|
||||
}
|
||||
// function bnToInput (bn: BN, si: SiDef | null): string {
|
||||
// const [siPower] = getSiPowers(si);
|
||||
//
|
||||
// const base = TEN.pow(siPower);
|
||||
// const div = bn.div(base);
|
||||
// const mod = bn.mod(base);
|
||||
//
|
||||
// return `${
|
||||
// div.gt(ZERO) ? div.toString() : '0'
|
||||
// }${
|
||||
// mod.gt(ZERO)
|
||||
// ? ((): string => {
|
||||
// const padding = Math.max(
|
||||
// mod.toString().length,
|
||||
// base.toString().length - div.toString().length,
|
||||
// bn.toString().length - div.toString().length
|
||||
// );
|
||||
//
|
||||
// return `.${mod.toString(10, padding).replace(/0*$/, '')}`;
|
||||
// })()
|
||||
// : ''
|
||||
// }`;
|
||||
// }
|
||||
|
||||
private renderSiDropdown (): React.ReactNode {
|
||||
const { siOptions, siUnit } = this.state;
|
||||
function getValuesFromString (value: string, si: SiDef | null, props: Props): [string, BN, boolean] {
|
||||
const [valueBn, isValid] = inputToBn(value, si, props);
|
||||
|
||||
return (
|
||||
<Dropdown
|
||||
dropdownClassName='ui--SiDropdown'
|
||||
isButton
|
||||
defaultValue={siUnit}
|
||||
onChange={this.selectSiUnit}
|
||||
options={siOptions}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return [
|
||||
value,
|
||||
valueBn,
|
||||
isValid
|
||||
];
|
||||
}
|
||||
|
||||
private renderMaxButton (): React.ReactNode {
|
||||
const { valueBN } = this.state;
|
||||
const { maxValue } = this.props;
|
||||
function getValuesFromBn (valueBn: BN, si: SiDef | null): [string, BN, boolean] {
|
||||
const value = si
|
||||
? valueBn.div(TEN.pow(new BN(si.power))).toString()
|
||||
: valueBn.toString();
|
||||
|
||||
if (!maxValue || valueBN.eq(maxValue)) {
|
||||
return;
|
||||
return [
|
||||
value,
|
||||
valueBn,
|
||||
true
|
||||
];
|
||||
}
|
||||
|
||||
function getValues (value: BN | string, si: SiDef | null, props: Props): [string, BN, boolean] {
|
||||
return BN.isBN(value)
|
||||
? getValuesFromBn(value, si)
|
||||
: getValuesFromString(value, si, props);
|
||||
}
|
||||
|
||||
function isNewPropsValue (propsValue: BN | string, value: string, valueBn: BN): boolean {
|
||||
return BN.isBN(propsValue) ? !propsValue.eq(valueBn) : propsValue !== value;
|
||||
}
|
||||
|
||||
function InputNumber (props: Props): React.ReactElement<Props> {
|
||||
const { bitLength = DEFAULT_BITLENGTH, className, defaultValue = ZERO, help, isDecimal, isSi, isDisabled, isError = false, maxLength, maxValue, onChange, onEnter, placeholder, style, value: propsValue, t } = props;
|
||||
|
||||
const [si, setSi] = useState<SiDef | null>(isSi ? formatBalance.findSi('-') : null);
|
||||
const [isPreKeyDown, setIsPreKeyDown] = useState(false);
|
||||
|
||||
const [[value, valueBn, isValid], setValues] = useState<[string, BN, boolean]>(
|
||||
getValues(propsValue || defaultValue, si, props)
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
if (!!propsValue && isNewPropsValue(propsValue, value, valueBn)) {
|
||||
setValues(getValues(propsValue, si, props));
|
||||
}
|
||||
}, [propsValue]);
|
||||
|
||||
return (
|
||||
<Button
|
||||
className='ui--MaxButton'
|
||||
icon=''
|
||||
onClick={this.setToMaxValue}
|
||||
>
|
||||
Max
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
useEffect((): void => {
|
||||
setValues(getValues(value, si, props));
|
||||
}, [value, si, bitLength, maxValue]);
|
||||
|
||||
private maxValue (bitLength?: number): BN {
|
||||
return new BN(2).pow(new BN(bitLength || DEFAULT_BITLENGTH)).subn(1);
|
||||
}
|
||||
useEffect((): void => {
|
||||
onChange && isValid && onChange(valueBn);
|
||||
}, [isValid, valueBn]);
|
||||
|
||||
private isValidBitLength (value: BN, bitLength?: number): boolean {
|
||||
return value.bitLength() <= (bitLength || DEFAULT_BITLENGTH);
|
||||
}
|
||||
|
||||
private isValidNumber (input: BN, bitLength: number = DEFAULT_BITLENGTH): boolean {
|
||||
const { maxValue, withMax } = this.props;
|
||||
const maxBN = this.maxValue(bitLength);
|
||||
|
||||
if (
|
||||
input.lt(new BN(0)) ||
|
||||
!input.lt(maxBN) ||
|
||||
!this.isValidBitLength(input, bitLength) ||
|
||||
(withMax && maxValue !== undefined && maxValue.gtn(0) && input.gt(maxValue))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private regex = (): RegExp => {
|
||||
const { isDecimal, isSi } = this.props;
|
||||
return new RegExp(
|
||||
(isSi || isDecimal)
|
||||
? `^(0|[1-9]\\d*)(\\${KEYS.DECIMAL}\\d*)?$`
|
||||
: '^(0|[1-9]\\d*)$'
|
||||
);
|
||||
}
|
||||
|
||||
private onChange = (value: string): void => {
|
||||
const { bitLength, onChange } = this.props;
|
||||
const { siUnit } = this.state;
|
||||
|
||||
try {
|
||||
const valueBN = this.inputValueToBn(value, siUnit);
|
||||
const isValid = this.isValidNumber(valueBN, bitLength);
|
||||
|
||||
this.setState({ isValid, value, valueBN });
|
||||
|
||||
onChange && onChange(
|
||||
isValid
|
||||
? valueBN
|
||||
: undefined
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
private onKeyDown = (event: React.KeyboardEvent<Element>): void => {
|
||||
const { isPreKeyDown } = this.state;
|
||||
const _onChange = (input: string): void => {
|
||||
setValues(getValuesFromString(input, si, props));
|
||||
};
|
||||
|
||||
const _onKeyDown = (event: React.KeyboardEvent<Element>): void => {
|
||||
if (KEYS_PRE.includes(event.key)) {
|
||||
this.setState({ isPreKeyDown: true });
|
||||
setIsPreKeyDown(true);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -222,131 +224,76 @@ class InputNumber extends React.PureComponent<Props, State> {
|
||||
const { selectionStart: i, selectionEnd: j, value } = event.target as HTMLInputElement;
|
||||
const newValue = `${value.substring(0, i || 0)}${event.key}${value.substring(j || 0)}`;
|
||||
|
||||
if (!this.regex().test(newValue)) {
|
||||
if (!getRegex(isDecimal || !!si).test(newValue)) {
|
||||
console.log(newValue, getRegex(isDecimal || !!si));
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private onKeyUp = (event: React.KeyboardEvent<Element>): void => {
|
||||
const _onKeyUp = (event: React.KeyboardEvent<Element>): void => {
|
||||
if (KEYS_PRE.includes(event.key)) {
|
||||
this.setState({ isPreKeyDown: false });
|
||||
setIsPreKeyDown(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private onPaste = (event: React.ClipboardEvent<Element>): void => {
|
||||
const _onPaste = (event: React.ClipboardEvent<Element>): void => {
|
||||
const { value: newValue } = event.target as HTMLInputElement;
|
||||
|
||||
if (!this.regex().test(newValue)) {
|
||||
if (!getRegex(isDecimal || !!si).test(newValue)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private selectSiUnit = (siUnit: string): void => {
|
||||
this.setState((prevState: State): Pick<State, never> => {
|
||||
const { bitLength, onChange } = this.props;
|
||||
const valueBN = this.inputValueToBn(prevState.value, siUnit);
|
||||
const isValid = this.isValidNumber(valueBN, bitLength);
|
||||
const _onSelectSiUnit = (siUnit: string): void => {
|
||||
setSi(formatBalance.findSi(siUnit));
|
||||
};
|
||||
|
||||
onChange && onChange(
|
||||
isValid
|
||||
? prevState.valueBN
|
||||
: undefined
|
||||
);
|
||||
// const _onClickMaxButton = (): void => {
|
||||
// !!maxValue && setValue(bnToInput(maxValue, si));
|
||||
// };
|
||||
|
||||
return {
|
||||
isValid,
|
||||
siUnit,
|
||||
valueBN
|
||||
};
|
||||
});
|
||||
}
|
||||
const maxValueLength = getGlobalMaxValue(bitLength).toString().length - 1;
|
||||
|
||||
private setToMaxValue = (): void => {
|
||||
this.setState((prevState: State): Pick<State, never> => {
|
||||
const { bitLength, maxValue, onChange } = this.props;
|
||||
const { siUnit = prevState.siUnit } = this.state;
|
||||
|
||||
if (!maxValue) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const isValid = this.isValidNumber(maxValue, bitLength);
|
||||
|
||||
onChange && onChange(
|
||||
isValid
|
||||
? maxValue
|
||||
: undefined
|
||||
);
|
||||
|
||||
return {
|
||||
value: this.bnToInputValue(maxValue, siUnit),
|
||||
valueBN: maxValue,
|
||||
isValid
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private inputValueToBn = (value: string, siUnit?: string): BN => {
|
||||
const [siPower, basePower, siUnitPower] = this.getSiPowers(siUnit);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
|
||||
const isDecimalValue = value.match(/^(\d+)\.(\d+)$/);
|
||||
|
||||
if (isDecimalValue) {
|
||||
if (siUnitPower - isDecimalValue[2].length < -basePower) {
|
||||
return new BN(-1);
|
||||
}
|
||||
|
||||
const div = new BN(value.replace(/\.\d*$/, ''));
|
||||
const modString = value.replace(/^\d+\./, '');
|
||||
const mod = new BN(modString);
|
||||
|
||||
return div
|
||||
.mul(TEN.pow(siPower))
|
||||
.add(mod.mul(TEN.pow(new BN(basePower + siUnitPower - modString.length))));
|
||||
} else {
|
||||
return new BN(value.replace(/[^\d]/g, ''))
|
||||
.mul(TEN.pow(siPower));
|
||||
}
|
||||
}
|
||||
|
||||
private bnToInputValue = (bn: BN, siUnit?: string): string => {
|
||||
const [siPower] = this.getSiPowers(siUnit);
|
||||
|
||||
const base = TEN.pow(siPower);
|
||||
const div = bn.div(base);
|
||||
const mod = bn.mod(base);
|
||||
|
||||
return `${
|
||||
div.gt(ZERO) ? div.toString() : '0'
|
||||
}${
|
||||
mod.gt(ZERO)
|
||||
? ((): string => {
|
||||
const padding = Math.max(
|
||||
mod.toString().length,
|
||||
base.toString().length - div.toString().length,
|
||||
bn.toString().length - div.toString().length
|
||||
);
|
||||
|
||||
return `.${mod.toString(10, padding).replace(/0*$/, '')}`;
|
||||
})()
|
||||
: ''
|
||||
}`;
|
||||
}
|
||||
|
||||
private getSiPowers = (siUnit = this.state.siUnit): [BN, number, number] => {
|
||||
const { isSi } = this.props;
|
||||
|
||||
const basePower = isSi ? formatBalance.getDefaults().decimals : 0;
|
||||
const siUnitPower = isSi ? formatBalance.findSi(siUnit).power : 0;
|
||||
|
||||
return [new BN(basePower + siUnitPower), basePower, siUnitPower];
|
||||
}
|
||||
return (
|
||||
<Input
|
||||
{...props}
|
||||
className={classes('ui--InputNumber', className)}
|
||||
help={help}
|
||||
isAction={isSi}
|
||||
isDisabled={isDisabled}
|
||||
isError={!isValid || isError}
|
||||
maxLength={maxLength || maxValueLength}
|
||||
onChange={_onChange}
|
||||
onEnter={onEnter}
|
||||
onKeyDown={_onKeyDown}
|
||||
onKeyUp={_onKeyUp}
|
||||
onPaste={_onPaste}
|
||||
placeholder={placeholder || t('Positive number')}
|
||||
style={style}
|
||||
type='text'
|
||||
value={value}
|
||||
>
|
||||
{/* (ALLOW_MAX && withMax && !!maxValue && valueBn.lt(maxValue)) && (
|
||||
<Button
|
||||
className='ui--MaxButton'
|
||||
icon=''
|
||||
onClick={_onClickMaxButton}
|
||||
>
|
||||
{t('Max')}
|
||||
</Button>
|
||||
) */}
|
||||
{!!si && (
|
||||
<Dropdown
|
||||
dropdownClassName='ui--SiDropdown'
|
||||
isButton
|
||||
defaultValue={si.value}
|
||||
onChange={_onSelectSiUnit}
|
||||
options={getSiOptions()}
|
||||
/>
|
||||
)}
|
||||
</Input>
|
||||
);
|
||||
}
|
||||
|
||||
export {
|
||||
InputNumber
|
||||
};
|
||||
|
||||
export default translate(InputNumber);
|
||||
|
||||
@ -77,6 +77,7 @@ export const styles = `
|
||||
label,
|
||||
.result {
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ import React, { useContext, useEffect, useState } from 'react';
|
||||
import { InputAddress, Labelled, Static } from '@polkadot/react-components';
|
||||
import { ApiContext } from '@polkadot/react-api';
|
||||
import { Option } from '@polkadot/types';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
|
||||
import Inset, { InsetProps } from './Inset';
|
||||
import translate from './translate';
|
||||
@ -65,10 +65,10 @@ function TreasuryProposal ({ className, asInset, insetProps, onClick, proposal,
|
||||
/>
|
||||
</Labelled>
|
||||
<Static label={t('value')}>
|
||||
{formatBalance(value)}
|
||||
<FormatBalance value={value} />
|
||||
</Static>
|
||||
<Static label={t('bond')}>
|
||||
{formatBalance(bond)}
|
||||
<FormatBalance value={bond} />
|
||||
</Static>
|
||||
</>
|
||||
);
|
||||
|
||||
@ -8,9 +8,10 @@ import { DerivedBalances } from '@polkadot/api-derive/types';
|
||||
import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
|
||||
import React from 'react';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
|
||||
import FormatBalance from './FormatBalance';
|
||||
|
||||
interface Props extends BareProps, CallProps {
|
||||
balances_all?: DerivedBalances;
|
||||
children?: React.ReactNode;
|
||||
@ -18,15 +19,15 @@ interface Props extends BareProps, CallProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
function AvailableDisplay ({ balances_all, children, className, label = '' }: Props): React.ReactElement<Props> {
|
||||
function AvailableDisplay ({ balances_all, children, className, label }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<div className={className}>
|
||||
{label}{
|
||||
balances_all
|
||||
? formatBalance(balances_all.availableBalance)
|
||||
: '0'
|
||||
}{children}
|
||||
</div>
|
||||
<FormatBalance
|
||||
className={className}
|
||||
label={label}
|
||||
value={balances_all && balances_all.availableBalance}
|
||||
>
|
||||
{children}
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,8 @@ import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
import React from 'react';
|
||||
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
import FormatBalance from './FormatBalance';
|
||||
|
||||
interface Props extends BareProps, CallProps {
|
||||
children?: React.ReactNode;
|
||||
@ -19,15 +20,15 @@ interface Props extends BareProps, CallProps {
|
||||
balances_all?: DerivedBalances;
|
||||
}
|
||||
|
||||
export function BalanceDisplay ({ children, className, label = '', balances_all }: Props): React.ReactElement<Props> {
|
||||
export function BalanceDisplay ({ children, className, label, balances_all }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<div className={className}>
|
||||
{label}{
|
||||
balances_all
|
||||
? formatBalance(balances_all.freeBalance)
|
||||
: '-'
|
||||
}{children}
|
||||
</div>
|
||||
<FormatBalance
|
||||
className={className}
|
||||
label={label}
|
||||
value={balances_all && balances_all.freeBalance}
|
||||
>
|
||||
{children}
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -8,9 +8,10 @@ import { DerivedBalances } from '@polkadot/api-derive/types';
|
||||
import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
|
||||
import React from 'react';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
|
||||
import FormatBalance from './FormatBalance';
|
||||
|
||||
interface Props extends BareProps, CallProps {
|
||||
balances_all?: DerivedBalances;
|
||||
children?: React.ReactNode;
|
||||
@ -18,15 +19,15 @@ interface Props extends BareProps, CallProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
function BalanceVoting ({ balances_all, children, className, label = '' }: Props): React.ReactElement<Props> {
|
||||
function BalanceVoting ({ balances_all, children, className, label }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<div className={className}>
|
||||
{label}{
|
||||
balances_all
|
||||
? formatBalance(balances_all.votingBalance)
|
||||
: '0'
|
||||
}{children}
|
||||
</div>
|
||||
<FormatBalance
|
||||
className={className}
|
||||
label={label}
|
||||
value={balances_all && balances_all.votingBalance}
|
||||
>
|
||||
{children}
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,8 @@ import { AccountId, AccountIndex, Address, StakingLedger } from '@polkadot/types
|
||||
import React from 'react';
|
||||
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
import FormatBalance from './FormatBalance';
|
||||
|
||||
interface Props extends BareProps, CallProps {
|
||||
children?: React.ReactNode;
|
||||
@ -20,13 +21,13 @@ interface Props extends BareProps, CallProps {
|
||||
|
||||
export function BondedDisplay ({ children, className, label = '', staking_ledger }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<div className={className}>
|
||||
{label}{
|
||||
staking_ledger
|
||||
? formatBalance(staking_ledger.active)
|
||||
: '-'
|
||||
}{children}
|
||||
</div>
|
||||
<FormatBalance
|
||||
className={className}
|
||||
label={label}
|
||||
value={staking_ledger && staking_ledger.active}
|
||||
>
|
||||
{children}
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
51
packages/react-query/src/FormatBalance.tsx
Normal file
51
packages/react-query/src/FormatBalance.tsx
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2017-2019 @polkadot/react-query authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BareProps } from '@polkadot/react-api/types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { Compact } from '@polkadot/types';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
interface Props extends BareProps {
|
||||
children?: React.ReactNode;
|
||||
label?: React.ReactNode;
|
||||
value?: Compact<any> | BN | string | null;
|
||||
}
|
||||
|
||||
function format (value: Compact<any> | BN | string): string {
|
||||
const [prefix, postfix] = formatBalance(value, { forceUnit: '-', withSi: false }).split('.');
|
||||
|
||||
return `${prefix}.${`000${postfix || ''}`.slice(-3)}`;
|
||||
}
|
||||
|
||||
export function FormatBalance ({ children, className, label = '', value }: Props): React.ReactElement<Props> {
|
||||
const [unit] = useState(formatBalance.getDefaults().unit);
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
{label}{
|
||||
value
|
||||
? format(value)
|
||||
: '-'
|
||||
} {unit}{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default styled(FormatBalance)`
|
||||
display: inline;
|
||||
vertical-align: middle;
|
||||
|
||||
* {
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
> label,
|
||||
> .label {
|
||||
margin-right: 0.25rem;
|
||||
}
|
||||
`;
|
||||
@ -7,9 +7,9 @@ import { BareProps, CallProps } from '@polkadot/react-api/types';
|
||||
import { AccountId, AccountIndex, Address, BalanceOf } from '@polkadot/types/interfaces';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
import FormatBalance from './FormatBalance';
|
||||
|
||||
interface Props extends BareProps, CallProps {
|
||||
children?: React.ReactNode;
|
||||
@ -20,13 +20,13 @@ interface Props extends BareProps, CallProps {
|
||||
|
||||
export function LockedVote ({ children, className, electionsPhragmen_stakeOf, label = '' }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<div className={className}>
|
||||
{label}{
|
||||
electionsPhragmen_stakeOf
|
||||
? formatBalance(electionsPhragmen_stakeOf)
|
||||
: '-'
|
||||
}{children}
|
||||
</div>
|
||||
<FormatBalance
|
||||
className={className}
|
||||
label={label}
|
||||
value={electionsPhragmen_stakeOf}
|
||||
>
|
||||
{children}
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ export { default as AccountIndex } from './AccountIndex';
|
||||
export { default as AccountName } from './AccountName';
|
||||
export { default as Available } from './Available';
|
||||
export { default as Balance } from './Balance';
|
||||
export { default as FormatBalance } from './FormatBalance';
|
||||
export { default as BalanceVoting } from './BalanceVoting';
|
||||
export { BlockAuthors, BlockAuthorsContext } from './BlockAuthors';
|
||||
export { default as Bonded } from './Bonded';
|
||||
|
||||
@ -63,7 +63,7 @@ export function Proposal ({ deposit, democracy_minimumDeposit = ZERO, onChange,
|
||||
<Icon name='warning sign' />
|
||||
{t('The deposit is below the {{minimum}} minimum required for the proposal to be evaluated', {
|
||||
replace: {
|
||||
minimum: formatBalance(democracy_minimumDeposit)
|
||||
minimum: formatBalance(democracy_minimumDeposit, { forceUnit: '-' })
|
||||
}
|
||||
})}
|
||||
</div>
|
||||
@ -73,7 +73,7 @@ export function Proposal ({ deposit, democracy_minimumDeposit = ZERO, onChange,
|
||||
<Icon name='arrow right' />
|
||||
{t('The deposit of {{deposit}} will be reserved until the proposal is completed', {
|
||||
replace: {
|
||||
deposit: formatBalance(extraAmount)
|
||||
deposit: formatBalance(extraAmount, { forceUnit: '-' })
|
||||
}
|
||||
})}
|
||||
</div>
|
||||
|
||||
@ -294,6 +294,7 @@ class Signer extends React.PureComponent<Props, State> {
|
||||
<InputBalance
|
||||
defaultValue={new BN(0)}
|
||||
help={t('Add a tip to this extrinsic, paying the block author for greater priority')}
|
||||
isZeroable
|
||||
onChange={this.onChangeTip}
|
||||
label={t('Tip (optional)')}
|
||||
/>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user