ghost-portal/packages/react-components/src/InputFile.tsx
Jaco Greeff 2deb69682f
Adjust buttons based on ABI availability (#3839)
* Adjust buttons based on ABI availability

* Sorting
2020-10-10 12:02:15 +02:00

153 lines
4.1 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { ThemeProps } from './types';
import React, { useCallback, useState, createRef } from 'react';
import Dropzone, { DropzoneRef } from 'react-dropzone';
import styled from 'styled-components';
import { formatNumber, isHex, u8aToString, hexToU8a } from '@polkadot/util';
import Labelled from './Labelled';
import { useTranslation } from './translate';
export interface InputFileProps {
// Reference Example Usage: https://github.com/react-dropzone/react-dropzone/tree/master/examples/Accept
// i.e. MIME types: 'application/json, text/plain', or '.json, .txt'
accept?: string;
className?: string;
clearContent?: boolean;
help?: React.ReactNode;
isDisabled?: boolean;
isError?: boolean;
isFull?: boolean;
label: React.ReactNode;
onChange?: (contents: Uint8Array, name: string) => void;
placeholder?: React.ReactNode | null;
withEllipsis?: boolean;
withLabel?: boolean;
}
interface FileState {
name: string;
size: number;
}
const BYTE_STR_0 = '0'.charCodeAt(0);
const BYTE_STR_X = 'x'.charCodeAt(0);
const STR_NL = '\n';
const NOOP = (): void => undefined;
function convertResult (result: ArrayBuffer): Uint8Array {
const data = new Uint8Array(result);
// this converts the input (if detected as hex), via the hex conversion route
if (data[0] === BYTE_STR_0 && data[1] === BYTE_STR_X) {
let hex = u8aToString(data);
while (hex[hex.length - 1] === STR_NL) {
hex = hex.substr(0, hex.length - 1);
}
if (isHex(hex)) {
return hexToU8a(hex);
}
}
return data;
}
function InputFile ({ accept, className = '', clearContent, help, isDisabled, isError = false, isFull, label, onChange, placeholder, withEllipsis, withLabel }: InputFileProps): React.ReactElement<InputFileProps> {
const { t } = useTranslation();
const dropRef = createRef<DropzoneRef>();
const [file, setFile] = useState<FileState | undefined>();
const _onDrop = useCallback(
(files: File[]): void => {
files.forEach((file): void => {
const reader = new FileReader();
reader.onabort = NOOP;
reader.onerror = NOOP;
reader.onload = ({ target }: ProgressEvent<FileReader>): void => {
if (target && target.result) {
const name = file.name;
const data = convertResult(target.result as ArrayBuffer);
onChange && onChange(data, name);
dropRef && setFile({
name,
size: data.length
});
}
};
reader.readAsArrayBuffer(file);
});
},
[dropRef, onChange]
);
const dropZone = (
<Dropzone
accept={accept}
disabled={isDisabled}
multiple={false}
onDrop={_onDrop}
ref={dropRef}
>
{({ getInputProps, getRootProps }): JSX.Element => (
<div {...getRootProps({ className: `ui--InputFile${isError ? ' error' : ''} ${className}` })} >
<input {...getInputProps()} />
<em className='label' >
{
!file || clearContent
? placeholder || t<string>('click to select or drag and drop the file here')
: placeholder || t<string>('{{name}} ({{size}} bytes)', {
replace: {
name: file.name,
size: formatNumber(file.size)
}
})
}
</em>
</div>
)}
</Dropzone>
);
return label
? (
<Labelled
help={help}
isFull={isFull}
label={label}
withEllipsis={withEllipsis}
withLabel={withLabel}
>
{dropZone}
</Labelled>
)
: dropZone;
}
export default React.memo(styled(InputFile)(({ theme }: ThemeProps) => `
background: ${theme.bgInput};
border: 1px solid rgba(34, 36, 38, 0.15);
border-radius: 0.28571429rem;
font-size: 1rem;
margin: 0.25rem 0;
padding: 0.67857143em 1em;
width: 100% !important;
&.error {
background: ${theme.bgInputError};
border-color: #e0b4b4;
}
&:hover {
cursor: pointer;
}
`));