@@ -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,12 @@ 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+ ) ;
4250
4351 return (
4452 < div className = { cn ( "box-border h-fit w-50" , className ) } >
@@ -71,6 +79,8 @@ function FileUploader({
7179
7280 if ( item ) {
7381 const file = await item . getFile ( ) ;
82+ const validated = validationFunction ?.( file ) ?? true ;
83+ if ( ! validated ) return ;
7484 setFileSelected ( file ) ;
7585 callback ( file ) ;
7686 }
@@ -82,6 +92,8 @@ function FileUploader({
8292 onSelect = { ( e ) => {
8393 if ( e ) {
8494 const file = e [ 0 ] ;
95+ const validated = validationFunction ?.( file ) ?? true ;
96+ if ( ! validated ) return ;
8597 setFileSelected ( file ) ;
8698 callback ( file ) ;
8799 }
@@ -109,7 +121,7 @@ function FileUploader({
109121 </ FileTrigger >
110122 </ DropZone >
111123 { msg && (
112- < div className = "mt-4 flex items-start " >
124+ < div className = "mt-4 flex items-center " >
113125 { variant === "success" && (
114126 < SuccessIcon className = "fill-klerosUIComponentsSuccess mr-2 max-w-4 min-w-4" />
115127 ) }
0 commit comments