@@ -28,6 +28,11 @@ type Nullable<
2828 Path extends FormPaths < T > | FormPathArrays < T > | FormPathLeaves < T >
2929> = null extends FormPathType < T , Path > ? null : never ;
3030
31+ type Optional <
32+ T extends Record < string , unknown > ,
33+ Path extends FormPaths < T > | FormPathArrays < T > | FormPathLeaves < T >
34+ > = [ undefined ] extends [ FormPathType < T , Path > ] ? undefined : never ;
35+
3136type DefaultOptions = {
3237 trueStringValue : string ;
3338 dateFormat :
@@ -123,8 +128,8 @@ export function fileFieldProxy<
123128> (
124129 form : SuperForm < T > ,
125130 path : Path ,
126- options ?: ProxyOptions
127- ) : FormFieldProxy < FileList | File | Nullable < T , Path > , Path > {
131+ options ?: ProxyOptions & { empty ?: 'null' | 'undefined' }
132+ ) : FormFieldProxy < FileList | File | Nullable < T , Path > | Optional < T , Path > , Path > {
128133 const fileField = fileProxy ( form , path , options ) ;
129134 const formField = formFieldProxy ( form , path , options ) ;
130135
@@ -134,13 +139,22 @@ export function fileFieldProxy<
134139export function fileProxy < T extends Record < string , unknown > , Path extends FormPathLeaves < T , File > > (
135140 form : Writable < T > | SuperForm < T > ,
136141 path : Path ,
137- options ?: ProxyOptions
142+ options ?: ProxyOptions & { empty ?: 'null' | 'undefined' }
138143) {
139144 const formFile = fieldProxy ( form , path , options ) as FieldProxy < File > ;
140145 const fileProxy = writable < FileList > ( browser ? new DataTransfer ( ) . files : ( { } as FileList ) ) ;
141146
147+ let initialized = false ;
148+ let initialValue : File | null | undefined ;
149+
142150 formFile . subscribe ( ( file ) => {
143151 if ( ! browser ) return ;
152+
153+ if ( ! initialized ) {
154+ initialValue = options ?. empty ? ( options . empty === 'undefined' ? undefined : null ) : file ;
155+ initialized = true ;
156+ }
157+
144158 const dt = new DataTransfer ( ) ;
145159 if ( file ) dt . items . add ( file ) ;
146160 fileProxy . set ( dt . files ) ;
@@ -150,8 +164,9 @@ export function fileProxy<T extends Record<string, unknown>, Path extends FormPa
150164 subscribe ( run : ( value : FileList ) => void ) {
151165 return fileProxy . subscribe ( run ) ;
152166 } ,
153- set ( file : FileList | File | Nullable < T , Path > ) {
167+ set ( file : FileList | File | Nullable < T , Path > | Optional < T , Path > ) {
154168 if ( ! browser ) return ;
169+
155170 if ( ! file || file instanceof File ) {
156171 const dt = new DataTransfer ( ) ;
157172 if ( file ) dt . items . add ( file ) ;
@@ -160,7 +175,9 @@ export function fileProxy<T extends Record<string, unknown>, Path extends FormPa
160175 }
161176
162177 fileProxy . set ( file ) ;
178+
163179 if ( file . length > 0 ) formFile . set ( file . item ( 0 ) as File ) ;
180+ else formFile . set ( initialValue as File ) ;
164181 } ,
165182 update ( ) {
166183 throw new SuperFormError ( 'You cannot update a fileProxy, only set it.' ) ;
@@ -198,7 +215,7 @@ export function filesProxy<
198215 subscribe ( run : ( value : FileList ) => void ) {
199216 return filesProxy . subscribe ( run ) ;
200217 } ,
201- set ( files : FileList | File [ ] | Nullable < T , Path > ) {
218+ set ( files : FileList | File [ ] | Nullable < T , Path > | Optional < T , Path > ) {
202219 if ( ! browser ) return ;
203220 if ( ! ( files instanceof FileList ) ) {
204221 const dt = new DataTransfer ( ) ;
@@ -217,7 +234,7 @@ export function filesProxy<
217234 }
218235 formFiles . set ( output ) ;
219236 } ,
220- update ( updater : Updater < File [ ] | Nullable < T , Path > > ) {
237+ update ( updater : Updater < File [ ] | Nullable < T , Path > | Optional < T , Path > > ) {
221238 filesStore . set ( updater ( get ( formFiles ) ) ) ;
222239 }
223240 } ;
0 commit comments