Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/lib/client/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,26 @@ export function filesFieldProxy<
export function filesProxy<
T extends Record<string, unknown>,
Path extends FormPathArrays<T, File[]>
>(form: Writable<T> | SuperForm<T>, path: Path, options?: ProxyOptions) {
>(
form: Writable<T> | SuperForm<T>,
path: Path,
options?: ProxyOptions & { empty?: 'null' | 'undefined' }
) {
const formFiles = fieldProxy(form, path as any, options) as FieldProxy<
File[] | Nullable<T, Path> | Optional<T, Path>
>;
const filesProxy = writable<FileList>(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)) {
Expand Down Expand Up @@ -244,13 +256,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<File[] | Nullable<T, Path> | Optional<T, Path>>) {
Expand Down