Skip to content

Commit d8b4211

Browse files
committed
[form] Fix multi-field selector
1 parent e0a796e commit d8b4211

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

.changeset/blue-kiwis-do.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Fix multi-field selector
6+
7+
This is not a proper fix (array of multi-fields is still broken) but it's a good place to start.

packages/form/src/form/fields/multi-field.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script lang="ts">
22
import { proxy } from "@/lib/svelte.svelte";
3+
import { deepEqual } from '@/lib/deep-equal.js'
34
import {
45
getDiscriminatorFieldFromSchema,
56
mergeSchemas,
67
type EnumOption,
8+
type SchemaValue,
79
} from '@/core/index.js';
810
911
import type { Config } from '../config.js';
@@ -43,20 +45,24 @@
4345
)
4446
);
4547
46-
const selectedOption = proxy((isRegOnly) => {
48+
let lastValue: SchemaValue | undefined
49+
const selectedOption = proxy((isRegOnly, currentSelected: number | undefined) => {
4750
if (isRegOnly) {
4851
config.schema;
4952
value;
5053
retrievedOptions;
5154
return -1;
5255
}
53-
const discriminator = getDiscriminatorFieldFromSchema(config.schema);
56+
if (currentSelected !== undefined && deepEqual(lastValue, value)) {
57+
return currentSelected
58+
}
59+
lastValue = $state.snapshot(value)
5460
return getClosestMatchingOption(
5561
ctx,
5662
value,
5763
retrievedOptions,
5864
0,
59-
discriminator
65+
getDiscriminatorFieldFromSchema(config.schema),
6066
);
6167
}, (newSelected, oldSelected) => {
6268
if (oldSelected === undefined) {

packages/form/src/lib/svelte.svelte.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ export interface SyncInput<V> {
44
/**
55
* @param isDependencyRegistrationOnlyCall - when `true`, indicates that function is called only for dependency registration and result will be ignored
66
*/
7-
(isDependencyRegistrationOnlyCall: false): V;
8-
(isDependencyRegistrationOnlyCall: true): void;
7+
(isDependencyRegistrationOnlyCall: false, currentValue: V | undefined): V;
8+
(isDependencyRegistrationOnlyCall: true, currentValue: V | undefined): void;
99
}
1010

1111
export function proxy<V>(input: SyncInput<V>, onChange?: (value: V, prev: V) => void) {
@@ -15,10 +15,10 @@ export function proxy<V>(input: SyncInput<V>, onChange?: (value: V, prev: V) =>
1515
const proxyVal = proxyValue;
1616
if (ignoreInputUpdate) {
1717
ignoreInputUpdate = false;
18-
input(true);
18+
input(true, proxyVal);
1919
return proxyVal as V;
2020
}
21-
return input(false);
21+
return input(false, proxyVal);
2222
});
2323
return {
2424
get value() {

0 commit comments

Comments
 (0)