Skip to content

Commit 35dd9a4

Browse files
committed
[fields] Make combination field SRR friendly
1 parent fd40b74 commit 35dd9a4

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

.changeset/wise-peaches-kiss.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+
Make `combination` field SRR friendly

packages/form/src/fields/combination.svelte

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,30 @@
7171
)
7272
);
7373
74-
let readableSelectedOption = $state(0);
75-
let writableSelectedOption = $derived(
74+
let previousSelectedOption = $state.raw<number>();
75+
let nextSelectedOption = $derived(
7676
getClosestMatchingOption(
7777
ctx,
7878
value,
7979
retrievedOptions,
80-
readableSelectedOption,
80+
previousSelectedOption ?? 0,
8181
getDiscriminatorFieldFromSchema(config.schema)
8282
)
8383
);
8484
$effect(() => {
85-
const nextSelected = writableSelectedOption;
86-
if (readableSelectedOption === nextSelected) {
85+
const nextSelected = nextSelectedOption;
86+
if (previousSelectedOption === nextSelected) {
8787
return;
8888
}
8989
value = untrack(() => {
9090
const nextSchema = retrievedOptions[nextSelected];
9191
if (nextSchema === undefined) {
9292
return undefined;
9393
}
94-
const oldSchema = retrievedOptions[readableSelectedOption];
94+
const oldSchema =
95+
previousSelectedOption !== undefined
96+
? retrievedOptions[previousSelectedOption]
97+
: undefined;
9598
return getDefaultFieldState(
9699
ctx,
97100
nextSchema,
@@ -100,7 +103,7 @@
100103
: value
101104
);
102105
});
103-
readableSelectedOption = nextSelected;
106+
previousSelectedOption = nextSelected;
104107
});
105108
106109
const optionsUiSchemas = $derived.by(() => {
@@ -185,7 +188,7 @@
185188
const errors = $derived(getFieldErrors(ctx, config.path));
186189
187190
const combinationFieldConfig: Config | null = $derived.by(() => {
188-
const selected = readableSelectedOption;
191+
const selected = previousSelectedOption ?? nextSelectedOption;
189192
if (selected < 0) {
190193
return null;
191194
}
@@ -236,7 +239,8 @@
236239
uiOption={(opt) => retrieveUiOption(ctx, widgetConfig, opt)}
237240
options={enumOptions}
238241
bind:value={
239-
() => readableSelectedOption, (v) => (writableSelectedOption = v)
242+
() => previousSelectedOption ?? nextSelectedOption,
243+
(v) => (nextSelectedOption = v)
240244
}
241245
/>
242246
{/snippet}

0 commit comments

Comments
 (0)