From ec1a110cb212b8853d6d93f62e3ab2a5fe9a769f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 8 Aug 2018 11:15:33 +0200 Subject: [PATCH] Add AddressRow component (#208) --- packages/ui-app/src/AddressMini.tsx | 7 ++- packages/ui-app/src/AddressRow.tsx | 40 +++++++++++++ packages/ui-app/src/AddressSummary.tsx | 66 +++++++++++++++------- packages/ui-app/src/styles/components.css | 29 ++++++++++ packages/ui-app/src/util/toShortAddress.ts | 7 +++ 5 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 packages/ui-app/src/AddressRow.tsx create mode 100644 packages/ui-app/src/util/toShortAddress.ts diff --git a/packages/ui-app/src/AddressMini.tsx b/packages/ui-app/src/AddressMini.tsx index 990847491e..56d7d3a13f 100644 --- a/packages/ui-app/src/AddressMini.tsx +++ b/packages/ui-app/src/AddressMini.tsx @@ -8,6 +8,7 @@ import React from 'react'; import IdentityIcon from '@polkadot/ui-react/IdentityIcon'; import classes from './util/classes'; +import toShortAddress from './util/toShortAddress'; import translate from './translate'; type Props = BareProps & { @@ -23,18 +24,18 @@ class AddressMini extends React.PureComponent { return null; } - const shortValue = `${value.slice(0, 7)}…${value.slice(-7)}`; + const shortValue = toShortAddress(value); return (
- {isShort ? shortValue : value} +
{isShort ? shortValue : value}
); } diff --git a/packages/ui-app/src/AddressRow.tsx b/packages/ui-app/src/AddressRow.tsx new file mode 100644 index 0000000000..488ab92a65 --- /dev/null +++ b/packages/ui-app/src/AddressRow.tsx @@ -0,0 +1,40 @@ +// Copyright 2017-2018 @polkadot/ui-app authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import React from 'react'; + +import IdentityIcon from '@polkadot/ui-react/IdentityIcon'; + +import classes from './util/classes'; +import { AddressSummary } from './AddressSummary'; +import translate from './translate'; + +class AddressRow extends AddressSummary { + render () { + const { className, style } = this.props; + const { address, isValid } = this.state; + + return ( +
+
+ +
+ {this.renderAddress()} + {this.renderBalance()} +
+
+ {this.renderChildren()} +
+ ); + } +} + +export default translate(AddressRow); diff --git a/packages/ui-app/src/AddressSummary.tsx b/packages/ui-app/src/AddressSummary.tsx index 04218f8689..82d6fc821e 100644 --- a/packages/ui-app/src/AddressSummary.tsx +++ b/packages/ui-app/src/AddressSummary.tsx @@ -13,10 +13,11 @@ import addressDecode from '@polkadot/util-keyring/address/decode'; import addressEncode from '@polkadot/util-keyring/address/encode'; import classes from './util/classes'; +import toShortAddress from './util/toShortAddress'; import CopyButton from './CopyButton'; import translate from './translate'; -type Props = I18nProps & { +export type Props = I18nProps & { children?: React.ReactNode, name?: string, value: string, @@ -24,7 +25,7 @@ type Props = I18nProps & { withNonce?: boolean }; -type State = { +export type State = { address: string, isValid: boolean, publicKey: Uint8Array | null, @@ -32,7 +33,7 @@ type State = { }; const DEFAULT_ADDR = '5'.padEnd(16, 'x'); -const DEFAULT_SHORT = `${DEFAULT_ADDR.slice(0, 7)}…${DEFAULT_ADDR.slice(-7)}`; +const DEFAULT_SHORT = toShortAddress(DEFAULT_ADDR); class AddressSummary extends React.PureComponent { state: State = {} as State; @@ -41,7 +42,7 @@ class AddressSummary extends React.PureComponent { try { publicKey = addressDecode(value); address = addressEncode(publicKey); - shortValue = `${address.slice(0, 7)}…${address.slice(-7)}`; + shortValue = toShortAddress(address); } catch (error) { publicKey = null; } @@ -57,8 +58,8 @@ class AddressSummary extends React.PureComponent { } render () { - const { children, className, name, style } = this.props; - const { address, isValid, shortValue } = this.state; + const { className, style } = this.props; + const { address, isValid } = this.state; return (
{ size={96} value={address} /> -
-
- {name} -
-
- {shortValue} -
- -
+ {this.renderAddress()} {this.renderBalance()}
-
- {children} -
+ {this.renderChildren()} ); } - renderBalance () { + protected renderAddress () { + const { name } = this.props; + const { address, shortValue } = this.state; + + return ( +
+
+ {name} +
+
+ {shortValue} +
+ +
+ ); + } + + protected renderBalance () { const { isValid, publicKey } = this.state; if (!isValid) { @@ -126,6 +134,26 @@ class AddressSummary extends React.PureComponent { : null ]; } + + protected renderChildren () { + const { children } = this.props; + + if (!children) { + return null; + } + + return ( +
+ {children} +
+ ); + } } +export { + DEFAULT_ADDR, + DEFAULT_SHORT, + AddressSummary +}; + export default translate(AddressSummary); diff --git a/packages/ui-app/src/styles/components.css b/packages/ui-app/src/styles/components.css index a6c6a7276d..3795a55445 100644 --- a/packages/ui-app/src/styles/components.css +++ b/packages/ui-app/src/styles/components.css @@ -31,6 +31,34 @@ border-radius: .28571429rem !important; } +.ui--AddressMini { + display: inline-block; + padding: 0.25rem; +} + +.ui--AddressMini > div { + display: inline-block; + vertical-align: middle; +} + +.ui--AddressMini > .ui--IdentityIcon { + margin-left: 1rem; + margin-right: 0.5rem; +} + +.ui--AddressRow { + display: flex; + width: 100%; +} + +.ui--AddressRow-base { + display: flex; +} + +.ui--AddressRow-icon { + margin-right: 1em; +} + .ui--AddressSummary { padding: 0 2em; text-align: left; @@ -72,6 +100,7 @@ .ui--AddressSummary-data { padding: 0.5em 0; + white-space: nowrap; } .ui--AddressSummary-data * { diff --git a/packages/ui-app/src/util/toShortAddress.ts b/packages/ui-app/src/util/toShortAddress.ts new file mode 100644 index 0000000000..f572c535b8 --- /dev/null +++ b/packages/ui-app/src/util/toShortAddress.ts @@ -0,0 +1,7 @@ +// Copyright 2017-2018 @polkadot/ui-app authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +export default function toShortAddress (address: string): string { + return `${address.slice(0, 7)}…${address.slice(-7)}`; +}