Skip to content

Commit 0c8cdf5

Browse files
committed
[form] Format multi-field field code
1 parent fbd46f4 commit 0c8cdf5

File tree

1 file changed

+59
-41
lines changed

1 file changed

+59
-41
lines changed

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

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
mergeSchemas,
77
type EnumOption,
88
type SchemaValue,
9-
} from '@/core/index.js';
9+
} from "@/core/index.js";
1010
11-
import type { Config } from '../config.js';
12-
import type { UiSchema } from '../ui-schema.js';
11+
import type { Config } from "../config.js";
12+
import type { UiSchema } from "../ui-schema.js";
1313
import {
1414
getTemplate,
1515
getWidget,
@@ -22,7 +22,7 @@
2222
retrieveSchema,
2323
sanitizeDataForNewSchema,
2424
getFormContext,
25-
makePseudoId
25+
makePseudoId,
2626
} from "../context/index.js";
2727
2828
import type { FieldProps } from "./model.js";
@@ -45,37 +45,51 @@
4545
)
4646
);
4747
48-
let lastValue: SchemaValue | undefined
49-
const selectedOption = proxy((isRegOnly, currentSelected: number | undefined) => {
50-
if (isRegOnly) {
51-
config.schema;
52-
value;
53-
retrievedOptions;
54-
return -1;
48+
let lastValue: SchemaValue | undefined;
49+
const selectedOption = proxy(
50+
(isRegOnly, currentSelected: number | undefined) => {
51+
if (isRegOnly) {
52+
config.schema;
53+
value;
54+
retrievedOptions;
55+
return -1;
56+
}
57+
if (
58+
currentSelected !== undefined &&
59+
isSchemaValueDeepEqual(lastValue, value)
60+
) {
61+
return currentSelected;
62+
}
63+
lastValue = $state.snapshot(value);
64+
return getClosestMatchingOption(
65+
ctx,
66+
value,
67+
retrievedOptions,
68+
0,
69+
getDiscriminatorFieldFromSchema(config.schema)
70+
);
71+
},
72+
(newSelected, oldSelected) => {
73+
if (oldSelected === undefined) {
74+
return;
75+
}
76+
const newSchema =
77+
newSelected < 0 ? undefined : retrievedOptions[newSelected];
78+
if (newSchema === undefined) {
79+
value = undefined;
80+
return;
81+
}
82+
const oldSchema =
83+
oldSelected < 0 ? undefined : retrievedOptions[oldSelected];
84+
value = getDefaultFieldState(
85+
ctx,
86+
newSchema,
87+
oldSchema !== undefined
88+
? sanitizeDataForNewSchema(ctx, newSchema, oldSchema, value)
89+
: value
90+
);
5591
}
56-
if (currentSelected !== undefined && isSchemaValueDeepEqual(lastValue, value)) {
57-
return currentSelected
58-
}
59-
lastValue = $state.snapshot(value)
60-
return getClosestMatchingOption(
61-
ctx,
62-
value,
63-
retrievedOptions,
64-
0,
65-
getDiscriminatorFieldFromSchema(config.schema),
66-
);
67-
}, (newSelected, oldSelected) => {
68-
if (oldSelected === undefined) {
69-
return
70-
}
71-
const newSchema = newSelected < 0 ? undefined : retrievedOptions[newSelected];
72-
if (newSchema === undefined) {
73-
value = undefined
74-
return
75-
}
76-
const oldSchema = oldSelected < 0 ? undefined : retrievedOptions[oldSelected];
77-
value = getDefaultFieldState(ctx, newSchema, oldSchema !== undefined ? sanitizeDataForNewSchema(ctx, newSchema, oldSchema, value) : value)
78-
});
92+
);
7993
8094
const optionSchema = $derived.by(() => {
8195
const selected = selectedOption.value;
@@ -109,23 +123,27 @@
109123
const enumOptionLabel = $derived.by(() => {
110124
const customTitle = config.uiOptions?.title ?? config.schema.title;
111125
return customTitle !== undefined
112-
? (index: number) => ctx.translation("multi-schema-option-label-with-title", customTitle, index)
126+
? (index: number) =>
127+
ctx.translation(
128+
"multi-schema-option-label-with-title",
129+
customTitle,
130+
index
131+
)
113132
: (index: number) => ctx.translation("multi-schema-option-label", index);
114-
})
133+
});
115134
const enumOptions = $derived<EnumOption<number>[]>(
116135
retrievedOptions.map((s, i) => ({
117136
id: makePseudoId(ctx, config.idSchema.$id, i),
118-
label:
119-
optionsUiOptions[i]?.title ??
120-
s.title ??
121-
enumOptionLabel(i),
137+
label: optionsUiOptions[i]?.title ?? s.title ?? enumOptionLabel(i),
122138
value: i,
123139
disabled: false,
124140
}))
125141
);
126142
127143
const widgetConfig: Config = $derived.by(() => {
128-
const suffix = combinationKey.toLowerCase() as Lowercase<typeof combinationKey>;
144+
const suffix = combinationKey.toLowerCase() as Lowercase<
145+
typeof combinationKey
146+
>;
129147
return {
130148
...config,
131149
schema: { type: "integer", default: 0 },

0 commit comments

Comments
 (0)