Make InputAddressSimple slightly more user-friendly (#8756)

This commit is contained in:
Jaco 2023-01-11 07:38:40 +02:00 committed by GitHub
parent eff5d0da97
commit e841aab243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import { toAddress } from './util';
interface Props {
autoFocus?: boolean;
bytesLength?: 20 | 32;
children?: React.ReactNode;
className?: string;
defaultValue?: string | null;
@ -23,14 +24,15 @@ interface Props {
onChange?: (address: string | null) => void;
onEnter?: () => void;
onEscape?: () => void;
placeholder?: string;
}
function InputAddressSimple ({ autoFocus, children, className = '', defaultValue, forceIconType, help, isDisabled, isError, isFull, label, noConvert, onChange, onEnter, onEscape }: Props): React.ReactElement<Props> {
function InputAddressSimple ({ autoFocus, bytesLength, children, className = '', defaultValue, forceIconType, help, isDisabled, isError, isFull, label, noConvert, onChange, onEnter, onEscape, placeholder }: Props): React.ReactElement<Props> {
const [address, setAddress] = useState<string | null>(defaultValue || null);
const _onChange = useCallback(
(_address: string): void => {
const address = toAddress(_address) || null;
const address = toAddress(_address, undefined, bytesLength) || null;
const output = noConvert
? address
? _address
@ -40,7 +42,7 @@ function InputAddressSimple ({ autoFocus, children, className = '', defaultValue
setAddress(output);
onChange && onChange(output);
},
[noConvert, onChange]
[bytesLength, noConvert, onChange]
);
return (
@ -56,6 +58,7 @@ function InputAddressSimple ({ autoFocus, children, className = '', defaultValue
onChange={_onChange}
onEnter={onEnter}
onEscape={onEscape}
placeholder={placeholder}
>
{children}
</Input>

View File

@ -2,17 +2,21 @@
// SPDX-License-Identifier: Apache-2.0
import { keyring } from '@polkadot/ui-keyring';
import { assert, hexToU8a, isHex } from '@polkadot/util';
import { hexToU8a, isHex } from '@polkadot/util';
import { ethereumEncode } from '@polkadot/util-crypto';
export function toAddress (value?: string | Uint8Array | null, allowIndices = false): string | undefined {
export function toAddress (value?: string | Uint8Array | null, allowIndices = false, bytesLength?: 20 | 32): string | undefined {
if (value) {
try {
const u8a = isHex(value)
? hexToU8a(value)
: keyring.decodeAddress(value);
assert(allowIndices || u8a.length === 32 || u8a.length === 20, 'AccountIndex values not allowed');
if (!allowIndices && u8a.length !== 32 && u8a.length !== 20) {
throw new Error('AccountIndex values not allowed');
} else if (bytesLength && u8a.length !== bytesLength) {
throw new Error('Invalid key length');
}
if (u8a.length === 20) {
return ethereumEncode(u8a);

View File

@ -6,7 +6,6 @@ import type { Props as BaseProps } from '../types';
import React, { useCallback, useState } from 'react';
import { InputAddressSimple } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { isEthereumAddress, validateAddress } from '@polkadot/util-crypto';
import Bare from './Bare';
@ -34,7 +33,6 @@ function isValidAddress (value: string | null | undefined, isEthereum: boolean):
}
function BasicAccountIdBase (props: Props): React.ReactElement<Props> {
const { isEthereum } = useApi();
const { bytesLength, className = '', defaultValue: { value }, isDisabled, isError, isInOption, label, onChange, withLabel } = props;
const [defaultValue] = useState(() => (value as string)?.toString());
@ -50,6 +48,7 @@ function BasicAccountIdBase (props: Props): React.ReactElement<Props> {
return (
<Bare className={className}>
<InputAddressSimple
bytesLength={bytesLength}
className='full'
defaultValue={defaultValue}
forceIconType={bytesLength === 20 ? 'ethereum' : 'substrate'}
@ -60,7 +59,7 @@ function BasicAccountIdBase (props: Props): React.ReactElement<Props> {
label={label}
noConvert
onChange={_onChange}
placeholder={isEthereum ? '5...' : '0x1...'}
placeholder={bytesLength === 20 ? '0x1...' : '5...'}
withLabel={withLabel}
/>
</Bare>

View File

@ -96,6 +96,8 @@ export default function getInitValue (registry: Registry, def: TypeDef): unknown
return '';
case 'AccountId':
case 'AccountId20':
case 'AccountId32':
case 'AccountIdOf':
case 'Address':
case 'Call':