@@ -3,37 +3,34 @@ import React, { ChangeEvent, DragEvent, useEffect, useRef, useState } from "reac
3
3
import "./ImageUpload.scss" ;
4
4
import { Icon } from "../Icon" ;
5
5
import clsx from "clsx" ;
6
- import { ImageUploadProps } from "./ImageUpload.types" ;
6
+ import { ImageType , ImageUploadProps } from "./ImageUpload.types" ;
7
7
8
8
/**
9
- * A custom image upload input component with support for drag & drop
10
- * as well as multi-file upload by providing the `multiSelect` prop.
9
+ * A custom image upload input component with support for drag & drop.
11
10
*
12
11
* By default, this component can be used as an uncontrolled input and all uploaded files
13
12
* can be retrieved on form submission. For further control however, you can pass an optional function
14
13
* to the `onImageSelected` prop.
15
14
*
16
- * @param accept - String value indicating all the accepted file types (separated by a comma). Options include:
17
- * - image/apng
18
- * - image/avif
15
+ * @param accept - List of image types as provided by the `ImageType` enum. Available options include:
19
16
* - image/gif
20
17
* - image/jpeg
21
18
* - image/png
22
- * - image/svg+xml
23
19
* - image/webp
24
20
* @param id - String HTML identifier for the input field.
25
21
* @param maxSize - The max allowed size for file uploads (in Megabytes).
26
22
* @param onImageSelected - Optional function for controlled handling of uploads. Triggered on every change to uploader.
27
23
* @param hasUploaded - Boolean indicator to trigger image upload cleanup once the upload process has finalized on the frontend.
28
24
*/
29
25
export const ImageUpload = ( {
30
- accept = "image/ png, image/ jpeg" ,
26
+ accept = [ ImageType . png , ImageType . jpeg ] ,
31
27
id,
32
28
maxSize = 2 ,
33
29
onImageSelected,
34
30
hasUploaded = false
35
31
} : ImageUploadProps ) => {
36
32
const inputRef = useRef < HTMLInputElement > ( null ) ;
33
+ const acceptToStr = accept . join ( ", " )
37
34
const maxSizeInBytes = maxSize * 1024 * 1024 ;
38
35
const [ dragActive , setDragActive ] = useState ( false ) ;
39
36
const [ uploadedImages , setUploadedImages ] = useState < File [ ] > ( [ ] ) ;
@@ -71,9 +68,10 @@ export const ImageUpload = ({
71
68
* @param acceptedPreviews - Array of previously generated preview urls for each accepted file.
72
69
*/
73
70
const isValidFile = ( file : File | null , acceptedFiles : File [ ] , declinedFiles : File [ ] , acceptedPreviews : string [ ] ) => {
71
+ const acceptedTypes = new Set ( accept ) ;
74
72
if ( file == null ) return false ;
75
73
76
- if ( ! accept . includes ( file . type ) || file . size > maxSizeInBytes ) {
74
+ if ( ! acceptedTypes . has ( file . type as ImageType ) || file . size > maxSizeInBytes ) {
77
75
declinedFiles . push ( file ) ;
78
76
return false ;
79
77
} else {
@@ -243,7 +241,7 @@ export const ImageUpload = ({
243
241
className = 'c4imgupload--input'
244
242
multiple = { false }
245
243
type = "file"
246
- accept = { accept }
244
+ accept = { acceptToStr }
247
245
onClick = { ( ) => setError ( undefined ) }
248
246
onChange = { onImageChanged }
249
247
/>
0 commit comments