Skip to content

Commit 73d7785

Browse files
committed
File proxy fix.
1 parent 7d5f09f commit 73d7785

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/lib/client/proxies.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3136
type 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<
134139
export 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
};

src/routes/(v2)/v2/files-proxy/+page.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
function clearFileDirectly() {
3232
// @ts-expect-error Not nullable
3333
$form.image = null;
34+
$form.image = undefined;
3435
}
3536
3637
function clearFileWithProxy() {
3738
// @ts-expect-error Not nullable
3839
file.set(null);
40+
file.set(undefined);
3941
}
4042
4143
/////////////////////////////////////////////////////////////////////////////
@@ -65,10 +67,14 @@
6567
}
6668
6769
function clearFilesDirectly() {
70+
// @ts-expect-error Not optional
71+
$form.images = undefined;
6872
$form.images = null;
6973
}
7074
7175
function clearFilesWithProxy() {
76+
// @ts-expect-error Not optional
77+
files.values.set(undefined);
7278
files.values.set(null);
7379
}
7480

src/routes/(v2)/v2/files-proxy/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export const schema = z.object({
99
image: z
1010
.instanceof(File, { message: 'Please upload a file.' })
1111
.refine((f) => f.size < 10000, 'Max 10Kb upload size.')
12+
.optional()
1213
});

0 commit comments

Comments
 (0)