Skip to content

Commit 86e9d56

Browse files
authored
fix filesStore initialValue to match fileStore (#637)
* fix filesStore initialValue to match fileStore * prettier
1 parent c5863e2 commit 86e9d56

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/lib/client/proxies.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,26 @@ export function filesFieldProxy<
207207
export 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

Comments
 (0)