@@ -207,14 +207,26 @@ export function filesFieldProxy<
207207export function filesProxy <
208208 T extends Record < string , unknown > ,
209209 Path extends FormPathArrays < T , File [ ] >
210- > ( form : Writable < T > | SuperForm < T > , path : Path , options ?: ProxyOptions ) {
210+ > (
211+ form : Writable < T > | SuperForm < T > ,
212+ path : Path ,
213+ options ?: ProxyOptions & { empty ?: 'null' | 'undefined' }
214+ ) {
211215 const formFiles = fieldProxy ( form , path as any , options ) as FieldProxy <
212216 File [ ] | Nullable < T , Path > | Optional < T , Path >
213217 > ;
214218 const filesProxy = writable < FileList > ( browser ? new DataTransfer ( ) . files : ( { } as FileList ) ) ;
219+ let initialized = false ;
220+ let initialValue : File [ ] | null | undefined ;
215221
216222 formFiles . subscribe ( ( files ) => {
217223 if ( ! browser ) return ;
224+
225+ if ( ! initialized ) {
226+ initialValue = options ?. empty ? ( options . empty === 'undefined' ? undefined : null ) : files ;
227+ initialized = true ;
228+ }
229+
218230 const dt = new DataTransfer ( ) ;
219231
220232 if ( Array . isArray ( files ) ) {
@@ -244,13 +256,17 @@ export function filesProxy<
244256 filesProxy . set ( dt . files ) ;
245257 formFiles . set ( files ) ;
246258 } else {
247- const output : File [ ] = [ ] ;
248- for ( let i = 0 ; i < files . length ; i ++ ) {
249- const file = files . item ( i ) ;
250- if ( file ) output . push ( file ) ;
259+ if ( files . length > 0 ) {
260+ const output : File [ ] = [ ] ;
261+ for ( let i = 0 ; i < files . length ; i ++ ) {
262+ const file = files . item ( i ) ;
263+ if ( file ) output . push ( file ) ;
264+ }
265+ filesProxy . set ( files ) ;
266+ formFiles . set ( output ) ;
267+ } else {
268+ formFiles . set ( initialValue as File [ ] ) ;
251269 }
252- filesProxy . set ( files ) ;
253- formFiles . set ( output ) ;
254270 }
255271 } ,
256272 update ( updater : Updater < File [ ] | Nullable < T , Path > | Optional < T , Path > > ) {
0 commit comments