@@ -3,37 +3,34 @@ import React, { ChangeEvent, DragEvent, useEffect, useRef, useState } from "reac
33import "./ImageUpload.scss" ;
44import { Icon } from "../Icon" ;
55import clsx from "clsx" ;
6- import { ImageUploadProps } from "./ImageUpload.types" ;
6+ import { ImageType , ImageUploadProps } from "./ImageUpload.types" ;
77
88/**
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.
1110 *
1211 * By default, this component can be used as an uncontrolled input and all uploaded files
1312 * can be retrieved on form submission. For further control however, you can pass an optional function
1413 * to the `onImageSelected` prop.
1514 *
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:
1916 * - image/gif
2017 * - image/jpeg
2118 * - image/png
22- * - image/svg+xml
2319 * - image/webp
2420 * @param id - String HTML identifier for the input field.
2521 * @param maxSize - The max allowed size for file uploads (in Megabytes).
2622 * @param onImageSelected - Optional function for controlled handling of uploads. Triggered on every change to uploader.
2723 * @param hasUploaded - Boolean indicator to trigger image upload cleanup once the upload process has finalized on the frontend.
2824 */
2925export const ImageUpload = ( {
30- accept = "image/ png, image/ jpeg" ,
26+ accept = [ ImageType . png , ImageType . jpeg ] ,
3127 id,
3228 maxSize = 2 ,
3329 onImageSelected,
3430 hasUploaded = false
3531} : ImageUploadProps ) => {
3632 const inputRef = useRef < HTMLInputElement > ( null ) ;
33+ const acceptToStr = accept . join ( ", " )
3734 const maxSizeInBytes = maxSize * 1024 * 1024 ;
3835 const [ dragActive , setDragActive ] = useState ( false ) ;
3936 const [ uploadedImages , setUploadedImages ] = useState < File [ ] > ( [ ] ) ;
@@ -71,9 +68,10 @@ export const ImageUpload = ({
7168 * @param acceptedPreviews - Array of previously generated preview urls for each accepted file.
7269 */
7370 const isValidFile = ( file : File | null , acceptedFiles : File [ ] , declinedFiles : File [ ] , acceptedPreviews : string [ ] ) => {
71+ const acceptedTypes = new Set ( accept ) ;
7472 if ( file == null ) return false ;
7573
76- if ( ! accept . includes ( file . type ) || file . size > maxSizeInBytes ) {
74+ if ( ! acceptedTypes . has ( file . type as ImageType ) || file . size > maxSizeInBytes ) {
7775 declinedFiles . push ( file ) ;
7876 return false ;
7977 } else {
@@ -243,7 +241,7 @@ export const ImageUpload = ({
243241 className = 'c4imgupload--input'
244242 multiple = { false }
245243 type = "file"
246- accept = { accept }
244+ accept = { acceptToStr }
247245 onClick = { ( ) => setError ( undefined ) }
248246 onChange = { onImageChanged }
249247 />
0 commit comments