Allow file to be hashed (#5226)

* Allow file to be hashed

* isInOption

* Double-option wrapping

* Option revert
This commit is contained in:
Jaco 2021-05-07 18:16:47 +03:00 committed by GitHub
parent 288f75f380
commit 83d615003c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,28 +3,80 @@
import type { Props } from '../types';
import React from 'react';
import React, { useCallback, useState } from 'react';
import { Toggle } from '@polkadot/react-components';
import { u8aToHex } from '@polkadot/util';
import { useTranslation } from '../translate';
import BaseBytes from './BaseBytes';
import File from './File';
function Hash256 ({ className = '', defaultValue, isDisabled, isError, isInOption, label, name, onChange, onEnter, onEscape, registry, type, withLabel }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [isFileDrop, setFileInput] = useState(false);
const [placeholder, setPlaceholder] = useState<string | null>(null);
const _onChangeFile = useCallback(
(u8a: Uint8Array): void => {
const value = registry.hash(u8a);
setPlaceholder(u8aToHex(value));
onChange && onChange({
isValid: true,
value
});
},
[onChange, registry]
);
const _setFileInput = useCallback(
(value: boolean): void => {
setPlaceholder(null);
setFileInput(value);
},
[setFileInput, setPlaceholder]
);
function Hash256 ({ className = '', defaultValue, isDisabled, isError, label, name, onChange, onEnter, onEscape, type, withLabel }: Props): React.ReactElement<Props> {
return (
<BaseBytes
asHex
className={className}
defaultValue={defaultValue}
isDisabled={isDisabled}
isError={isError}
label={label}
length={32}
name={name}
onChange={onChange}
onEnter={onEnter}
onEscape={onEscape}
type={type}
withCopy={isDisabled}
withLabel={withLabel}
/>
<div className={className}>
{!isDisabled && isFileDrop
? (
<File
isDisabled={isDisabled}
isError={isError}
label={label}
onChange={_onChangeFile}
placeholder={placeholder || undefined}
withLabel={withLabel}
/>
)
: (
<BaseBytes
asHex
defaultValue={defaultValue}
isDisabled={isDisabled}
isError={isError}
label={label}
length={32}
name={name}
onChange={onChange}
onEnter={onEnter}
onEscape={onEscape}
type={type}
withCopy={isDisabled}
withLabel={withLabel}
/>
)}
{!isDisabled && !isInOption && (
<Toggle
isOverlay
label={t<string>('hash a file')}
onChange={_setFileInput}
value={isFileDrop}
/>
)}
</div>
);
}