|
| 1 | +import React, { FC, Fragment } from 'react'; |
| 2 | +import { useDropzone, DropzoneState } from 'react-dropzone'; |
| 3 | + |
| 4 | +import { makeStyles, createStyles } from '@material-ui/styles'; |
| 5 | +import CloudUploadIcon from '@material-ui/icons/CloudUpload'; |
| 6 | +import CancelIcon from '@material-ui/icons/Cancel'; |
| 7 | +import { Theme, Box, Typography, LinearProgress, Button } from '@material-ui/core'; |
| 8 | + |
| 9 | +interface SingleUploadStyleProps extends DropzoneState { |
| 10 | + uploading: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +const progressPercentage = (progress: ProgressEvent) => Math.round((progress.loaded * 100) / progress.total); |
| 14 | + |
| 15 | +const getBorderColor = (theme: Theme, props: SingleUploadStyleProps) => { |
| 16 | + if (props.isDragAccept) { |
| 17 | + return theme.palette.success.main; |
| 18 | + } |
| 19 | + if (props.isDragReject) { |
| 20 | + return theme.palette.error.main; |
| 21 | + } |
| 22 | + if (props.isDragActive) { |
| 23 | + return theme.palette.info.main; |
| 24 | + } |
| 25 | + return theme.palette.grey[700]; |
| 26 | +} |
| 27 | + |
| 28 | +const useStyles = makeStyles((theme: Theme) => createStyles({ |
| 29 | + dropzone: { |
| 30 | + padding: theme.spacing(8, 2), |
| 31 | + borderWidth: 2, |
| 32 | + borderRadius: 2, |
| 33 | + borderStyle: 'dashed', |
| 34 | + color: theme.palette.grey[700], |
| 35 | + transition: 'border .24s ease-in-out', |
| 36 | + cursor: (props: SingleUploadStyleProps) => props.uploading ? 'default' : 'pointer', |
| 37 | + width: '100%', |
| 38 | + borderColor: (props: SingleUploadStyleProps) => getBorderColor(theme, props) |
| 39 | + } |
| 40 | +})); |
| 41 | + |
| 42 | +export interface SingleUploadProps { |
| 43 | + onDrop: (acceptedFiles: File[]) => void; |
| 44 | + onCancel: () => void; |
| 45 | + accept?: string | string[]; |
| 46 | + uploading: boolean; |
| 47 | + progress?: ProgressEvent; |
| 48 | +} |
| 49 | + |
| 50 | +const SingleUpload: FC<SingleUploadProps> = ({ onDrop, onCancel, accept, uploading, progress }) => { |
| 51 | + const dropzoneState = useDropzone({ onDrop, accept, disabled: uploading, multiple: false }); |
| 52 | + const { getRootProps, getInputProps } = dropzoneState; |
| 53 | + const classes = useStyles({ ...dropzoneState, uploading }); |
| 54 | + |
| 55 | + |
| 56 | + const renderProgressText = () => { |
| 57 | + if (uploading) { |
| 58 | + if (progress?.lengthComputable) { |
| 59 | + return `Uploading: ${progressPercentage(progress)}%`; |
| 60 | + } |
| 61 | + return "Uploading\u2026"; |
| 62 | + } |
| 63 | + return "Drop file here or click to browse"; |
| 64 | + } |
| 65 | + |
| 66 | + const renderProgress = (progress?: ProgressEvent) => ( |
| 67 | + <LinearProgress |
| 68 | + variant={!progress || progress.lengthComputable ? "determinate" : "indeterminate"} |
| 69 | + value={!progress ? 0 : progress.lengthComputable ? progressPercentage(progress) : 0} |
| 70 | + /> |
| 71 | + ); |
| 72 | + |
| 73 | + return ( |
| 74 | + <div {...getRootProps({ className: classes.dropzone })}> |
| 75 | + <input {...getInputProps()} /> |
| 76 | + <Box flexDirection="column" display="flex" alignItems="center"> |
| 77 | + <CloudUploadIcon fontSize='large' /> |
| 78 | + <Typography variant="h6"> |
| 79 | + {renderProgressText()} |
| 80 | + </Typography> |
| 81 | + {uploading && ( |
| 82 | + <Fragment> |
| 83 | + <Box width="100%" p={2}> |
| 84 | + {renderProgress(progress)} |
| 85 | + </Box> |
| 86 | + <Button startIcon={<CancelIcon />} variant="contained" color="secondary" onClick={onCancel}> |
| 87 | + Cancel |
| 88 | + </Button> |
| 89 | + </Fragment> |
| 90 | + )} |
| 91 | + </Box> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +export default SingleUpload; |
0 commit comments