Skip to content

Commit 1b8f1a6

Browse files
authored
Fix file inputs (#248)
* [fields] Fix file input handling by snapshotting files before async validation * [shadcn4] Fix unwanted file widget rerenders
1 parent b9831b2 commit 1b8f1a6

File tree

7 files changed

+21
-9
lines changed

7 files changed

+21
-9
lines changed

.changeset/bumpy-showers-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/shadcn4-theme": patch
3+
---
4+
5+
Fix unwanted file widget rerenders

.changeset/open-hounds-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Fix file input handling by snapshotting files before async validation

packages/form/src/fields/extra/file.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@
6262
if (files === undefined || files.length === 0) {
6363
return undefined;
6464
}
65+
const file = files[0]!
6566
if (!(await validateFileList(signal, ctx, config, files))) {
6667
throw new FileListValidationError();
6768
}
68-
return fileToDataURL(signal, files[0]!);
69+
return fileToDataURL(signal, file);
6970
},
7071
});
7172

packages/form/src/fields/extra/files.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@
6969
if (files === undefined) {
7070
return undefined;
7171
}
72+
const array = Array.from(files);
7273
if (!(await validateFileList(signal, ctx, config, files))) {
7374
throw new FileListValidationError();
7475
}
75-
return Promise.all(
76-
Array.from(files).map((f) => fileToDataURL(signal, f))
77-
);
76+
return Promise.all(array.map((f) => fileToDataURL(signal, f)));
7877
},
7978
});
8079

packages/form/src/fields/extra/native-file.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@
5555
if (files === undefined || files.length === 0) {
5656
return undefined;
5757
}
58+
const file = files[0]!;
5859
if (!(await validateFileList(signal, ctx, config, files))) {
5960
throw new FileListValidationError();
6061
}
61-
return files[0]!;
62+
return file;
6263
},
6364
onSuccess(file: File | undefined) {
6465
value = file;

packages/form/src/fields/extra/native-files.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@
5555
if (files === undefined) {
5656
return undefined;
5757
}
58+
const array = Array.from(files)
5859
if (!(await validateFileList(signal, ctx, config, files))) {
5960
throw new FileListValidationError();
6061
}
61-
return Array.from(files);
62+
return array;
6263
},
6364
onSuccess(file: File[] | undefined) {
6465
value = file;

packages/shadcn4-theme/src/lib/theme/extra-widgets/file.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
<Input
2424
{...inputAttributes(ctx, config, 'file', handlers, {
25-
multiple,
26-
'data-loading': loading,
27-
'data-processing': processing
25+
multiple
2826
})}
2927
bind:files={value}
3028
type="file"
29+
data-loading={loading}
30+
data-processing={processing}
3131
/>

0 commit comments

Comments
 (0)