From dc943d7cd65e2e9450c0597186f4c98b24c76f31 Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Wed, 22 May 2019 18:08:58 +0200 Subject: [PATCH] Button/TxButton submit on enter (#1170) * Button/TxButton submit on enter * promise linting * testing ref approach * Add TxComponent to handle submit on enter * remove accountNonce * remove input ref * remove TxButton constructor * remove comment * empty constructor * linting * bug with null ref * linting --- packages/app-123code/src/Transfer.tsx | 7 ++- packages/app-accounts/src/Creator.tsx | 11 +++-- packages/app-accounts/src/Restore.tsx | 6 ++- packages/app-accounts/src/Vanity/index.tsx | 6 ++- packages/app-accounts/src/modals/Backup.tsx | 6 ++- .../app-accounts/src/modals/ChangePass.tsx | 6 ++- packages/app-address-book/src/Creator.tsx | 9 ++-- packages/app-address-book/src/Editor.tsx | 6 ++- packages/app-contracts/src/Call.tsx | 7 ++- packages/app-contracts/src/Code.tsx | 11 +++-- packages/app-contracts/src/Instantiate.tsx | 13 +++-- packages/app-contracts/src/Params.tsx | 5 +- packages/app-democracy/src/Propose.tsx | 7 ++- packages/app-explorer/src/Query.tsx | 7 +-- packages/app-extrinsics/src/Selection.tsx | 6 ++- packages/app-js/src/ActionButtons.tsx | 6 ++- packages/app-staking/src/Account/Bond.tsx | 6 ++- .../app-staking/src/Account/BondExtra.tsx | 6 ++- packages/app-staking/src/Account/Unbond.tsx | 6 ++- .../app-staking/src/Account/Validating.tsx | 7 ++- packages/app-storage/src/Query.tsx | 2 +- .../app-storage/src/Selection/Modules.tsx | 6 ++- packages/app-storage/src/Selection/Raw.tsx | 6 ++- packages/app-sudo/src/Sudo.tsx | 6 ++- packages/app-toolbox/src/Rpc/Selection.tsx | 6 ++- packages/app-toolbox/src/Unlock.tsx | 8 ++-- packages/app-transfer/src/Submit.tsx | 47 ------------------- packages/app-transfer/src/Transfer.tsx | 24 ++++++---- packages/ui-api/src/with/api.tsx | 3 ++ packages/ui-app/src/Button/Button.tsx | 7 +++ packages/ui-app/src/Button/index.tsx | 8 ++-- packages/ui-app/src/Button/types.ts | 1 + packages/ui-app/src/Extrinsic.tsx | 4 +- packages/ui-app/src/Input.tsx | 1 + packages/ui-app/src/InputBalance.tsx | 4 +- packages/ui-app/src/InputNumber.tsx | 4 +- packages/ui-app/src/Params/Call.tsx | 3 +- packages/ui-app/src/Params/Extrinsic.tsx | 6 ++- packages/ui-app/src/Params/Proposal.tsx | 3 +- packages/ui-app/src/Password.tsx | 6 +-- packages/ui-app/src/TxButton.tsx | 9 +++- packages/ui-app/src/TxComponent.tsx | 27 +++++++++++ packages/ui-app/src/index.tsx | 1 + packages/ui-params/src/Param/Amount.tsx | 3 +- packages/ui-params/src/Param/Balance.tsx | 3 +- packages/ui-params/src/Param/BaseBytes.tsx | 5 +- packages/ui-params/src/Param/Bytes.tsx | 3 +- packages/ui-params/src/Param/Code.tsx | 3 +- packages/ui-params/src/Param/Hash.tsx | 3 +- packages/ui-params/src/Param/KeyValue.tsx | 3 +- .../ui-params/src/Param/KeyValueArray.tsx | 3 +- packages/ui-params/src/Param/Moment.tsx | 3 +- packages/ui-params/src/Param/Text.tsx | 3 +- packages/ui-params/src/Param/Tuple.tsx | 3 +- packages/ui-params/src/Param/Unknown.tsx | 3 +- packages/ui-params/src/Param/Vector.tsx | 3 +- packages/ui-params/src/Param/index.tsx | 3 +- packages/ui-params/src/index.tsx | 8 ++-- packages/ui-params/src/types.ts | 2 + packages/ui-signer/src/Modal.tsx | 7 --- packages/ui-signer/src/Unlock.tsx | 6 +-- packages/ui-signer/src/util.ts | 0 62 files changed, 247 insertions(+), 156 deletions(-) delete mode 100644 packages/app-transfer/src/Submit.tsx create mode 100644 packages/ui-app/src/TxComponent.tsx delete mode 100644 packages/ui-signer/src/util.ts diff --git a/packages/app-123code/src/Transfer.tsx b/packages/app-123code/src/Transfer.tsx index e5a0d5dd29..c618e1c776 100644 --- a/packages/app-123code/src/Transfer.tsx +++ b/packages/app-123code/src/Transfer.tsx @@ -4,7 +4,7 @@ import BN from 'bn.js'; import React from 'react'; -import { Button, InputAddress, InputBalance, TxButton } from '@polkadot/ui-app'; +import { Button, InputAddress, InputBalance, TxButton, TxComponent } from '@polkadot/ui-app'; import Summary from './Summary'; @@ -16,7 +16,7 @@ type State = { recipientId?: string; }; -export default class Transfer extends React.PureComponent { +export default class Transfer extends TxComponent { state: State = {}; render () { @@ -31,11 +31,13 @@ export default class Transfer extends React.PureComponent { { label='make transfer' params={[recipientId, amount]} tx='balances.transfer' + ref={this.button} /> diff --git a/packages/app-accounts/src/Creator.tsx b/packages/app-accounts/src/Creator.tsx index 08b8920e4f..a31013b7cc 100644 --- a/packages/app-accounts/src/Creator.tsx +++ b/packages/app-accounts/src/Creator.tsx @@ -12,7 +12,7 @@ import FileSaver from 'file-saver'; import React from 'react'; import { DEV_PHRASE } from '@polkadot/keyring/defaults'; import { withApi, withMulti } from '@polkadot/ui-api'; -import { AddressSummary, AddressRow, Button, Dropdown, Input, InputTags, Labelled, Modal, Password } from '@polkadot/ui-app'; +import { AddressSummary, AddressRow, Button, Dropdown, Input, InputTags, Labelled, Modal, Password, TxComponent } from '@polkadot/ui-app'; import { InputAddress } from '@polkadot/ui-app/InputAddress'; import keyring from '@polkadot/ui-keyring'; import uiSettings from '@polkadot/ui-settings'; @@ -90,7 +90,7 @@ function addressFromSeed (phrase: string, derivePath: string, pairType: KeypairT .address(); } -class Creator extends React.PureComponent { +class Creator extends TxComponent { state: State = { seedType: 'bip' } as State; constructor (props: Props) { @@ -150,6 +150,7 @@ class Creator extends React.PureComponent { isPrimary label={t('Save')} onClick={this.onShowWarning} + ref={this.button} /> ); @@ -179,6 +180,7 @@ class Creator extends React.PureComponent { isError={!isNameValid} label={t('name')} onChange={this.onChangeName} + onEnter={this.submit} value={name} /> @@ -190,6 +192,7 @@ class Creator extends React.PureComponent { isError={!isSeedValid} label={seedLabel} onChange={this.onChangeSeed} + onEnter={this.submit} value={seed} > { isError={!isPassValid} label={t('password')} onChange={this.onChangePass} + onEnter={this.submit} value={password} /> @@ -240,6 +244,7 @@ class Creator extends React.PureComponent { isError={!!deriveError} label={t('secret derivation path')} onChange={this.onChangeDerive} + onEnter={this.submit} value={derivePath} /> @@ -340,7 +345,7 @@ class Creator extends React.PureComponent { private nextState (newState: State): void { this.setState( - (prevState: State, props: Props): State => { + (prevState: State): State => { const { derivePath = prevState.derivePath, name = prevState.name, pairType = prevState.pairType, password = prevState.password, seed = prevState.seed, seedOptions = prevState.seedOptions, seedType = prevState.seedType, showWarning = prevState.showWarning, tags = prevState.tags } = newState; let address = prevState.address; const deriveError = deriveValidate(derivePath, pairType); diff --git a/packages/app-accounts/src/Restore.tsx b/packages/app-accounts/src/Restore.tsx index b920b25891..7c0f1dd103 100644 --- a/packages/app-accounts/src/Restore.tsx +++ b/packages/app-accounts/src/Restore.tsx @@ -8,7 +8,7 @@ import { ActionStatus } from '@polkadot/ui-app/Status/types'; import { ComponentProps } from './types'; import React from 'react'; -import { AddressSummary, Button, InputFile, Password } from '@polkadot/ui-app'; +import { AddressSummary, Button, InputFile, Password, TxComponent } from '@polkadot/ui-app'; import { InputAddress } from '@polkadot/ui-app/InputAddress'; import { isHex, isObject, u8aToString } from '@polkadot/util'; import keyring from '@polkadot/ui-keyring'; @@ -25,7 +25,7 @@ type State = { password: string }; -class Restore extends React.PureComponent { +class Restore extends TxComponent { state: State = { address: null, isFileValid: false, @@ -57,6 +57,7 @@ class Restore extends React.PureComponent { isPrimary onClick={this.onSave} label={t('Restore')} + ref={this.button} /> @@ -89,6 +90,7 @@ class Restore extends React.PureComponent { isError={!isPassValid} label={t('password')} onChange={this.onChangePass} + onEnter={this.submit} value={password} /> diff --git a/packages/app-accounts/src/Vanity/index.tsx b/packages/app-accounts/src/Vanity/index.tsx index 1d353b3572..329254f89b 100644 --- a/packages/app-accounts/src/Vanity/index.tsx +++ b/packages/app-accounts/src/Vanity/index.tsx @@ -9,7 +9,7 @@ import { ComponentProps } from '../types'; import React from 'react'; import styled from 'styled-components'; -import { Button, Dropdown, Input } from '@polkadot/ui-app'; +import { Button, Dropdown, Input, TxComponent } from '@polkadot/ui-app'; import uiSettings from '@polkadot/ui-settings'; import generator from '../vanitygen'; @@ -51,7 +51,7 @@ const Wrapper = styled.div` } `; -class VanityApp extends React.PureComponent { +class VanityApp extends TxComponent { results: Array = []; state: State = { elapsed: 0, @@ -98,6 +98,7 @@ class VanityApp extends React.PureComponent { ? t('Stop generation') : t('Start generation') } + ref={this.button} /> ); @@ -135,6 +136,7 @@ class VanityApp extends React.PureComponent { isError={!isMatchValid} label={t('Search for')} onChange={this.onChangeMatch} + onEnter={this.submit} value={match} /> { +class Backup extends TxComponent { state: State = { isPassValid: false, password: '' @@ -59,6 +59,7 @@ class Backup extends React.PureComponent { isPrimary label={t('Download')} onClick={this.doBackup} + ref={this.button} /> @@ -87,6 +88,7 @@ class Backup extends React.PureComponent { isError={!isPassValid} label={t('password')} onChange={this.onChangePass} + onEnter={this.submit} tabIndex={0} value={password} /> diff --git a/packages/app-accounts/src/modals/ChangePass.tsx b/packages/app-accounts/src/modals/ChangePass.tsx index fe30667a94..5285a41421 100644 --- a/packages/app-accounts/src/modals/ChangePass.tsx +++ b/packages/app-accounts/src/modals/ChangePass.tsx @@ -5,7 +5,7 @@ import { I18nProps } from '@polkadot/ui-app/types'; import React from 'react'; -import { AddressRow, Button, Modal, Password } from '@polkadot/ui-app'; +import { AddressRow, Button, Modal, Password, TxComponent } from '@polkadot/ui-app'; import { ActionStatus } from '@polkadot/ui-app/Status/types'; import keyring from '@polkadot/ui-keyring'; @@ -23,7 +23,7 @@ type State = { oldPass: string }; -class ChangePass extends React.PureComponent { +class ChangePass extends TxComponent { state: State = { isNewValid: false, isOldValid: false, @@ -62,6 +62,7 @@ class ChangePass extends React.PureComponent { isPrimary label={t('Change')} onClick={this.doChange} + ref={this.button} /> @@ -98,6 +99,7 @@ class ChangePass extends React.PureComponent { isError={!isNewValid} label={t('your new password')} onChange={this.onChangeNew} + onEnter={this.submit} tabIndex={2} value={newPass} /> diff --git a/packages/app-address-book/src/Creator.tsx b/packages/app-address-book/src/Creator.tsx index 9fb1c1b4c3..f19d4c8a63 100644 --- a/packages/app-address-book/src/Creator.tsx +++ b/packages/app-address-book/src/Creator.tsx @@ -7,7 +7,7 @@ import { ComponentProps } from './types'; import React from 'react'; -import { AddressSummary, Button, Input, InputTags } from '@polkadot/ui-app'; +import { AddressSummary, Button, Input, InputTags, TxComponent } from '@polkadot/ui-app'; import { ActionStatus } from '@polkadot/ui-app/Status/types'; import { InputAddress } from '@polkadot/ui-app/InputAddress'; import keyring from '@polkadot/ui-keyring'; @@ -26,7 +26,7 @@ type State = { tags: Array }; -class Creator extends React.PureComponent { +class Creator extends TxComponent { state: State; constructor (props: Props) { @@ -69,6 +69,7 @@ class Creator extends React.PureComponent { isPrimary onClick={this.onCommit} label={t('Save')} + ref={this.button} /> ); @@ -88,6 +89,7 @@ class Creator extends React.PureComponent { isError={!isAddressValid} label={t('address')} onChange={this.onChangeAddress} + onEnter={this.submit} value={address} /> @@ -98,6 +100,7 @@ class Creator extends React.PureComponent { isError={!isNameValid} label={t('name')} onChange={this.onChangeName} + onEnter={this.submit} value={name} /> @@ -127,7 +130,7 @@ class Creator extends React.PureComponent { nextState (newState: State, allowEdit: boolean = false): void { this.setState( - (prevState: State, props: Props): State => { + (prevState: State): State => { let { address = prevState.address, name = prevState.name, tags = prevState.tags } = newState; let isAddressValid = true; let isAddressExisting = false; diff --git a/packages/app-address-book/src/Editor.tsx b/packages/app-address-book/src/Editor.tsx index 1d4a1ed298..bdeefb4588 100644 --- a/packages/app-address-book/src/Editor.tsx +++ b/packages/app-address-book/src/Editor.tsx @@ -7,7 +7,7 @@ import { I18nProps } from '@polkadot/ui-app/types'; import { ComponentProps } from './types'; import React from 'react'; -import { AddressSummary, Button, Input, InputAddress, InputTags } from '@polkadot/ui-app'; +import { AddressSummary, Button, Input, InputAddress, InputTags, TxComponent } from '@polkadot/ui-app'; import { ActionStatus } from '@polkadot/ui-app/Status/types'; import keyring from '@polkadot/ui-keyring'; @@ -24,7 +24,7 @@ type State = { tags: Array }; -class Editor extends React.PureComponent { +class Editor extends TxComponent { state: State; constructor (props: Props) { @@ -76,6 +76,7 @@ class Editor extends React.PureComponent { isPrimary onClick={this.onCommit} label={t('Save')} + ref={this.button} /> ); @@ -115,6 +116,7 @@ class Editor extends React.PureComponent { help={t('Name associated with the selected contact. You can change it here. This name will be changed across all the apps.')} label={t('name')} onChange={this.onChangeName} + onEnter={this.submit} value={editedName} /> diff --git a/packages/app-contracts/src/Call.tsx b/packages/app-contracts/src/Call.tsx index 4f003de48e..99ed135693 100644 --- a/packages/app-contracts/src/Call.tsx +++ b/packages/app-contracts/src/Call.tsx @@ -7,7 +7,7 @@ import { ComponentProps } from './types'; import BN from 'bn.js'; import React from 'react'; -import { Button, Dropdown, InputAddress, InputBalance, InputNumber, TxButton } from '@polkadot/ui-app'; +import { Button, Dropdown, InputAddress, InputBalance, InputNumber, TxButton, TxComponent } from '@polkadot/ui-app'; import { ContractAbi } from '@polkadot/types'; import store from './store'; @@ -28,7 +28,7 @@ type State = { params: Array }; -class Call extends React.PureComponent { +class Call extends TxComponent { state: State = { accountId: null, endowment: new BN(0), @@ -94,6 +94,7 @@ class Call extends React.PureComponent { /> { isError={!isGasValid} label={t('maximum gas allowed')} onChange={this.onChangeGas} + onEnter={this.sendTx} /> { onSuccess={this.toggleBusy} params={this.constructCall} tx='contract.call' + ref={this.button} /> diff --git a/packages/app-contracts/src/Code.tsx b/packages/app-contracts/src/Code.tsx index 6d5c34c0ee..f18e93b434 100644 --- a/packages/app-contracts/src/Code.tsx +++ b/packages/app-contracts/src/Code.tsx @@ -8,7 +8,7 @@ import { ComponentProps } from './types'; import BN from 'bn.js'; import React from 'react'; -import { Button, Input, InputAddress, InputFile, InputNumber, TxButton } from '@polkadot/ui-app'; +import { Button, Input, InputAddress, InputFile, InputNumber, TxButton, TxComponent } from '@polkadot/ui-app'; import { compactAddLength } from '@polkadot/util'; import { Hash } from '@polkadot/types'; @@ -34,7 +34,7 @@ type State = { wasm?: Uint8Array | null }; -class Deploy extends React.PureComponent { +class Deploy extends TxComponent { state: State = { accountId: null, gasLimit: new BN(0), @@ -105,6 +105,7 @@ class Deploy extends React.PureComponent { help={t('The maximum amount of gas that can be used by this deployment, if the code requires more, the deployment will fail.')} label={t('maximum gas allowed')} onChange={this.onChangeGas} + onEnter={this.sendTx} /> { onSuccess={this.onSuccess} params={[gasLimit, wasm]} tx='contract.putCode' + ref={this.button} /> @@ -136,6 +138,7 @@ class Deploy extends React.PureComponent { isError={!isCodeValid} label={t('code hash')} onChange={this.onChangeHash} + onEnter={this.submit} value={codeHash} /> { isPrimary label={t('Save')} onClick={this.onSave} + ref={this.button} /> @@ -172,7 +176,7 @@ class Deploy extends React.PureComponent { private renderInputName () { const { t } = this.props; - const { isNameValid, name } = this.state; + const { isNameValid, isNew, name } = this.state; return ( { isError={!isNameValid} label={t('code bundle name')} onChange={this.onChangeName} + onEnter={this[isNew ? 'sendTx' : 'submit']} value={name} /> ); diff --git a/packages/app-contracts/src/Instantiate.tsx b/packages/app-contracts/src/Instantiate.tsx index 9a925a0e43..4eabe3f8da 100644 --- a/packages/app-contracts/src/Instantiate.tsx +++ b/packages/app-contracts/src/Instantiate.tsx @@ -8,7 +8,7 @@ import { ComponentProps } from './types'; import BN from 'bn.js'; import React from 'react'; -import { Button, Dropdown, Input, InputAddress, InputBalance, InputNumber, TxButton } from '@polkadot/ui-app'; +import { Button, Dropdown, Input, InputAddress, InputBalance, InputNumber, TxButton, TxComponent } from '@polkadot/ui-app'; import { AccountId, ContractAbi } from '@polkadot/types'; import ABI from './ABI'; @@ -38,7 +38,7 @@ type State = { params: Array }; -class Create extends React.PureComponent { +class Create extends TxComponent { state: State = { accountId: null, endowment: new BN(0), @@ -147,6 +147,7 @@ class Create extends React.PureComponent { } { isError={!isEndowValid} label={t('endowment')} onChange={this.onChangeEndowment} + onEnter={this.sendTx} /> { onSuccess={this.onSuccess} params={this.constructCall} tx='contract.create' + ref={this.button} /> @@ -195,6 +199,7 @@ class Create extends React.PureComponent { isError={!isAddressValid} label={t('contract address')} onChange={this.onChangeAddress} + onEnter={this.submit} value={address} /> { isPrimary label={t('Save')} onClick={this.onSave} + ref={this.button} /> @@ -231,7 +237,7 @@ class Create extends React.PureComponent { private renderInputName () { const { t } = this.props; - const { isNameValid, name } = this.state; + const { isNameValid, isNew, name } = this.state; return ( { isError={!isNameValid} label={t('contract name')} onChange={this.onChangeName} + onEnter={this[isNew ? 'sendTx' : 'submit']} value={name} /> ); diff --git a/packages/app-contracts/src/Params.tsx b/packages/app-contracts/src/Params.tsx index 252fc74ba3..7191a3e131 100644 --- a/packages/app-contracts/src/Params.tsx +++ b/packages/app-contracts/src/Params.tsx @@ -11,7 +11,8 @@ import { getTypeDef, TypeDef } from '@polkadot/types'; type Props = { params?: ContractABIArgs, - onChange: (values: Array) => void + onChange: (values: Array) => void, + onEnter?: () => void }; type State = { @@ -35,6 +36,7 @@ export default class Params extends React.PureComponent { } render () { + const { onEnter } = this.props; const { params } = this.state; if (!params.length) { @@ -44,6 +46,7 @@ export default class Params extends React.PureComponent { return ( ); diff --git a/packages/app-democracy/src/Propose.tsx b/packages/app-democracy/src/Propose.tsx index a08fa78ea9..8bbfb00df0 100644 --- a/packages/app-democracy/src/Propose.tsx +++ b/packages/app-democracy/src/Propose.tsx @@ -11,7 +11,7 @@ import { RouteComponentProps } from 'react-router'; import { withRouter } from 'react-router-dom'; import { Method, Proposal } from '@polkadot/types'; -import { Button, Extrinsic, InputAddress, InputBalance, TxButton } from '@polkadot/ui-app'; +import { Button, Extrinsic, InputAddress, InputBalance, TxButton, TxComponent } from '@polkadot/ui-app'; import { withApi, withMulti } from '@polkadot/ui-api'; import translate from './translate'; @@ -27,7 +27,7 @@ type State = { isValid: boolean }; -class Propose extends React.PureComponent { +class Propose extends TxComponent { state: State = { method: null, value: new BN(0), @@ -60,6 +60,7 @@ class Propose extends React.PureComponent { defaultValue={defaultExtrinsic} label={t('propose')} onChange={this.onChangeExtrinsic} + onEnter={this.sendTx} /> { help={t('The amount that will be bonded to submit the proposal')} label={t('value')} onChange={this.onChangeValue} + onEnter={this.sendTx} /> { ...(hasValue ? [value] : []) ]} onSuccess={this.onSubmitProposal} + ref={this.button} /> diff --git a/packages/app-explorer/src/Query.tsx b/packages/app-explorer/src/Query.tsx index e98b73bac6..a5cccdfee2 100644 --- a/packages/app-explorer/src/Query.tsx +++ b/packages/app-explorer/src/Query.tsx @@ -5,7 +5,7 @@ import { I18nProps } from '@polkadot/ui-app/types'; import React from 'react'; -import { Button, FilterOverlay, Input } from '@polkadot/ui-app'; +import { Button, FilterOverlay, Input, TxComponent } from '@polkadot/ui-app'; import { isHex } from '@polkadot/util'; import translate from './translate'; @@ -20,7 +20,7 @@ type State = { isValid: boolean }; -class Query extends React.PureComponent { +class Query extends TxComponent { constructor (props: Props) { super(props); @@ -41,13 +41,14 @@ class Query extends React.PureComponent { isError={!isValid && value.length !== 0} placeholder={t('block hash or number to query')} onChange={this.setHash} - onEnter={this.onQuery} + onEnter={this.submit} withLabel={false} >