@@ -23,6 +23,7 @@ const FileUpload = <T extends RequiredFormProps>({
2323 acceptableTypes,
2424 fileId,
2525 fileName,
26+ fileUrlKey,
2627 formik,
2728 isEdit,
2829} : {
@@ -32,6 +33,7 @@ const FileUpload = <T extends RequiredFormProps>({
3233 acceptableTypes : string ,
3334 fileId : string ,
3435 fileName : string ,
36+ fileUrlKey : string ,
3537 formik : FormikProps < T > ,
3638 isEdit ?: boolean ,
3739} ) => {
@@ -46,6 +48,16 @@ const FileUpload = <T extends RequiredFormProps>({
4648 // reference used for activating file input when button is clicked
4749 const hiddenFileInput = useRef < HTMLInputElement > ( null ) ;
4850
51+ const existingFileUrl = fileUrlKey ? formik . values [ fileUrlKey ] as string : undefined ;
52+
53+ const displayName = file
54+ ? file . name
55+ : ( formik . values [ fileName ] as string ) ;
56+
57+ const displayUrl = file
58+ ? URL . createObjectURL ( file )
59+ : existingFileUrl ;
60+
4961 // Trigger formik validation
5062 // Setting formik fields in a promise callback does not trigger formik
5163 // validation (or at the very least, does not trigger it with the new
@@ -62,6 +74,9 @@ const FileUpload = <T extends RequiredFormProps>({
6274 setLoaded ( 0 ) ;
6375 formik . setFieldValue ( fileId , "" ) ;
6476 formik . setFieldValue ( fileName , "" ) ;
77+ if ( fileUrlKey ) {
78+ formik . setFieldValue ( fileUrlKey , "" ) ;
79+ }
6580 } ;
6681
6782 // upload file to backend
@@ -120,11 +135,11 @@ const FileUpload = <T extends RequiredFormProps>({
120135 < div className = "content-container" >
121136 { /* If user already uploaded a file, its name and a delete button is rendered */ }
122137 { /* else render button for upload */ }
123- { ! ! formik . values [ fileId ] && file ? (
138+ { ( file || existingFileUrl ) ? (
124139 < div className = "upload-file-info" >
125140 < p className = { isEdit ? "edit" : "" } >
126- < a href = { URL . createObjectURL ( file ) } target = "_blank" rel = "noreferrer" >
127- { formik . values [ fileName ] as string }
141+ < a href = { displayUrl } target = "_blank" rel = "noreferrer" >
142+ { displayName }
128143 </ a >
129144 </ p >
130145 < div className = "button-container" >
0 commit comments