ghost-portal/packages/react-params/src/Param/Code.tsx
2021-01-03 11:33:39 +01:00

54 lines
1.2 KiB
TypeScript

// Copyright 2017-2021 @polkadot/react-params authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Props } from '../types';
import React, { useCallback, useState } from 'react';
import { isWasm } from '@polkadot/util';
import Bytes from './Bytes';
import BytesFile from './File';
function Code ({ className = '', defaultValue, isDisabled, isError, label, onChange, onEnter, onEscape, type, withLabel }: Props): React.ReactElement<Props> {
const [isValid, setIsValid] = useState(false);
const _onChange = useCallback(
(value: Uint8Array): void => {
const isValid = isWasm(value);
onChange && onChange({ isValid, value });
setIsValid(isValid);
},
[onChange]
);
if (isDisabled) {
return (
<Bytes
className={className}
defaultValue={defaultValue}
isError={isError || !isValid}
label={label}
onEnter={onEnter}
onEscape={onEscape}
type={type}
withLabel={withLabel}
/>
);
}
return (
<BytesFile
className={className}
defaultValue={defaultValue}
isError={isError || !isValid}
label={label}
onChange={_onChange}
withLabel={withLabel}
/>
);
}
export default React.memo(Code);