|
6 | 6 | mergeSchemas, |
7 | 7 | type EnumOption, |
8 | 8 | type SchemaValue, |
9 | | - } from '@/core/index.js'; |
| 9 | + } from "@/core/index.js"; |
10 | 10 |
|
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"; |
13 | 13 | import { |
14 | 14 | getTemplate, |
15 | 15 | getWidget, |
|
22 | 22 | retrieveSchema, |
23 | 23 | sanitizeDataForNewSchema, |
24 | 24 | getFormContext, |
25 | | - makePseudoId |
| 25 | + makePseudoId, |
26 | 26 | } from "../context/index.js"; |
27 | 27 |
|
28 | 28 | import type { FieldProps } from "./model.js"; |
|
45 | 45 | ) |
46 | 46 | ); |
47 | 47 |
|
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 | + ); |
55 | 91 | } |
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 | + ); |
79 | 93 |
|
80 | 94 | const optionSchema = $derived.by(() => { |
81 | 95 | const selected = selectedOption.value; |
|
109 | 123 | const enumOptionLabel = $derived.by(() => { |
110 | 124 | const customTitle = config.uiOptions?.title ?? config.schema.title; |
111 | 125 | 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 | + ) |
113 | 132 | : (index: number) => ctx.translation("multi-schema-option-label", index); |
114 | | - }) |
| 133 | + }); |
115 | 134 | const enumOptions = $derived<EnumOption<number>[]>( |
116 | 135 | retrievedOptions.map((s, i) => ({ |
117 | 136 | 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), |
122 | 138 | value: i, |
123 | 139 | disabled: false, |
124 | 140 | })) |
125 | 141 | ); |
126 | 142 |
|
127 | 143 | 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 | + >; |
129 | 147 | return { |
130 | 148 | ...config, |
131 | 149 | schema: { type: "integer", default: 0 }, |
|
0 commit comments