From f87bce11b03c94bef8145b20c5d74a7c832eebc9 Mon Sep 17 00:00:00 2001 From: Nathan Cahill Date: Fri, 5 Sep 2025 15:47:40 -0600 Subject: [PATCH 1/2] fix filesStore initialValue to match fileStore --- src/lib/client/proxies.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/lib/client/proxies.ts b/src/lib/client/proxies.ts index 96c0b101..5b79010e 100644 --- a/src/lib/client/proxies.ts +++ b/src/lib/client/proxies.ts @@ -207,14 +207,22 @@ export function filesFieldProxy< export function filesProxy< T extends Record, Path extends FormPathArrays ->(form: Writable | SuperForm, path: Path, options?: ProxyOptions) { +>(form: Writable | SuperForm, path: Path, options?: ProxyOptions & { empty?: 'null' | 'undefined' }) { const formFiles = fieldProxy(form, path as any, options) as FieldProxy< File[] | Nullable | Optional >; const filesProxy = writable(browser ? new DataTransfer().files : ({} as FileList)); + let initialized = false; + let initialValue: File[] | null | undefined; formFiles.subscribe((files) => { if (!browser) return; + + if (!initialized) { + initialValue = options?.empty ? (options.empty === 'undefined' ? undefined : null) : files; + initialized = true; + } + const dt = new DataTransfer(); if (Array.isArray(files)) { @@ -244,13 +252,17 @@ export function filesProxy< filesProxy.set(dt.files); formFiles.set(files); } else { - const output: File[] = []; - for (let i = 0; i < files.length; i++) { - const file = files.item(i); - if (file) output.push(file); + if (files.length > 0) { + const output: File[] = []; + for (let i = 0; i < files.length; i++) { + const file = files.item(i); + if (file) output.push(file); + } + filesProxy.set(files); + formFiles.set(output); + } else { + formFiles.set(initialValue as File[]); } - filesProxy.set(files); - formFiles.set(output); } }, update(updater: Updater | Optional>) { From f70c49cc511e3f9913adf20be05f25b9f71638ce Mon Sep 17 00:00:00 2001 From: Nathan Cahill Date: Fri, 5 Sep 2025 16:18:03 -0600 Subject: [PATCH 2/2] prettier --- src/lib/client/proxies.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/client/proxies.ts b/src/lib/client/proxies.ts index 5b79010e..55a9eff4 100644 --- a/src/lib/client/proxies.ts +++ b/src/lib/client/proxies.ts @@ -207,7 +207,11 @@ export function filesFieldProxy< export function filesProxy< T extends Record, Path extends FormPathArrays ->(form: Writable | SuperForm, path: Path, options?: ProxyOptions & { empty?: 'null' | 'undefined' }) { +>( + form: Writable | SuperForm, + path: Path, + options?: ProxyOptions & { empty?: 'null' | 'undefined' } +) { const formFiles = fieldProxy(form, path as any, options) as FieldProxy< File[] | Nullable | Optional >;