|
1 | 1 | <script lang="ts"> |
| 2 | + import { untrack } from 'svelte'; |
| 3 | +
|
| 4 | + import { deepEqual } from '@/lib/deep-equal.js' |
2 | 5 | import { |
3 | 6 | getDefaultValueForType, |
4 | 7 | getSimpleSchemaType, |
5 | 8 | isAdditionalProperty, |
6 | 9 | isSchemaExpandable, |
7 | 10 | isSchemaObjectValue, |
8 | 11 | orderProperties, |
| 12 | + type Schema, |
| 13 | + type SchemaObjectValue, |
9 | 14 | } from '@/core/index.js'; |
10 | 15 |
|
11 | 16 | import type { UiSchema } from '../../ui-schema.js'; |
|
34 | 39 | config, |
35 | 40 | value = $bindable(), |
36 | 41 | }: FieldProps<"object"> = $props(); |
37 | | - $effect(() => { |
38 | | - if (value === undefined) { |
39 | | - value = {} |
40 | | - } |
41 | | - }) |
42 | | - |
43 | 42 | const newKeySeparator = $derived(config.uiOptions?.duplicateKeySuffixSeparator ?? "-") |
44 | 43 | const objCtx: ObjectContext = { |
45 | 44 | get newKeySeparator() { |
|
49 | 48 | setObjectContext(objCtx) |
50 | 49 | |
51 | 50 | // NOTE: This is required for computing a schema which will include all additional properties |
52 | | - // in the `properties` field with the `ADDITIONAL_PROPERTY_FLAG` flag. |
| 51 | + // in the `properties` field with the `ADDITIONAL_PROPERTY_FLAG` flag and |
| 52 | + // `dependencies` resolution. |
53 | 53 | const retrievedSchema = $derived(retrieveSchema(ctx, config.schema, value)) |
54 | 54 | const requiredProperties = $derived(new Set(retrievedSchema.required)); |
55 | | - const schemaProperties = $derived(retrievedSchema.properties); |
| 55 | +
|
| 56 | + let lastSchemaProperties: Schema['properties'] = undefined |
| 57 | + const schemaProperties = $derived.by(() => { |
| 58 | + if (!deepEqual(lastSchemaProperties, retrievedSchema.properties)) { |
| 59 | + lastSchemaProperties = $state.snapshot(retrievedSchema.properties) |
| 60 | + } |
| 61 | + return lastSchemaProperties |
| 62 | + }); |
| 63 | + // This code should populate `defaults` for properties from `dependencies` before new `fields` |
| 64 | + // will populate their `defaults`. |
| 65 | + $effect.pre(() => { |
| 66 | + schemaProperties; |
| 67 | + value = untrack(() => getDefaultFieldState(ctx, retrievedSchema, value) as SchemaObjectValue); |
| 68 | + }) |
| 69 | +
|
56 | 70 | const schemaPropertiesOrder = $derived( |
57 | 71 | isSchemaObjectValue(schemaProperties) |
58 | | - ? orderProperties(schemaProperties, config.uiOptions?.order ?? createOriginalKeysOrder(schemaProperties)) |
59 | | - : [] |
| 72 | + ? orderProperties(schemaProperties, config.uiOptions?.order ?? createOriginalKeysOrder(schemaProperties)) |
| 73 | + : [] |
60 | 74 | ); |
61 | 75 |
|
62 | 76 | const ObjectProperty = $derived(getField(ctx, "objectProperty", config)); |
|
0 commit comments