From 917e837ffc1b3c50fba82ff55ef299b4bcf1ba9a Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 18 Sep 2023 18:27:58 +0100 Subject: [PATCH] Make update type forms to work with global bindings --- .../client/src/components/Component.svelte | 1 + .../src/components/app/forms/Form.svelte | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index 3ac48146b3..d7d6722e1a 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -216,6 +216,7 @@ errorState, parent: id, ancestors: [...($component?.ancestors ?? []), instance._component], + path: [...($component?.path ?? []), id], }) const initialise = (instance, force = false) => { diff --git a/packages/client/src/components/app/forms/Form.svelte b/packages/client/src/components/app/forms/Form.svelte index 87883fe4b6..c973ff000d 100644 --- a/packages/client/src/components/app/forms/Form.svelte +++ b/packages/client/src/components/app/forms/Form.svelte @@ -20,6 +20,7 @@ export let editAutoColumns = false const context = getContext("context") + const component = getContext("component") const { API, fetchDatasourceSchema } = getContext("sdk") const getInitialFormStep = () => { @@ -37,28 +38,36 @@ $: fetchSchema(dataSource) $: schemaKey = generateSchemaKey(schema) - $: initialValues = getInitialValues(actionType, dataSource, $context) + $: initialValues = getInitialValues( + actionType, + dataSource, + $component.path, + $context + ) $: resetKey = Helpers.hashString( schemaKey + JSON.stringify(initialValues) + disabled ) // Returns the closes data context which isn't a built in context - const getInitialValues = (type, dataSource, context) => { + const getInitialValues = (type, dataSource, path, context) => { // Only inherit values for update forms if (type !== "Update") { return {} } // Only inherit values for forms targeting internal tables - if (!dataSource?.tableId) { + const dsType = dataSource?.type + if (dsType !== "table" && dsType !== "viewV2") { return {} } - // Don't inherit values representing built in contexts - if (["user", "url"].includes(context.closestComponentId)) { - return {} + // Look up the component tree and find something that is provided by an + // ancestor that matches our datasource. This is for backwards compatibility + // as previously we could use the "closest" context. + for (let id of path.reverse().slice(1)) { + if (context[id]?.tableId === dataSource.tableId) { + return context[id] + } } - // Always inherit the closest datasource - const closestContext = context[`${context.closestComponentId}`] || {} - return closestContext || {} + return {} } // Fetches the form schema from this form's dataSource