|
1 | 1 | <script lang="ts"> |
2 | 2 | import { chain, fromFactories } from "@sjsf/form/lib/resolver"; |
3 | 3 | import { getSimpleSchemaType } from "@sjsf/form/core"; |
4 | | - import { createForm, BasicForm, type Schema, type Config } from "@sjsf/form"; |
| 4 | + import { |
| 5 | + createForm, |
| 6 | + BasicForm, |
| 7 | + type Schema, |
| 8 | + type Config, |
| 9 | + type ActionField, |
| 10 | + } from "@sjsf/form"; |
5 | 11 | import OptionalObjectTemplate from "@sjsf/form/templates/extra/optional-object.svelte"; |
6 | 12 | import OptionalArrayTemplate from "@sjsf/form/templates/extra/optional-array.svelte"; |
| 13 | + import OptionalMultiFieldTemplate from "@sjsf/form/templates/extra/optional-multi-field.svelte"; |
7 | 14 | import { clearEdit } from "@sjsf/form/fields/actions/clear-edit.svelte"; |
8 | 15 |
|
9 | 16 | import * as defaults from "$lib/form-defaults"; |
|
158 | 165 | const theme = chain( |
159 | 166 | fromFactories({ |
160 | 167 | objectTemplate: (cfg: Config) => |
161 | | - !cfg.required ? OptionalObjectTemplate : undefined, |
| 168 | + !cfg.required && cfg.schema.properties |
| 169 | + ? OptionalObjectTemplate |
| 170 | + : undefined, |
162 | 171 | arrayTemplate: (cfg: Config) => |
163 | 172 | !cfg.required ? OptionalArrayTemplate : undefined, |
| 173 | + multiFieldTemplate: (cfg: Config) => |
| 174 | + !cfg.required ? OptionalMultiFieldTemplate : undefined, |
164 | 175 | }), |
165 | 176 | defaults.theme |
166 | 177 | ); |
167 | 178 |
|
168 | 179 | const extraUiOptions = fromFactories({ |
169 | | - action: (cfg: Config) => { |
170 | | - if (!cfg.required) { |
171 | | - const type = getSimpleSchemaType(cfg.schema); |
172 | | - if (type === "array" || type === "object") { |
173 | | - return clearEdit; |
| 180 | + actions: ({ required, schema }: Config) => { |
| 181 | + if (!required) { |
| 182 | + let key: ActionField | undefined; |
| 183 | + if (schema.oneOf) { |
| 184 | + key = "oneOfField"; |
| 185 | + } else if (schema.anyOf) { |
| 186 | + key = "anyOfField"; |
| 187 | + } else { |
| 188 | + const type = getSimpleSchemaType(schema); |
| 189 | + if (type === "array") { |
| 190 | + key = "arrayField"; |
| 191 | + } else if (type === "object" && schema.properties) { |
| 192 | + key = "objectField"; |
| 193 | + } |
| 194 | + } |
| 195 | + if (key) { |
| 196 | + return { [key]: clearEdit }; |
174 | 197 | } |
175 | 198 | } |
176 | 199 | return undefined; |
|
0 commit comments