-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileUploader.tsx
190 lines (186 loc) · 5.22 KB
/
FileUploader.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {
Box,
Chip,
Grid2 as Grid,
LinearProgress,
Stack,
Typography,
alpha
} from "@mui/material";
import FileUploadIcon from "@mui/icons-material/FileUpload";
import { FileUploaderProps } from "./FileUploader.types";
import React from "react";
import UploaderHeader from "../Uploader/UploaderHeader";
import useUploader from "../Uploader/useUploader";
export default function FileUploader({
acceptedFiles,
error,
disabled = false,
dropzoneText = "Drag & Drop a file here or browse",
filesLimit = 1,
isValidating = false,
maxFileSize = Infinity,
multiple = false,
onAdd,
onDelete,
required = false,
selectedFiles = [],
title = "Upload a File",
titleVariant,
subText
}: FileUploaderProps) {
// useUploader is a custom hook that handles the logic for uploading files
const { getRootProps, getInputProps, handleDelete, rejectionMessage } =
useUploader({
acceptedFiles,
disabled,
filesLimit,
maxFileSize,
multiple,
onAdd,
onDelete,
selectedFiles
});
// are we rendering an error state?
const isError = rejectionMessage || error;
// render
return (
<Box data-testid="dropzone-base">
<UploaderHeader
title={title}
titleVariant={titleVariant}
subText={subText}
required={required}
showDelete={!isValidating && !multiple && selectedFiles.length === 1}
onDelete={() => handleDelete(selectedFiles[0])}
disabled={disabled}
/>
<Box
{...getRootProps()}
data-testid="dropzone-root"
sx={theme => ({
"&:hover": {
"& .dropzoneText": {
color: isError
? theme.palette.error.main
: theme.palette.primary.main
},
background: isError
? "inherit"
: alpha(theme.palette.primary.main, 0.04),
borderColor: isError
? theme.palette.error.main
: theme.palette.primary.main,
color: theme.palette.primary.main
},
".dropzoneSingleFile, .dropzoneSingleFile > *": {
color: disabled
? theme.palette.text.disabled
: theme.palette.primary.main
},
".dropzoneSingleFile, .dropzoneText": {
alignItems: "center",
flexDirection: "row",
gap: 1,
height: "100%",
justifyContent: "center"
},
".dropzoneText, .dropzoneText > *": {
color: isError
? theme.palette.error.main
: disabled
? theme.palette.text.disabled
: theme.palette.mode === "dark"
? theme.palette.text.primary
: theme.palette.text.secondary
},
background: theme.palette.background.default,
borderColor: error ? theme.palette.error.main : theme.palette.divider,
borderStyle: "dashed",
borderWidth: 1,
boxSizing: "border-box",
cursor: "pointer",
display: "flex",
fontSize: "16px",
height: "56px",
justifyContent: "center",
p: 2,
pointerEvents:
disabled ||
isValidating ||
(!multiple && selectedFiles.length === 1)
? "none"
: "auto"
})}
>
<input {...getInputProps()} />
{isValidating ? (
<Stack className="dropzoneText">
<Typography
sx={{
fontSize: "14px"
}}
>
{multiple ? `Validating Selection(s)` : `Validating Selection`}
</Typography>
<Box
sx={{
width: 200
}}
>
<LinearProgress />
</Box>
</Stack>
) : (
<>
{!multiple && selectedFiles.length === 1 ? (
<Stack className="dropzoneSingleFile">
<Typography
sx={{
fontSize: "15px"
}}
>
{selectedFiles[0].file.name}
</Typography>
</Stack>
) : (
<Stack className="dropzoneText">
<FileUploadIcon />
<Typography
sx={{
fontSize: "15px"
}}
>
{rejectionMessage ?? dropzoneText}
</Typography>
</Stack>
)}
</>
)}
</Box>
{multiple && selectedFiles.length > 0 ? (
<Grid
spacing={1}
direction="row"
container={true}
sx={{
mt: 0.5
}}
>
{selectedFiles.map((thisFile, i) => {
return (
<Grid key={`${thisFile.file?.name ?? "file"}-${i}`}>
<Chip
disabled={disabled || isValidating}
variant="outlined"
label={thisFile.file.name}
onDelete={disabled ? undefined : () => handleDelete(thisFile)}
/>
</Grid>
);
})}
</Grid>
) : null}
</Box>
);
}