@@ -211,9 +212,10 @@ function renderValidatorPrefs ({ stakingInfo, t, withValidatorPrefs = false }: P
{validatorPrefsDisplay.validatorPayment && stakingInfo.validatorPrefs.validatorPayment && (
<>
- {
- formatBalance(stakingInfo.validatorPrefs.validatorPayment)
- }
+
>
)}
>
@@ -237,40 +239,61 @@ function renderBalances (props: Props): React.ReactNode {
{balancesAll && balanceDisplay.total && (
<>
- {formatBalance(balancesAll.votingBalance)}
+
>
)}
{balancesAll && balanceDisplay.available && (
<>
- {formatBalance(balancesAll.availableBalance)}
+
>
)}
{balancesAll && balanceDisplay.locked && balancesAll.lockedBalance && balancesAll.lockedBalance.gtn(0) && (
<>
- {formatBalance(balancesAll.lockedBalance)}
+
>
)}
{balancesAll && balanceDisplay.reserved && balancesAll.reservedBalance && balancesAll.reservedBalance.gtn(0) && (
<>
- {formatBalance(balancesAll.reservedBalance)}
+
>
)}
{balanceDisplay.bonded && (ownBonded.gtn(0) || otherBonded.length !== 0) && (
<>
- {formatBalance(ownBonded)}{otherBonded.length !== 0 && (
- ` (+${otherBonded.map((bonded): string => formatBalance(bonded)).join(', ')})`
- )}
+
+ {otherBonded.length !== 0 && (
+ <> (+{otherBonded.map((bonded, index): React.ReactNode =>
+
+ )})>
+ )}
+
>
)}
{balanceDisplay.redeemable && stakingInfo && stakingInfo.redeemable && stakingInfo.redeemable.gtn(0) && (
<>
-
- {formatBalance(stakingInfo.redeemable)}
+
{stakingInfo.controllerId && (
)}
-
+
>
)}
{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;
diff --git a/packages/react-components/src/AddressMini.tsx b/packages/react-components/src/AddressMini.tsx
index 3f0575d7af..468e7a9c29 100644
--- a/packages/react-components/src/AddressMini.tsx
+++ b/packages/react-components/src/AddressMini.tsx
@@ -114,6 +114,7 @@ export default styled(AddressMini)`
.ui--AddressMini-balances {
display: grid;
+ .ui--Balance,
.ui--Bonded,
.ui--LockedVote {
font-size: 0.75rem;
diff --git a/packages/react-components/src/Balance.tsx b/packages/react-components/src/Balance.tsx
index 58aafa66da..e8432783c2 100644
--- a/packages/react-components/src/Balance.tsx
+++ b/packages/react-components/src/Balance.tsx
@@ -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 =>
+
+ );
+ }
}
return (
-
- {label}{value}
-
+ {others && (
+ (+{others})
+ )}
+
);
}
@@ -51,7 +61,7 @@ export default function BalanceDisplay (props: Props): React.ReactElement
return balance
? (
<>
- {renderProvided(props)}
+ {renderProvided({ className, label, value: balance })}
>
)
: (
diff --git a/packages/react-components/src/Bonded.tsx b/packages/react-components/src/Bonded.tsx
index 0449a87cbb..0585af3459 100644
--- a/packages/react-components/src/Bonded.tsx
+++ b/packages/react-components/src/Bonded.tsx
@@ -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 (
-
- {label}{value}
-
- );
-}
-
export default function BondedDisplay (props: Props): React.ReactElement | null {
const { bonded, params, className, label, style } = props;
@@ -51,7 +29,7 @@ export default function BondedDisplay (props: Props): React.ReactElement
return bonded
? (
<>
- {renderProvided(props)}
+ {renderProvided({ className, label, value: bonded })}
>
)
: (
diff --git a/packages/react-components/src/Call.tsx b/packages/react-components/src/Call.tsx
index e988caee82..9dff4a8f49 100644
--- a/packages/react-components/src/Call.tsx
+++ b/packages/react-components/src/Call.tsx
@@ -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)}
+
)}
{
+function InputBalance ({ autoFocus, className, defaultValue, help, isDisabled, isError, isZeroable, label, labelExtra, maxValue, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax }: Props): React.ReactElement {
return (
void;
onEnter?: () => void;
@@ -60,7 +61,7 @@ class InputBalanceBonded extends React.PureComponent {
}
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 {
isDisabled={isDisabled}
isError={isError}
isSi
+ isZeroable={isZeroable}
label={label}
maxValue={maxBalance}
onChange={onChange}
diff --git a/packages/react-components/src/InputNumber.tsx b/packages/react-components/src/InputNumber.tsx
index 8cc3a4d3aa..b752264ad3 100644
--- a/packages/react-components/src/InputNumber.tsx
+++ b/packages/react-components/src/InputNumber.tsx
@@ -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 {
- 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 | 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 (
-
- {(ALLOW_MAX && withMax && !!maxValue) && this.renderMaxButton()}
- {isSi && this.renderSiDropdown()}
-
- );
- }
+// 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 (
-
- );
- }
+ 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 {
+ 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(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 (
-
- );
- }
+ 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): void => {
- const { isPreKeyDown } = this.state;
+ const _onChange = (input: string): void => {
+ setValues(getValuesFromString(input, si, props));
+ };
+ const _onKeyDown = (event: React.KeyboardEvent): void => {
if (KEYS_PRE.includes(event.key)) {
- this.setState({ isPreKeyDown: true });
+ setIsPreKeyDown(true);
return;
}
@@ -222,131 +224,76 @@ class InputNumber extends React.PureComponent {
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): void => {
+ const _onKeyUp = (event: React.KeyboardEvent): void => {
if (KEYS_PRE.includes(event.key)) {
- this.setState({ isPreKeyDown: false });
+ setIsPreKeyDown(false);
}
- }
+ };
- private onPaste = (event: React.ClipboardEvent): void => {
+ const _onPaste = (event: React.ClipboardEvent): 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 => {
- 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 => {
- 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 (
+
+ {/* (ALLOW_MAX && withMax && !!maxValue && valueBn.lt(maxValue)) && (
+
+ ) */}
+ {!!si && (
+
+ )}
+
+ );
}
-export {
- InputNumber
-};
-
export default translate(InputNumber);
diff --git a/packages/react-components/src/Row.tsx b/packages/react-components/src/Row.tsx
index f8f2a87bd4..d4503c7b47 100644
--- a/packages/react-components/src/Row.tsx
+++ b/packages/react-components/src/Row.tsx
@@ -77,6 +77,7 @@ export const styles = `
label,
.result {
display: inline-block;
+ vertical-align: middle;
}
}
diff --git a/packages/react-components/src/TreasuryProposal.tsx b/packages/react-components/src/TreasuryProposal.tsx
index 14da241617..d917b2a619 100644
--- a/packages/react-components/src/TreasuryProposal.tsx
+++ b/packages/react-components/src/TreasuryProposal.tsx
@@ -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,
/>
- {formatBalance(value)}
+
- {formatBalance(bond)}
+
>
);
diff --git a/packages/react-query/src/Available.tsx b/packages/react-query/src/Available.tsx
index e414c82c61..685463c63e 100644
--- a/packages/react-query/src/Available.tsx
+++ b/packages/react-query/src/Available.tsx
@@ -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 {
+function AvailableDisplay ({ balances_all, children, className, label }: Props): React.ReactElement {
return (
-
- {label}{
- balances_all
- ? formatBalance(balances_all.availableBalance)
- : '0'
- }{children}
-
+
+ {children}
+
);
}
diff --git a/packages/react-query/src/Balance.tsx b/packages/react-query/src/Balance.tsx
index 9cec68e7f2..a7a413a347 100644
--- a/packages/react-query/src/Balance.tsx
+++ b/packages/react-query/src/Balance.tsx
@@ -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 {
+export function BalanceDisplay ({ children, className, label, balances_all }: Props): React.ReactElement {
return (
-
- {label}{
- balances_all
- ? formatBalance(balances_all.freeBalance)
- : '-'
- }{children}
-
+
+ {children}
+
);
}
diff --git a/packages/react-query/src/BalanceVoting.tsx b/packages/react-query/src/BalanceVoting.tsx
index c887af9450..2ac5be92c2 100644
--- a/packages/react-query/src/BalanceVoting.tsx
+++ b/packages/react-query/src/BalanceVoting.tsx
@@ -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 {
+function BalanceVoting ({ balances_all, children, className, label }: Props): React.ReactElement {
return (
-
- {label}{
- balances_all
- ? formatBalance(balances_all.votingBalance)
- : '0'
- }{children}
-
+
+ {children}
+
);
}
diff --git a/packages/react-query/src/Bonded.tsx b/packages/react-query/src/Bonded.tsx
index 09d55ef587..e17ce2db23 100644
--- a/packages/react-query/src/Bonded.tsx
+++ b/packages/react-query/src/Bonded.tsx
@@ -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 {
return (
-
- {label}{
- staking_ledger
- ? formatBalance(staking_ledger.active)
- : '-'
- }{children}
-
+
+ {children}
+
);
}
diff --git a/packages/react-query/src/FormatBalance.tsx b/packages/react-query/src/FormatBalance.tsx
new file mode 100644
index 0000000000..d91a5f90ae
--- /dev/null
+++ b/packages/react-query/src/FormatBalance.tsx
@@ -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 | BN | string | null;
+}
+
+function format (value: Compact | 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 {
+ const [unit] = useState(formatBalance.getDefaults().unit);
+
+ return (
+
+ {label}{
+ value
+ ? format(value)
+ : '-'
+ } {unit}{children}
+
+ );
+}
+
+export default styled(FormatBalance)`
+ display: inline;
+ vertical-align: middle;
+
+ * {
+ vertical-align: middle;
+ }
+
+ > label,
+ > .label {
+ margin-right: 0.25rem;
+ }
+`;
diff --git a/packages/react-query/src/LockedVote.tsx b/packages/react-query/src/LockedVote.tsx
index b2770d2027..70702e86af 100644
--- a/packages/react-query/src/LockedVote.tsx
+++ b/packages/react-query/src/LockedVote.tsx
@@ -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 {
return (
-
- {label}{
- electionsPhragmen_stakeOf
- ? formatBalance(electionsPhragmen_stakeOf)
- : '-'
- }{children}
-
+
+ {children}
+
);
}
diff --git a/packages/react-query/src/index.ts b/packages/react-query/src/index.ts
index ff8534c762..4b40d62679 100644
--- a/packages/react-query/src/index.ts
+++ b/packages/react-query/src/index.ts
@@ -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';
diff --git a/packages/react-signer/src/Checks/Proposal.tsx b/packages/react-signer/src/Checks/Proposal.tsx
index 5e10d52ec7..f2fac94bc2 100644
--- a/packages/react-signer/src/Checks/Proposal.tsx
+++ b/packages/react-signer/src/Checks/Proposal.tsx
@@ -63,7 +63,7 @@ export function Proposal ({ deposit, democracy_minimumDeposit = ZERO, onChange,
{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: '-' })
}
})}