diff --git a/packages/react-components/src/Button/types.ts b/packages/react-components/src/Button/types.ts index c4f626c50c..676cc708da 100644 --- a/packages/react-components/src/Button/types.ts +++ b/packages/react-components/src/Button/types.ts @@ -6,6 +6,8 @@ import { BareProps } from '../types'; export type Button$Sizes = 'mini' | 'tiny' | 'small' | 'medium' | 'large' | 'big' | 'huge' | 'massive'; +export type Button$OnClick = () => void | Promise; + export interface ButtonProps extends BareProps { children?: React.ReactNode; floated?: 'left' | 'right'; @@ -20,7 +22,7 @@ export interface ButtonProps extends BareProps { isPrimary?: boolean; label?: React.ReactNode; labelPosition?: 'left' | 'right'; - onClick?: () => void | Promise; + onClick?: Button$OnClick; ref?: any; size?: Button$Sizes; tabIndex?: number; diff --git a/packages/react-components/src/Extrinsic.tsx b/packages/react-components/src/Extrinsic.tsx index 85b275f4a9..9ab0e8a4d6 100644 --- a/packages/react-components/src/Extrinsic.tsx +++ b/packages/react-components/src/Extrinsic.tsx @@ -23,6 +23,7 @@ interface Props extends BareProps { label?: React.ReactNode; onChange: (method?: Call) => void; onEnter?: () => void; + onEscape?: () => void; withLabel?: boolean; } @@ -33,7 +34,7 @@ function getParams ({ meta }: CallFunction): { name: string; type: TypeDef }[] { })); } -export default function ExtrinsicDisplay ({ defaultValue, isDisabled, isError, isPrivate, label, onChange, onEnter, withLabel }: Props): React.ReactElement { +export default function ExtrinsicDisplay ({ defaultValue, isDisabled, isError, isPrivate, label, onChange, onEnter, onEscape, withLabel }: Props): React.ReactElement { const [extrinsic, setCall] = useState<{ fn: CallFunction; params: { name: string; type: TypeDef }[] }>({ fn: defaultValue, params: getParams(defaultValue) }); const [values, setValues] = useState([]); @@ -82,6 +83,7 @@ export default function ExtrinsicDisplay ({ defaultValue, isDisabled, isError, i key={`${section}.${method}:params` /* force re-render on change */} onChange={setValues} onEnter={onEnter} + onEscape={onEscape} overrides={paramComponents} params={params} /> diff --git a/packages/react-components/src/Input.tsx b/packages/react-components/src/Input.tsx index f0bf31d199..280aa7a6b9 100644 --- a/packages/react-components/src/Input.tsx +++ b/packages/react-components/src/Input.tsx @@ -31,6 +31,7 @@ interface Props extends BareProps { min?: any; name?: string; onEnter?: () => void; + onEscape?: () => void; onChange?: (value: string) => void; onBlur?: () => void; onKeyDown?: (event: React.KeyboardEvent) => void; @@ -86,7 +87,7 @@ const isSelectAll = (key: string, isPreKeyDown: boolean): boolean => let counter = 0; -export default function Input ({ autoFocus = false, children, className, defaultValue, help, icon, isEditable = false, isAction = false, isDisabled = false, isError = false, isHidden = false, isReadOnly = false, label, labelExtra, max, maxLength, min, name, onBlur, onChange, onEnter, onKeyDown, onKeyUp, onPaste, placeholder, style, tabIndex, type = 'text', value, withEllipsis, withLabel }: Props): React.ReactElement { +export default function Input ({ autoFocus = false, children, className, defaultValue, help, icon, isEditable = false, isAction = false, isDisabled = false, isError = false, isHidden = false, isReadOnly = false, label, labelExtra, max, maxLength, min, name, onBlur, onChange, onEnter, onEscape, onKeyDown, onKeyUp, onPaste, placeholder, style, tabIndex, type = 'text', value, withEllipsis, withLabel }: Props): React.ReactElement { const [stateName] = useState(`in_${counter++}_at_${Date.now()}`); const _onBlur = (): void => { @@ -105,6 +106,11 @@ export default function Input ({ autoFocus = false, children, className, default (event.target as any).blur(); onEnter(); } + + if (onEscape && event.keyCode === 27) { + (event.target as any).blur(); + onEscape(); + } }; const _onPaste = (event: React.ClipboardEvent): void => { onPaste && onPaste(event); diff --git a/packages/react-components/src/InputAddress/types.ts b/packages/react-components/src/InputAddress/types.ts index 761dbff4cc..4aa938d62a 100644 --- a/packages/react-components/src/InputAddress/types.ts +++ b/packages/react-components/src/InputAddress/types.ts @@ -2,10 +2,32 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { KeyringSectionOption } from '@polkadot/ui-keyring/options/types'; +import { KeyringSectionOption, KeyringOption$Type } from '@polkadot/ui-keyring/options/types'; +import { BareProps } from '../types'; import React from 'react'; +export interface InputAddressProps extends BareProps { + defaultValue?: Uint8Array | string | null; + help?: React.ReactNode; + hideAddress?: boolean; + isDisabled?: boolean; + isError?: boolean; + isInput?: boolean; + isMultiple?: boolean; + label?: React.ReactNode; + labelExtra?: React.ReactNode; + onChange?: (value: string | null) => void; + onChangeMulti?: (value: string[]) => void; + options?: KeyringSectionOption[]; + optionsAll?: Record; + placeholder?: string; + type?: KeyringOption$Type; + value?: string | Uint8Array | string[] | null; + withEllipsis?: boolean; + withLabel?: boolean; +} + export interface Option extends KeyringSectionOption { className?: string; text: React.ReactNode; diff --git a/packages/react-components/src/InputAddressSimple.tsx b/packages/react-components/src/InputAddressSimple.tsx index f9c6275462..2eebd30429 100644 --- a/packages/react-components/src/InputAddressSimple.tsx +++ b/packages/react-components/src/InputAddressSimple.tsx @@ -18,9 +18,10 @@ interface Props extends BareProps { label?: React.ReactNode; onChange?: (address: string | null) => void; onEnter?: () => void; + onEscape?: () => void; } -function InputAddressSimple ({ children, className, defaultValue, help, label, onChange, onEnter }: Props): React.ReactElement { +function InputAddressSimple ({ children, className, defaultValue, help, label, onChange, onEnter, onEscape }: Props): React.ReactElement { const [address, setAddress] = useState(defaultValue || null); const _onChange = (_address: string): void => { @@ -40,6 +41,7 @@ function InputAddressSimple ({ children, className, defaultValue, help, label, o label={label} onChange={_onChange} onEnter={onEnter} + onEscape={onEscape} > {children} diff --git a/packages/react-components/src/InputBalance.tsx b/packages/react-components/src/InputBalance.tsx index fe7a9812f1..41529ea268 100644 --- a/packages/react-components/src/InputBalance.tsx +++ b/packages/react-components/src/InputBalance.tsx @@ -23,6 +23,7 @@ interface Props extends BareProps { maxValue?: BN; onChange?: (value?: BN) => void; onEnter?: () => void; + onEscape?: () => void; placeholder?: string; value?: BN | string; withEllipsis?: boolean; @@ -32,7 +33,7 @@ interface Props extends BareProps { const DEFAULT_BITLENGTH = BitLengthOption.CHAIN_SPEC as BitLength; -function InputBalance ({ autoFocus, className, defaultValue: inDefault, help, isDisabled, isError, isZeroable, label, labelExtra, maxValue, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax }: Props): React.ReactElement { +function InputBalance ({ autoFocus, className, defaultValue: inDefault, help, isDisabled, isError, isZeroable, label, labelExtra, maxValue, onChange, onEnter, onEscape, placeholder, style, value, withEllipsis, withLabel, withMax }: Props): React.ReactElement { const defaultValue = inDefault ? formatBalance(inDefault, { forceUnit: '-', withSi: false }).replace(',', isDisabled ? ',' : '') : inDefault; @@ -53,6 +54,7 @@ function InputBalance ({ autoFocus, className, defaultValue: inDefault, help, is maxValue={maxValue} onChange={onChange} onEnter={onEnter} + onEscape={onEscape} placeholder={placeholder} style={style} value={value} diff --git a/packages/react-components/src/InputBalanceBonded.tsx b/packages/react-components/src/InputBalanceBonded.tsx index 72bb871e08..af6d67eb52 100644 --- a/packages/react-components/src/InputBalanceBonded.tsx +++ b/packages/react-components/src/InputBalanceBonded.tsx @@ -32,6 +32,7 @@ interface Props extends BareProps, ApiProps { label?: any; onChange?: (value?: BN) => void; onEnter?: () => void; + onEscape?: () => void; placeholder?: string; stashId: string; value?: BN | string; @@ -61,7 +62,7 @@ class InputBalanceBonded extends React.PureComponent { } public render (): React.ReactNode { - const { autoFocus, className, defaultValue, help, isDisabled, isError, isZeroable, label, onChange, onEnter, placeholder, style, value, withEllipsis, withLabel, withMax } = this.props; + const { autoFocus, className, defaultValue, help, isDisabled, isError, isZeroable, label, onChange, onEnter, onEscape, placeholder, style, value, withEllipsis, withLabel, withMax } = this.props; const { maxBalance } = this.state; return ( @@ -79,6 +80,7 @@ class InputBalanceBonded extends React.PureComponent { maxValue={maxBalance} onChange={onChange} onEnter={onEnter} + onEscape={onEscape} placeholder={placeholder} style={style} value={value} diff --git a/packages/react-components/src/InputNumber.tsx b/packages/react-components/src/InputNumber.tsx index 0dd7cc136c..9b04b3b406 100644 --- a/packages/react-components/src/InputNumber.tsx +++ b/packages/react-components/src/InputNumber.tsx @@ -34,6 +34,7 @@ interface Props extends BareProps, I18nProps { maxValue?: BN; onChange?: (value?: BN) => void; onEnter?: () => void; + onEscape?: () => void; placeholder?: string; value?: BN | string; withEllipsis?: boolean; @@ -192,7 +193,7 @@ function isNewPropsValue (propsValue: BN | string, value: string, valueBn: BN): } 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 { bitLength = DEFAULT_BITLENGTH, className, defaultValue = ZERO, help, isDecimal, isSi, isDisabled, isError = false, maxLength, maxValue, onChange, onEnter, onEscape, placeholder, style, value: propsValue, t } = props; const [si, setSi] = useState(isSi ? formatBalance.findSi('-') : null); const [isPreKeyDown, setIsPreKeyDown] = useState(false); @@ -270,6 +271,7 @@ function InputNumber (props: Props): React.ReactElement { maxLength={maxLength || maxValueLength} onChange={_onChange} onEnter={onEnter} + onEscape={onEscape} onKeyDown={_onKeyDown} onKeyUp={_onKeyUp} onPaste={_onPaste} diff --git a/packages/react-components/src/Params/Call.tsx b/packages/react-components/src/Params/Call.tsx index 4d775ac3c4..9f3752673f 100644 --- a/packages/react-components/src/Params/Call.tsx +++ b/packages/react-components/src/Params/Call.tsx @@ -10,7 +10,7 @@ import { useApi } from '@polkadot/react-hooks'; import Extrinsic from './Extrinsic'; -export default function Call ({ className, isDisabled, isError, label, onChange, onEnter, style, withLabel }: Props): React.ReactElement { +export default function Call ({ className, isDisabled, isError, label, onChange, onEnter, onEscape, style, withLabel }: Props): React.ReactElement { const { api, apiDefaultTx } = useApi(); const defaultValue = ((): SubmittableExtrinsicFunction<'promise'> => { @@ -31,6 +31,7 @@ export default function Call ({ className, isDisabled, isError, label, onChange, label={label} onChange={onChange} onEnter={onEnter} + onEscape={onEscape} style={style} withLabel={withLabel} /> diff --git a/packages/react-components/src/Params/Extrinsic.tsx b/packages/react-components/src/Params/Extrinsic.tsx index 8c10e5f182..bf23a7b755 100644 --- a/packages/react-components/src/Params/Extrinsic.tsx +++ b/packages/react-components/src/Params/Extrinsic.tsx @@ -4,7 +4,7 @@ import { Call } from '@polkadot/types/interfaces'; import { CallFunction } from '@polkadot/types/types'; -import { RawParamOnChange, RawParamOnEnter } from '@polkadot/react-params/types'; +import { RawParamOnChange, RawParamOnEnter, RawParamOnEscape } from '@polkadot/react-params/types'; import { BareProps } from '../types'; import React from 'react'; @@ -19,6 +19,7 @@ interface Props extends BareProps { label: React.ReactNode; onChange?: RawParamOnChange; onEnter?: RawParamOnEnter; + onEscape?: RawParamOnEscape; withLabel?: boolean; } @@ -32,7 +33,7 @@ function onChange ({ onChange }: Props): (method?: Call) => void { } export default function ExtrinsicDisplay (props: Props): React.ReactElement { - const { className, defaultValue, isDisabled, isError, isPrivate, label, onEnter, style, withLabel } = props; + const { className, defaultValue, isDisabled, isError, isPrivate, label, onEnter, onEscape, style, withLabel } = props; return ( diff --git a/packages/react-components/src/Params/Proposal.tsx b/packages/react-components/src/Params/Proposal.tsx index f6ee995e4e..0ce194da10 100644 --- a/packages/react-components/src/Params/Proposal.tsx +++ b/packages/react-components/src/Params/Proposal.tsx @@ -28,7 +28,7 @@ function onChange ({ onChange }: Props): (_: RawParam) => void { export default function ProposalDisplay (props: Props): React.ReactElement { const { apiDefaultTxSudo } = useApi(); - const { className, isDisabled, isError, label, onEnter, style, withLabel } = props; + const { className, isDisabled, isError, label, onEnter, onEscape, style, withLabel } = props; return ( diff --git a/packages/react-components/src/Password.tsx b/packages/react-components/src/Password.tsx index 66ac174923..35bcf4566b 100644 --- a/packages/react-components/src/Password.tsx +++ b/packages/react-components/src/Password.tsx @@ -22,12 +22,13 @@ interface Props extends BareProps { name?: string; onChange: (value: string) => void; onEnter?: () => void; + onEscape?: () => void; tabIndex?: number; value: any; withLabel?: boolean; } -export default function Password ({ autoFocus, children, className, defaultValue, help, isDisabled, isError, label, name, onChange, onEnter, style, tabIndex, value, withLabel }: Props): React.ReactElement { +export default function Password ({ autoFocus, children, className, defaultValue, help, isDisabled, isError, label, name, onChange, onEnter, onEscape, style, tabIndex, value, withLabel }: Props): React.ReactElement { const [isVisible, setIsVisible] = useState(false); const _toggleVisible = (): void => setIsVisible(!isVisible); @@ -46,6 +47,7 @@ export default function Password ({ autoFocus, children, className, defaultValue name={name} onChange={onChange} onEnter={onEnter} + onEscape={onEscape} style={style} tabIndex={tabIndex} type={ diff --git a/packages/react-components/src/TxButton.tsx b/packages/react-components/src/TxButton.tsx index a29cfc9159..cbd7ba86c5 100644 --- a/packages/react-components/src/TxButton.tsx +++ b/packages/react-components/src/TxButton.tsx @@ -3,51 +3,22 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { SubmittableExtrinsic } from '@polkadot/api/promise/types'; -import { ApiProps } from '@polkadot/react-api/types'; import { assert, isFunction, isUndefined } from '@polkadot/util'; -import { Index } from '@polkadot/types/interfaces'; -import { IExtrinsic } from '@polkadot/types/types'; -import { QueueTx, QueueTxExtrinsicAdd, TxCallback, TxFailedCallback } from './Status/types'; +import { QueueTx, QueueTxExtrinsicAdd } from './Status/types'; +import { TxButtonProps as Props } from './types'; import React from 'react'; import { SubmittableResult } from '@polkadot/api'; import { withApi } from '@polkadot/react-api'; import Button from './Button'; -import { Button$Sizes } from './Button/types'; import { QueueConsumer } from './Status/Context'; -type ConstructFn = () => any[]; - interface InjectedProps { queueExtrinsic: QueueTxExtrinsicAdd; txqueue: QueueTx[]; } -interface Props extends ApiProps { - accountId?: string; - accountNonce?: Index; - className?: string; - extrinsic?: IExtrinsic | SubmittableExtrinsic; - icon: string; - iconSize?: Button$Sizes; - isBasic?: boolean; - isDisabled?: boolean; - isNegative?: boolean; - isPrimary?: boolean; - isUnsigned?: boolean; - label: React.ReactNode; - onClick?: () => any; - onFailed?: TxFailedCallback; - onStart?: () => void; - onSuccess?: TxCallback; - onUpdate?: TxCallback; - params?: any[] | ConstructFn; - tooltip?: string; - tx?: string; - withSpinner?: boolean; -} - type InnerProps = Props & InjectedProps; interface State { @@ -61,7 +32,10 @@ class TxButtonInner extends React.PureComponent { }; public render (): React.ReactNode { - const { accountId, className, icon, iconSize, isBasic, isDisabled, isNegative, isPrimary, isUnsigned, label, tooltip } = this.props; + const { accountId, className, icon, iconSize, innerRef, isBasic, isDisabled, isNegative, isPrimary, isUnsigned, label, tooltip } = this.props; + + assert(!!this.props.extrinsic || (!!this.props.params && !!this.props.tx), 'TxButton: Invalid extrinsic provided'); + const { isSending } = this.state; const needsAccount = isUnsigned ? false @@ -83,6 +57,7 @@ class TxButtonInner extends React.PureComponent { } label={label} onClick={this.send} + ref={innerRef} size={iconSize} /> ); @@ -146,14 +121,15 @@ class TxButton extends React.PureComponent { protected button: any = React.createRef(); public render (): React.ReactNode { + const { innerRef, ...props } = this.props; return ( {({ queueExtrinsic, txqueue }): React.ReactNode => ( )} diff --git a/packages/react-components/src/TxModalNew.tsx b/packages/react-components/src/TxModalNew.tsx new file mode 100644 index 0000000000..7be0efd671 --- /dev/null +++ b/packages/react-components/src/TxModalNew.tsx @@ -0,0 +1,138 @@ +// Copyright 2017-2019 @polkadot/app-contracts 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 { TxModalProps as Props } from './types'; + +import React, { useState, useEffect } from 'react'; +import { Button, InputAddress, Modal } from '@polkadot/react-components'; +// import { useTx } from '@polkadot/react-hooks'; +import { isUndefined } from '@polkadot/util'; + +import translate from './translate'; + +function TxModal

({ + t, + accountId, + onChangeAccountId, + sendTx, + isSending, + trigger: Trigger, + header = t('Submit signed extrinsic'), + content, + preContent, + isDisabled = false, + isSubmittable = true, + modalProps = {}, + inputAddressExtra, + inputAddressLabel = t('using my account'), + inputAddressHelp = t('Select the account to use for this action.'), + inputAddressProps = {}, + cancelButtonLabel = t('Cancel'), + submitButtonIcon = 'sign-in', + submitButtonLabel = t('Submit'), + submitButtonProps = {}, + ...props +}: P): React.ReactElement

| null { + const isControlled = !isUndefined(props.isOpen); + const [isOpen, setIsOpen] = useState(isControlled ? props.isOpen : false); + + const onOpen = (): void => { + !isControlled && setIsOpen(true); + + props.onOpen && props.onOpen(); + }; + + const onClose = (): void => { + !isControlled && setIsOpen(false); + + props.onClose && props.onClose(); + }; + + // const onStart = (): void => { + // props.onSubmit && props.onSubmit(); + // }; + + // const onFailed = (): void => { + // props.onFailed && props.onFailed(); + // }; + + // const onSuccess = (): void => { + // !isControlled && setIsOpen(false); + + // props.onSuccess && props.onSuccess(); + // }; + + // const txState = useTx( + // txSource, + // { + // ...(props.accountId ? { accountId: props.accountId } : {}), + // onChangeAccountId: props.onChangeAccountId, + // onStart, + // onFailed, + // onSuccess + // } + // ); + + // const { accountId, onChangeAccountId, isSending, sendTx } = th + + useEffect((): void => { + !isUndefined(props.isOpen) && setIsOpen(props.isOpen); + }, [props.isOpen]); + + const allModalProps = { + className: ['ui--Modal', modalProps.className || ''].join(' '), + dimmer: 'inverted', + ...modalProps, + onClose, + open: isOpen + }; + + return ( + <> + {Trigger ? : null} + + + {header} + + + {preContent} + + {content} + + + +