|
71 | 71 | )
|
72 | 72 | );
|
73 | 73 |
|
74 |
| - let readableSelectedOption = $state(0); |
75 |
| - let writableSelectedOption = $derived( |
| 74 | + let previousSelectedOption = $state.raw<number>(); |
| 75 | + let nextSelectedOption = $derived( |
76 | 76 | getClosestMatchingOption(
|
77 | 77 | ctx,
|
78 | 78 | value,
|
79 | 79 | retrievedOptions,
|
80 |
| - readableSelectedOption, |
| 80 | + previousSelectedOption ?? 0, |
81 | 81 | getDiscriminatorFieldFromSchema(config.schema)
|
82 | 82 | )
|
83 | 83 | );
|
84 | 84 | $effect(() => {
|
85 |
| - const nextSelected = writableSelectedOption; |
86 |
| - if (readableSelectedOption === nextSelected) { |
| 85 | + const nextSelected = nextSelectedOption; |
| 86 | + if (previousSelectedOption === nextSelected) { |
87 | 87 | return;
|
88 | 88 | }
|
89 | 89 | value = untrack(() => {
|
90 | 90 | const nextSchema = retrievedOptions[nextSelected];
|
91 | 91 | if (nextSchema === undefined) {
|
92 | 92 | return undefined;
|
93 | 93 | }
|
94 |
| - const oldSchema = retrievedOptions[readableSelectedOption]; |
| 94 | + const oldSchema = |
| 95 | + previousSelectedOption !== undefined |
| 96 | + ? retrievedOptions[previousSelectedOption] |
| 97 | + : undefined; |
95 | 98 | return getDefaultFieldState(
|
96 | 99 | ctx,
|
97 | 100 | nextSchema,
|
|
100 | 103 | : value
|
101 | 104 | );
|
102 | 105 | });
|
103 |
| - readableSelectedOption = nextSelected; |
| 106 | + previousSelectedOption = nextSelected; |
104 | 107 | });
|
105 | 108 |
|
106 | 109 | const optionsUiSchemas = $derived.by(() => {
|
|
185 | 188 | const errors = $derived(getFieldErrors(ctx, config.path));
|
186 | 189 |
|
187 | 190 | const combinationFieldConfig: Config | null = $derived.by(() => {
|
188 |
| - const selected = readableSelectedOption; |
| 191 | + const selected = previousSelectedOption ?? nextSelectedOption; |
189 | 192 | if (selected < 0) {
|
190 | 193 | return null;
|
191 | 194 | }
|
|
236 | 239 | uiOption={(opt) => retrieveUiOption(ctx, widgetConfig, opt)}
|
237 | 240 | options={enumOptions}
|
238 | 241 | bind:value={
|
239 |
| - () => readableSelectedOption, (v) => (writableSelectedOption = v) |
| 242 | + () => previousSelectedOption ?? nextSelectedOption, |
| 243 | + (v) => (nextSelectedOption = v) |
240 | 244 | }
|
241 | 245 | />
|
242 | 246 | {/snippet}
|
|
0 commit comments