ghost-portal/packages/react-params/src/Param/Code.tsx
Tarik Gul 384049fb6b
2025 (#11172)
* 2025

* update ci scripts to add access to scripts
2025-01-03 03:59:53 +02:00

55 lines
1.3 KiB
TypeScript

// Copyright 2017-2025 @polkadot/react-params authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Props } from '../types.js';
import React, { useCallback, useState } from 'react';
import { isWasm } from '@polkadot/util';
import Bytes from './Bytes.js';
import BytesFile from './File.js';
function Code ({ className = '', defaultValue, isDisabled, isError, label, onChange, onEnter, onEscape, registry, 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}
registry={registry}
type={type}
withLabel={withLabel}
/>
);
}
return (
<BytesFile
className={className}
defaultValue={defaultValue}
isError={isError || !isValid}
label={label}
onChange={_onChange}
withLabel={withLabel}
/>
);
}
export default React.memo(Code);