Adjust Option<Bytes> encoding (per Option) (#8616)
This commit is contained in:
parent
999b9a5873
commit
f5b62540b3
@ -66,7 +66,6 @@ function expandParams (registry: Registry, st: StorageEntryTypeLatest, isIterabl
|
|||||||
if (isIterable && index === (types.length - 1)) {
|
if (isIterable && index === (types.length - 1)) {
|
||||||
type = getTypeDef(`Option<${str}>`);
|
type = getTypeDef(`Option<${str}>`);
|
||||||
type.withOptionActive = true;
|
type.withOptionActive = true;
|
||||||
type.withOptionNaked = true;
|
|
||||||
} else {
|
} else {
|
||||||
type = getTypeDef(str);
|
type = getTypeDef(str);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import React, { useCallback, useState } from 'react';
|
|||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import { CopyButton, IdentityIcon, Input } from '@polkadot/react-components';
|
import { CopyButton, IdentityIcon, Input } from '@polkadot/react-components';
|
||||||
import { compactAddLength, hexToU8a, isAscii, isHex, stringToU8a, u8aConcat, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
|
import { compactAddLength, hexToU8a, isAscii, isHex, stringToU8a, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
|
||||||
import { decodeAddress } from '@polkadot/util-crypto';
|
import { decodeAddress } from '@polkadot/util-crypto';
|
||||||
|
|
||||||
import { useTranslation } from '../translate';
|
import { useTranslation } from '../translate';
|
||||||
@ -33,7 +33,6 @@ interface Props {
|
|||||||
withCopy?: boolean;
|
withCopy?: boolean;
|
||||||
withLabel?: boolean;
|
withLabel?: boolean;
|
||||||
withLength?: boolean;
|
withLength?: boolean;
|
||||||
withOptionNaked?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Validity {
|
interface Validity {
|
||||||
@ -68,7 +67,7 @@ function convertInput (value: string): [boolean, boolean, Uint8Array] {
|
|||||||
: [value === '0x', false, new Uint8Array([])];
|
: [value === '0x', false, new Uint8Array([])];
|
||||||
}
|
}
|
||||||
|
|
||||||
function BaseBytes ({ asHex, children, className = '', defaultValue: { value }, isDisabled, isError, isInOption, label, length = -1, onChange, onEnter, onEscape, size = 'full', type: { withOptionNaked }, validate = defaultValidate, withCopy, withLabel, withLength }: Props): React.ReactElement<Props> {
|
function BaseBytes ({ asHex, children, className = '', defaultValue: { value }, isDisabled, isError, label, length = -1, onChange, onEnter, onEscape, size = 'full', validate = defaultValidate, withCopy, withLabel, withLength }: Props): React.ReactElement<Props> {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [defaultValue] = useState(
|
const [defaultValue] = useState(
|
||||||
(): string | undefined => {
|
(): string | undefined => {
|
||||||
@ -102,10 +101,6 @@ function BaseBytes ({ asHex, children, className = '', defaultValue: { value },
|
|||||||
value = compactAddLength(value);
|
value = compactAddLength(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isInOption && isValid && !withOptionNaked) {
|
|
||||||
value = u8aConcat([1], value);
|
|
||||||
}
|
|
||||||
|
|
||||||
onChange && onChange({
|
onChange && onChange({
|
||||||
isValid,
|
isValid,
|
||||||
value: asHex
|
value: asHex
|
||||||
@ -115,7 +110,7 @@ function BaseBytes ({ asHex, children, className = '', defaultValue: { value },
|
|||||||
|
|
||||||
setValidity({ isAddress, isValid, lastValue: value });
|
setValidity({ isAddress, isValid, lastValue: value });
|
||||||
},
|
},
|
||||||
[asHex, isInOption, length, onChange, validate, withLength, withOptionNaked]
|
[asHex, length, onChange, validate, withLength]
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -2,21 +2,22 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import type { Codec, TypeDef } from '@polkadot/types/types';
|
import type { Codec, TypeDef } from '@polkadot/types/types';
|
||||||
import type { Props } from '../types';
|
import type { Props, RawParamOnChangeValue } from '../types';
|
||||||
|
|
||||||
import React, { useEffect, useMemo, useState } from 'react';
|
import React, { useCallback, useEffect, useState } from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import { Toggle } from '@polkadot/react-components';
|
import { Toggle } from '@polkadot/react-components';
|
||||||
import { Option } from '@polkadot/types';
|
import { Option } from '@polkadot/types';
|
||||||
import { objectSpread } from '@polkadot/util';
|
import { isU8a, u8aConcat } from '@polkadot/util';
|
||||||
|
|
||||||
import { useTranslation } from '../translate';
|
import { useTranslation } from '../translate';
|
||||||
import Param from './index';
|
import Param from './index';
|
||||||
|
|
||||||
const DEF_VALUE = { isValid: true, value: undefined };
|
const DEF_VALUE = { isValid: true, value: undefined };
|
||||||
|
const OPT_PREFIX = new Uint8Array([1]);
|
||||||
|
|
||||||
function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisabled, name, onChange, onEnter, onEscape, registry, type: { sub, withOptionActive, withOptionNaked } }: Props): React.ReactElement<Props> {
|
function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisabled, name, onChange, onEnter, onEscape, registry, type: { sub, withOptionActive } }: Props): React.ReactElement<Props> {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [isActive, setIsActive] = useState(() => withOptionActive || !!(_defaultValue && _defaultValue.value instanceof Option && _defaultValue.value.isSome) || false);
|
const [isActive, setIsActive] = useState(() => withOptionActive || !!(_defaultValue && _defaultValue.value instanceof Option && _defaultValue.value.isSome) || false);
|
||||||
const [defaultValue] = useState(
|
const [defaultValue] = useState(
|
||||||
@ -28,10 +29,6 @@ function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisable
|
|||||||
)
|
)
|
||||||
: DEF_VALUE
|
: DEF_VALUE
|
||||||
);
|
);
|
||||||
const subType = useMemo(
|
|
||||||
() => objectSpread<TypeDef>({}, sub, { withOptionNaked }),
|
|
||||||
[sub, withOptionNaked]
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect((): void => {
|
useEffect((): void => {
|
||||||
!isActive && onChange && onChange({
|
!isActive && onChange && onChange({
|
||||||
@ -40,19 +37,32 @@ function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisable
|
|||||||
});
|
});
|
||||||
}, [isActive, onChange]);
|
}, [isActive, onChange]);
|
||||||
|
|
||||||
|
const _onChange = useCallback(
|
||||||
|
(value: RawParamOnChangeValue) =>
|
||||||
|
onChange && onChange(
|
||||||
|
value.isValid && isU8a(value.value) && !withOptionActive && isActive
|
||||||
|
? { isValid: true, value: u8aConcat(OPT_PREFIX, value.value) }
|
||||||
|
: value
|
||||||
|
),
|
||||||
|
[isActive, onChange, withOptionActive]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={className}>
|
<div className={className}>
|
||||||
<Param
|
<Param
|
||||||
defaultValue={isActive ? defaultValue : DEF_VALUE}
|
defaultValue={
|
||||||
|
isActive
|
||||||
|
? defaultValue
|
||||||
|
: DEF_VALUE}
|
||||||
isDisabled={isDisabled || !isActive}
|
isDisabled={isDisabled || !isActive}
|
||||||
isInOption
|
isInOption
|
||||||
isOptional={!isActive && !isDisabled}
|
isOptional={!isActive && !isDisabled}
|
||||||
name={name}
|
name={name}
|
||||||
onChange={onChange}
|
onChange={_onChange}
|
||||||
onEnter={onEnter}
|
onEnter={onEnter}
|
||||||
onEscape={onEscape}
|
onEscape={onEscape}
|
||||||
registry={registry}
|
registry={registry}
|
||||||
type={subType}
|
type={sub as TypeDef}
|
||||||
/>
|
/>
|
||||||
{!isDisabled && (
|
{!isDisabled && (
|
||||||
<Toggle
|
<Toggle
|
||||||
|
|||||||
@ -69,5 +69,4 @@ export interface ExpandedCid {
|
|||||||
|
|
||||||
export interface TypeDefExt extends TypeDef {
|
export interface TypeDefExt extends TypeDef {
|
||||||
withOptionActive?: boolean;
|
withOptionActive?: boolean;
|
||||||
withOptionNaked?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user