aboutsummaryrefslogtreecommitdiff
path: root/src/components/FileUpload
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-08-22 14:56:07 +0300
committerGitHub <noreply@github.com>2020-08-22 14:56:07 +0300
commit89f038c7a0ccf6de94516cba8499a0bc69f8dae1 (patch)
tree5e6dbd7ae5a9d0ce1b79921b2b44986bb13f1874 /src/components/FileUpload
parenta42667af463b8c33a38b935b96d39582b543790b (diff)
parent93319d38e904535ce33a7868b3c1e0a2a4f33d65 (diff)
downloadwhich-ui-89f038c7a0ccf6de94516cba8499a0bc69f8dae1.tar.gz
Merge pull request #93 from which-ecosystem/avatar-uploads
Avatar uploads
Diffstat (limited to 'src/components/FileUpload')
-rw-r--r--src/components/FileUpload/FileUpload.tsx36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/components/FileUpload/FileUpload.tsx b/src/components/FileUpload/FileUpload.tsx
index 67d280d..961fa9a 100644
--- a/src/components/FileUpload/FileUpload.tsx
+++ b/src/components/FileUpload/FileUpload.tsx
@@ -1,28 +1,29 @@
-import React, { useEffect } from 'react';
-import { useFilePicker, utils } from 'react-sage';
+import React, { useRef } from 'react';
import Button from '@material-ui/core/Button';
import CloudUpload from '@material-ui/icons/CloudUpload';
interface PropTypes {
- callback: (fileUrl: string, file: File) => void;
+ callback: (file: File) => void;
}
const FileUpload: React.FC<PropTypes> = ({ callback, children }) => {
- const { files, onClick, HiddenFileInput } = useFilePicker();
+ const inputRef = useRef<HTMLInputElement>(null);
- useEffect(() => {
- if (files?.length) {
- const file = files[0];
- utils.loadFile(file).then(url => callback(url, file));
- }
- }, [files, callback]);
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+ const files = event.target?.files;
+ if (files?.length) callback(files[0]);
+ };
- const child = children && React.Children.toArray(children)[0];
+ const handleClick = () => {
+ if (inputRef?.current) inputRef.current.click();
+ };
+
+ const child = children && React.Children.only(children);
const defaultButton = (
<Button
- onClick={onClick}
+ onClick={handleClick}
variant="contained"
color="primary"
size="large"
@@ -34,10 +35,17 @@ const FileUpload: React.FC<PropTypes> = ({ callback, children }) => {
return (
<>
- <HiddenFileInput accept=".jpg, .jpeg, .png, .gif" multiple={false} />
+ <input
+ type="file"
+ ref={inputRef}
+ multiple={false}
+ accept=".jpg, .jpeg, .png, .gif"
+ style={{ display: 'none' }}
+ onChange={handleChange}
+ />
{
React.isValidElement(child)
- ? React.cloneElement(child, { onClick })
+ ? React.cloneElement(child, { onClick: handleClick })
: defaultButton
}
</>