|
| 1 | +import { makeStyles } from "@material-ui/core/styles" |
| 2 | +import { Stack } from "components/Stack/Stack" |
| 3 | +import { FC, DragEvent, useRef, ReactNode } from "react" |
| 4 | +import UploadIcon from "@material-ui/icons/CloudUploadOutlined" |
| 5 | +import { useClickable } from "hooks/useClickable" |
| 6 | +import CircularProgress from "@material-ui/core/CircularProgress" |
| 7 | +import { combineClasses } from "utils/combineClasses" |
| 8 | +import IconButton from "@material-ui/core/IconButton" |
| 9 | +import RemoveIcon from "@material-ui/icons/DeleteOutline" |
| 10 | +import FileIcon from "@material-ui/icons/FolderOutlined" |
| 11 | + |
| 12 | +const useFileDrop = ( |
| 13 | + callback: (file: File) => void, |
| 14 | + fileTypeRequired?: string, |
| 15 | +): { |
| 16 | + onDragOver: (e: DragEvent<HTMLDivElement>) => void |
| 17 | + onDrop: (e: DragEvent<HTMLDivElement>) => void |
| 18 | +} => { |
| 19 | + const onDragOver = (e: DragEvent<HTMLDivElement>) => { |
| 20 | + e.preventDefault() |
| 21 | + } |
| 22 | + |
| 23 | + const onDrop = (e: DragEvent<HTMLDivElement>) => { |
| 24 | + e.preventDefault() |
| 25 | + const file = e.dataTransfer.files[0] |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- file can be undefined |
| 27 | + if (!file) { |
| 28 | + return |
| 29 | + } |
| 30 | + if (fileTypeRequired && file.type !== fileTypeRequired) { |
| 31 | + return |
| 32 | + } |
| 33 | + callback(file) |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + onDragOver, |
| 38 | + onDrop, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +export interface FileUploadProps { |
| 43 | + isUploading: boolean |
| 44 | + onUpload: (file: File) => void |
| 45 | + onRemove?: () => void |
| 46 | + file?: File |
| 47 | + removeLabel: string |
| 48 | + title: string |
| 49 | + description?: ReactNode |
| 50 | + extension?: string |
| 51 | + fileTypeRequired?: string |
| 52 | +} |
| 53 | + |
| 54 | +export const FileUpload: FC<FileUploadProps> = ({ |
| 55 | + isUploading, |
| 56 | + onUpload, |
| 57 | + onRemove, |
| 58 | + file, |
| 59 | + removeLabel, |
| 60 | + title, |
| 61 | + description, |
| 62 | + extension, |
| 63 | + fileTypeRequired, |
| 64 | +}) => { |
| 65 | + const styles = useStyles() |
| 66 | + const inputRef = useRef<HTMLInputElement>(null) |
| 67 | + const tarDrop = useFileDrop(onUpload, fileTypeRequired) |
| 68 | + const clickable = useClickable(() => { |
| 69 | + if (inputRef.current) { |
| 70 | + inputRef.current.click() |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + if (!isUploading && file) { |
| 75 | + return ( |
| 76 | + <Stack |
| 77 | + className={styles.file} |
| 78 | + direction="row" |
| 79 | + justifyContent="space-between" |
| 80 | + alignItems="center" |
| 81 | + > |
| 82 | + <Stack direction="row" alignItems="center"> |
| 83 | + <FileIcon /> |
| 84 | + <span>{file.name}</span> |
| 85 | + </Stack> |
| 86 | + |
| 87 | + <IconButton title={removeLabel} size="small" onClick={onRemove}> |
| 88 | + <RemoveIcon /> |
| 89 | + </IconButton> |
| 90 | + </Stack> |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + return ( |
| 95 | + <> |
| 96 | + <div |
| 97 | + className={combineClasses({ |
| 98 | + [styles.root]: true, |
| 99 | + [styles.disabled]: isUploading, |
| 100 | + })} |
| 101 | + {...clickable} |
| 102 | + {...tarDrop} |
| 103 | + > |
| 104 | + <Stack alignItems="center" spacing={1}> |
| 105 | + {isUploading ? ( |
| 106 | + <CircularProgress size={32} /> |
| 107 | + ) : ( |
| 108 | + <UploadIcon className={styles.icon} /> |
| 109 | + )} |
| 110 | + |
| 111 | + <Stack alignItems="center" spacing={0.5}> |
| 112 | + <span className={styles.title}>{title}</span> |
| 113 | + <span className={styles.description}>{description}</span> |
| 114 | + </Stack> |
| 115 | + </Stack> |
| 116 | + </div> |
| 117 | + |
| 118 | + <input |
| 119 | + type="file" |
| 120 | + ref={inputRef} |
| 121 | + className={styles.input} |
| 122 | + accept={extension} |
| 123 | + onChange={(event) => { |
| 124 | + const file = event.currentTarget.files?.[0] |
| 125 | + if (file) { |
| 126 | + onUpload(file) |
| 127 | + } |
| 128 | + }} |
| 129 | + /> |
| 130 | + </> |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +const useStyles = makeStyles((theme) => ({ |
| 135 | + root: { |
| 136 | + display: "flex", |
| 137 | + alignItems: "center", |
| 138 | + justifyContent: "center", |
| 139 | + borderRadius: theme.shape.borderRadius, |
| 140 | + border: `2px dashed ${theme.palette.divider}`, |
| 141 | + padding: theme.spacing(6), |
| 142 | + cursor: "pointer", |
| 143 | + |
| 144 | + "&:hover": { |
| 145 | + backgroundColor: theme.palette.background.paper, |
| 146 | + }, |
| 147 | + }, |
| 148 | + |
| 149 | + disabled: { |
| 150 | + pointerEvents: "none", |
| 151 | + opacity: 0.75, |
| 152 | + }, |
| 153 | + |
| 154 | + icon: { |
| 155 | + fontSize: theme.spacing(8), |
| 156 | + }, |
| 157 | + |
| 158 | + title: { |
| 159 | + fontSize: theme.spacing(2), |
| 160 | + }, |
| 161 | + |
| 162 | + description: { |
| 163 | + color: theme.palette.text.secondary, |
| 164 | + textAlign: "center", |
| 165 | + maxWidth: theme.spacing(50), |
| 166 | + }, |
| 167 | + |
| 168 | + input: { |
| 169 | + display: "none", |
| 170 | + }, |
| 171 | + |
| 172 | + file: { |
| 173 | + borderRadius: theme.shape.borderRadius, |
| 174 | + border: `1px solid ${theme.palette.divider}`, |
| 175 | + padding: theme.spacing(2), |
| 176 | + background: theme.palette.background.paper, |
| 177 | + }, |
| 178 | +})) |
0 commit comments