From 83d615003c660e879de666814470b0cb0a275efb Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 7 May 2021 18:16:47 +0300 Subject: [PATCH] Allow file to be hashed (#5226) * Allow file to be hashed * isInOption * Double-option wrapping * Option revert --- packages/react-params/src/Param/Hash256.tsx | 88 ++++++++++++++++----- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/packages/react-params/src/Param/Hash256.tsx b/packages/react-params/src/Param/Hash256.tsx index 208c29b5ea..99f37e5008 100644 --- a/packages/react-params/src/Param/Hash256.tsx +++ b/packages/react-params/src/Param/Hash256.tsx @@ -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 { + const { t } = useTranslation(); + const [isFileDrop, setFileInput] = useState(false); + const [placeholder, setPlaceholder] = useState(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 { return ( - +
+ {!isDisabled && isFileDrop + ? ( + + ) + : ( + + )} + {!isDisabled && !isInOption && ( + ('hash a file')} + onChange={_setFileInput} + value={isFileDrop} + /> + )} +
); }