Skip to content

Commit fa9ef73

Browse files
committed
Merge branch 'main' of https://github.com/rouftom/react-mui-fileuploader into main
2 parents 5945ad9 + eaddd81 commit fa9ef73

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/FileUpload.jsx

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ function FileUpload(props) {
6464
placeholderImageDimension
6565
} = props
6666
const theme = useTheme()
67-
67+
6868
const [error, setError] = useState()
6969
const [animate, setAnimate] = useState()
7070
const [files, setFiles] = useState([])
71-
71+
7272
const oneMega = 1024 * 1024
7373
const filesCardRef = useRef()
74+
const inputRef = useRef()
7475

7576
let imageDimension = { width: 128, height: 128 }
7677
if (useMediaQuery(theme.breakpoints.up('xs')) && placeholderImageDimension?.xs) {
@@ -85,7 +86,7 @@ function FileUpload(props) {
8586
if (useMediaQuery(theme.breakpoints.up('lg')) && placeholderImageDimension?.lg) {
8687
imageDimension = placeholderImageDimension.lg
8788
}
88-
89+
8990
/**
9091
* @name renderPreview
9192
* @description
@@ -118,7 +119,7 @@ function FileUpload(props) {
118119
if (maxFileSize && maxFileSize > 0) {
119120
if (file.size > (1024 * 1024 * maxFileSize)) {
120121
let message = (
121-
errorSizeMessage ||
122+
errorSizeMessage ||
122123
`The size of files cannot exceed ${maxFileSize}Mb`
123124
)
124125
setError(message)
@@ -156,7 +157,7 @@ function FileUpload(props) {
156157
event?.dataTransfer?.clearData()
157158
}
158159
}
159-
160+
160161
/**
161162
* @name handleRemoveFile
162163
* @description
@@ -165,7 +166,9 @@ function FileUpload(props) {
165166
*/
166167
const handleRemoveFile = (index) => {
167168
setError(null)
168-
document.getElementById('input-files').value = ''
169+
if (inputRef.current) {
170+
inputRef.current.value = ''
171+
}
169172
if (typeof index !== 'number') {
170173
setFiles([])
171174
return onFilesChange([])
@@ -176,7 +179,7 @@ function FileUpload(props) {
176179
files?.splice(index, 1)
177180
setFiles([...files])
178181
}
179-
182+
180183
/**
181184
* @name handleDragEnter
182185
* @description
@@ -186,7 +189,7 @@ function FileUpload(props) {
186189
event.preventDefault()
187190
setAnimate(true)
188191
}, [])
189-
192+
190193
/**
191194
* @name handleDragOver
192195
* @description
@@ -197,7 +200,7 @@ function FileUpload(props) {
197200
event.preventDefault()
198201
setAnimate(true)
199202
}, [])
200-
203+
201204
/**
202205
* @name handleDrop
203206
* @description
@@ -209,16 +212,16 @@ function FileUpload(props) {
209212
let dt = event.dataTransfer
210213
if (dt.files) renderPreview(event, dt.files)
211214
}, [])
212-
215+
213216
/**
214217
* @name handleDragLeave
215218
* @description
216219
* @returns void
217220
*/
218-
const handleDragLeave = useCallback((event) => {
221+
const handleDragLeave = useCallback(() => {
219222
setAnimate(false)
220223
}, [])
221-
224+
222225
useEffect(() => {
223226
let dragDiv = filesCardRef.current
224227
if (dragDiv && !disabled) {
@@ -229,24 +232,24 @@ function FileUpload(props) {
229232
}
230233
// eslint-disable-next-line
231234
}, [filesCardRef.current])
232-
235+
233236
useEffect(() => {
234237
if (defaultFiles?.length > 0) {
235238
setFiles(defaultFiles)
236239
}
237240
// eslint-disable-next-line
238241
}, [defaultFiles])
239-
242+
240243
useEffect(() => {
241-
if (files && onFilesChange) {
244+
if (files && onFilesChange) {
242245
onFilesChange([...files])
243246
}
244247
// eslint-disable-next-line
245248
}, [files])
246-
247-
const background = animate ?
249+
250+
const background = animate ?
248251
theme.palette.secondary.light : theme.palette.primary.light
249-
252+
250253
return (
251254
<Paper
252255
sx={{ p: 1 }}
@@ -282,9 +285,9 @@ function FileUpload(props) {
282285
alignItems="center"
283286
justifyContent="center"
284287
>
285-
<Grid
286-
item
287-
xs={12} sm={3} md={4}
288+
<Grid
289+
item
290+
xs={12} sm={3} md={4}
288291
sx={{ textAlign: 'center' }}
289292
>
290293
<img
@@ -324,7 +327,7 @@ function FileUpload(props) {
324327
borderColor: theme.palette.grey["50"]
325328
}
326329
}}
327-
onClick={() => document.getElementById('input-files').click()}
330+
onClick={() => inputRef.current?.click()}
328331
>
329332
{buttonLabel}
330333
</Button>
@@ -333,7 +336,7 @@ function FileUpload(props) {
333336
<input
334337
type="file"
335338
accept={`*/*`}
336-
id="input-files"
339+
ref={inputRef}
337340
multiple={multiFile}
338341
onChange={renderPreview}
339342
style={{display: "none"}}
@@ -419,4 +422,4 @@ FileUpload.defaultProps = {
419422
placeholderImageDimension: []
420423
}
421424

422-
export default FileUpload
425+
export default FileUpload

0 commit comments

Comments
 (0)