Skip to content

Commit 4f8c576

Browse files
committed
[form] Fix defaults population for properties from dependencies
1 parent 4d629ea commit 4f8c576

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

.changeset/slimy-vans-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Fix defaults population for properties from `dependencies`

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<script lang="ts">
2+
import { untrack } from 'svelte';
3+
4+
import { deepEqual } from '@/lib/deep-equal.js'
25
import {
36
getDefaultValueForType,
47
getSimpleSchemaType,
58
isAdditionalProperty,
69
isSchemaExpandable,
710
isSchemaObjectValue,
811
orderProperties,
12+
type Schema,
13+
type SchemaObjectValue,
914
} from '@/core/index.js';
1015
1116
import type { UiSchema } from '../../ui-schema.js';
@@ -34,12 +39,6 @@
3439
config,
3540
value = $bindable(),
3641
}: FieldProps<"object"> = $props();
37-
$effect(() => {
38-
if (value === undefined) {
39-
value = {}
40-
}
41-
})
42-
4342
const newKeySeparator = $derived(config.uiOptions?.duplicateKeySuffixSeparator ?? "-")
4443
const objCtx: ObjectContext = {
4544
get newKeySeparator() {
@@ -49,14 +48,29 @@
4948
setObjectContext(objCtx)
5049
5150
// 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.
5353
const retrievedSchema = $derived(retrieveSchema(ctx, config.schema, value))
5454
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+
5670
const schemaPropertiesOrder = $derived(
5771
isSchemaObjectValue(schemaProperties)
58-
? orderProperties(schemaProperties, config.uiOptions?.order ?? createOriginalKeysOrder(schemaProperties))
59-
: []
72+
? orderProperties(schemaProperties, config.uiOptions?.order ?? createOriginalKeysOrder(schemaProperties))
73+
: []
6074
);
6175
6276
const ObjectProperty = $derived(getField(ctx, "objectProperty", config));

0 commit comments

Comments
 (0)