aboutsummaryrefslogtreecommitdiff
path: root/src/components/FileUpload
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-08-22 00:43:22 +0300
committereug-vs <eug-vs@keemail.me>2020-08-22 00:43:22 +0300
commit551928f9fe5542dd91c9a8578124b4fb05fb9be2 (patch)
treede89c99a72f9f4a5984d40f46aeea7c8c28229dd /src/components/FileUpload
parenta42667af463b8c33a38b935b96d39582b543790b (diff)
downloadwhich-ui-551928f9fe5542dd91c9a8578124b4fb05fb9be2.tar.gz
refactor: rewrite FileUpload without react-sage
Diffstat (limited to 'src/components/FileUpload')
-rw-r--r--src/components/FileUpload/FileUpload.tsx30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/components/FileUpload/FileUpload.tsx b/src/components/FileUpload/FileUpload.tsx
index 67d280d..1c05e7f 100644
--- a/src/components/FileUpload/FileUpload.tsx
+++ b/src/components/FileUpload/FileUpload.tsx
@@ -1,5 +1,5 @@
-import React, { useEffect } from 'react';
-import { useFilePicker, utils } from 'react-sage';
+import React, { useRef } from 'react';
+import { utils } from 'react-sage';
import Button from '@material-ui/core/Button';
import CloudUpload from '@material-ui/icons/CloudUpload';
@@ -9,20 +9,25 @@ interface PropTypes {
const FileUpload: React.FC<PropTypes> = ({ callback, children }) => {
- const { files, onClick, HiddenFileInput } = useFilePicker();
+ const inputRef = useRef<HTMLInputElement>(null);
- useEffect(() => {
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+ const files = event.target?.files;
if (files?.length) {
const file = files[0];
utils.loadFile(file).then(url => callback(url, file));
- }
- }, [files, callback]);
+ };
+ };
+
+ const handleClick = () => {
+ if (inputRef?.current) inputRef.current.click();
+ };
const child = children && React.Children.toArray(children)[0];
const defaultButton = (
<Button
- onClick={onClick}
+ onClick={handleClick}
variant="contained"
color="primary"
size="large"
@@ -34,10 +39,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
}
</>