1- import React , { useState } from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22import UploadIcon from "../../assets/svgs/form/upload-icon.svg" ;
33import SuccessIcon from "../../assets/svgs/status-icons/success.svg" ;
44import ErrorIcon from "../../assets/svgs/status-icons/error.svg" ;
@@ -25,6 +25,10 @@ interface FileUploaderProps {
2525 /** Whether the drop target is disabled. If true, the drop target will not accept any drops. */
2626 isDisabled ?: boolean ;
2727 className ?: string ;
28+ /** Provide a custom validation function, returning false invalidates the file */
29+ validationFunction ?: ( file ?: File ) => boolean ;
30+ /** Provide File for controlled behaviour */
31+ selectedFile ?: File ;
2832}
2933
3034/** Allows to upload a file by either dropping it on the dropzone or
@@ -37,8 +41,16 @@ function FileUploader({
3741 acceptedFileTypes,
3842 fileTriggerProps,
3943 isDisabled = false ,
44+ validationFunction,
45+ selectedFile,
4046} : Readonly < FileUploaderProps > ) {
41- const [ fileSelected , setFileSelected ] = useState < File > ( ) ;
47+ const [ fileSelected , setFileSelected ] = useState < File | undefined > (
48+ selectedFile ,
49+ ) ;
50+
51+ useEffect ( ( ) => {
52+ setFileSelected ( selectedFile ) ;
53+ } , [ selectedFile ] ) ;
4254
4355 return (
4456 < div className = { cn ( "box-border h-fit w-50" , className ) } >
@@ -71,7 +83,10 @@ function FileUploader({
7183
7284 if ( item ) {
7385 const file = await item . getFile ( ) ;
74- setFileSelected ( file ) ;
86+ const validated = validationFunction ?.( file ) ?? true ;
87+ if ( ! validated ) return ;
88+ if ( selectedFile === undefined ) setFileSelected ( file ) ;
89+
7590 callback ( file ) ;
7691 }
7792 } }
@@ -82,7 +97,9 @@ function FileUploader({
8297 onSelect = { ( e ) => {
8398 if ( e ) {
8499 const file = e [ 0 ] ;
85- setFileSelected ( file ) ;
100+ const validated = validationFunction ?.( file ) ?? true ;
101+ if ( ! validated ) return ;
102+ if ( selectedFile === undefined ) setFileSelected ( file ) ;
86103 callback ( file ) ;
87104 }
88105 } }
@@ -109,7 +126,7 @@ function FileUploader({
109126 </ FileTrigger >
110127 </ DropZone >
111128 { msg && (
112- < div className = "mt-4 flex items-start " >
129+ < div className = "mt-4 flex items-center " >
113130 { variant === "success" && (
114131 < SuccessIcon className = "fill-klerosUIComponentsSuccess mr-2 max-w-4 min-w-4" />
115132 ) }
0 commit comments