Skip to content

Commit e904c7f

Browse files
committed
[fields] Fix value tracking in file fields
1 parent 80609ab commit e904c7f

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

.changeset/cute-apples-marry.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+
Refactor async logic in file fields

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</script>
88

99
<script lang="ts">
10+
import { untrack } from "svelte";
11+
1012
import { fileToDataURL } from "@/lib/file.js";
1113
import { abortPrevious, createAction } from "@/lib/action.svelte.js";
1214
import {
@@ -44,7 +46,7 @@
4446
async execute(
4547
signal,
4648
files: FileList | undefined
47-
): Promise<string | undefined> {
49+
) {
4850
return files === undefined || files.length === 0
4951
? undefined
5052
: fileToDataURL(signal, files[0]!);
@@ -74,8 +76,10 @@
7476
if (value === lastValueUpdate) {
7577
return;
7678
}
77-
toValue.abort();
78-
toFiles.run(value);
79+
untrack(() => {
80+
toValue.abort();
81+
toFiles.run(value);
82+
});
7983
});
8084
8185
const errors = $derived(getErrors(ctx, config.id));

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</script>
88

99
<script lang="ts">
10+
import { untrack } from "svelte";
11+
1012
import { fileToDataURL } from "@/lib/file.js";
1113
import { abortPrevious, createAction } from "@/lib/action.svelte.js";
1214
import {
@@ -41,10 +43,7 @@
4143
let lastValueUpdate: string[] | undefined;
4244
const toValue = createAction({
4345
combinator: abortPrevious,
44-
async execute(
45-
signal,
46-
files: FileList | undefined
47-
): Promise<string[] | undefined> {
46+
async execute(signal, files: FileList | undefined) {
4847
if (files === undefined) {
4948
return undefined;
5049
}
@@ -82,8 +81,10 @@
8281
) {
8382
return;
8483
}
85-
toValue.abort();
86-
toFiles.run(value);
84+
untrack(() => {
85+
toValue.abort();
86+
toFiles.run(value);
87+
});
8788
});
8889
8990
const errors = $derived(getErrors(ctx, config.id));

0 commit comments

Comments
 (0)