From 8ab567e64c504a243f374a3468b52750b2f7fbe6 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 9 Jun 2021 12:29:27 +0100 Subject: [PATCH 1/5] Correctly set initial values when registering fields in forms --- .../standard-components/src/forms/Form.svelte | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/standard-components/src/forms/Form.svelte b/packages/standard-components/src/forms/Form.svelte index d73af72a47..9a2c909385 100644 --- a/packages/standard-components/src/forms/Form.svelte +++ b/packages/standard-components/src/forms/Form.svelte @@ -42,18 +42,6 @@ // Auto columns are always disabled const isAutoColumn = !!schema?.[field]?.autocolumn - if (fieldMap[field] != null) { - // Update disabled property just so that toggling the disabled field - // state in the builder makes updates in real time. - // We only need this because of optimisations which prevent fully - // remounting when settings change. - fieldMap[field].fieldState.update(state => { - state.disabled = disabled || fieldDisabled || isAutoColumn - return state - }) - return fieldMap[field] - } - // Create validation function based on field schema const constraints = schema?.[field]?.constraints const validate = createValidatorFromConstraints(constraints, field, table) @@ -67,6 +55,10 @@ fieldApi: makeFieldApi(field, defaultValue, validate), fieldSchema: schema?.[field] ?? {}, } + + // Set initial value + fieldMap[field].fieldApi.setValue(defaultValue, true) + return fieldMap[field] }, validate: () => { From f586042267db5836cda4929c3802a5dc42f931d5 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 9 Jun 2021 12:53:12 +0100 Subject: [PATCH 2/5] Add type setting to forms and fix inheriting initial values --- packages/standard-components/manifest.json | 7 +++++ .../standard-components/src/forms/Form.svelte | 31 ++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/standard-components/manifest.json b/packages/standard-components/manifest.json index 8f0f63f681..998d053fe3 100644 --- a/packages/standard-components/manifest.json +++ b/packages/standard-components/manifest.json @@ -1023,6 +1023,13 @@ "ValidateForm" ], "settings": [ + { + "type": "select", + "label": "Type", + "key": "type", + "options": ["Create", "Update"], + "defaultValue": "Create" + }, { "type": "schema", "label": "Schema", diff --git a/packages/standard-components/src/forms/Form.svelte b/packages/standard-components/src/forms/Form.svelte index 9a2c909385..438e93073e 100644 --- a/packages/standard-components/src/forms/Form.svelte +++ b/packages/standard-components/src/forms/Form.svelte @@ -8,6 +8,7 @@ export let theme export let size export let disabled = false + export let type = "Create" const component = getContext("component") const context = getContext("context") @@ -19,15 +20,29 @@ let fieldMap = {} // Returns the closes data context which isn't a built in context - const getInitialValues = context => { + const getInitialValues = (type, dataSource, context) => { + // Only inherit values for update forms + if (type !== "Update") { + return {} + } + // Only inherit values for forms targetting internal tables + if (!dataSource?.tableId) { + return {} + } + // Don't inherit values representing built in contexts if (["user", "url"].includes(context.closestComponentId)) { return {} } - return context[`${context.closestComponentId}`] || {} + // Only inherit values if the table ID matches + const closestContext = context[`${context.closestComponentId}`] || {} + if (dataSource.tableId !== closestContext?.tableId) { + return {} + } + return closestContext } // Use the closest data context as the initial form values - const initialValues = getInitialValues($context) + const initialValues = getInitialValues(type, dataSource, $context) // Form state contains observable data about the form const formState = writable({ values: initialValues, errors: {}, valid: true }) @@ -46,6 +61,7 @@ const constraints = schema?.[field]?.constraints const validate = createValidatorFromConstraints(constraints, field, table) + // Construct field object fieldMap[field] = { fieldState: makeFieldState( field, @@ -57,7 +73,14 @@ } // Set initial value - fieldMap[field].fieldApi.setValue(defaultValue, true) + const initialValue = get(fieldMap[field].fieldState).value + formState.update(state => ({ + ...state, + values: { + ...state.values, + [field]: initialValue, + }, + })) return fieldMap[field] }, From e5737987c550851b3207716537e6baf11562534a Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 9 Jun 2021 12:55:17 +0100 Subject: [PATCH 3/5] Change form type setting to 'actionType' to avoid clashing with old settings --- packages/standard-components/manifest.json | 2 +- packages/standard-components/src/forms/Form.svelte | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/standard-components/manifest.json b/packages/standard-components/manifest.json index 998d053fe3..81aa63e1e3 100644 --- a/packages/standard-components/manifest.json +++ b/packages/standard-components/manifest.json @@ -1026,7 +1026,7 @@ { "type": "select", "label": "Type", - "key": "type", + "key": "actionType", "options": ["Create", "Update"], "defaultValue": "Create" }, diff --git a/packages/standard-components/src/forms/Form.svelte b/packages/standard-components/src/forms/Form.svelte index 438e93073e..327cc70572 100644 --- a/packages/standard-components/src/forms/Form.svelte +++ b/packages/standard-components/src/forms/Form.svelte @@ -8,7 +8,7 @@ export let theme export let size export let disabled = false - export let type = "Create" + export let actionType = "Create" const component = getContext("component") const context = getContext("context") @@ -42,7 +42,7 @@ } // Use the closest data context as the initial form values - const initialValues = getInitialValues(type, dataSource, $context) + const initialValues = getInitialValues(actionType, dataSource, $context) // Form state contains observable data about the form const formState = writable({ values: initialValues, errors: {}, valid: true }) From 739abfd706a87d78af8abc4150d981aa0d6f30b0 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 9 Jun 2021 13:08:36 +0100 Subject: [PATCH 4/5] Update autoscreens to add new form type setting --- .../src/builderStore/store/screenTemplates/newRowScreen.js | 1 + .../src/builderStore/store/screenTemplates/rowDetailScreen.js | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/builder/src/builderStore/store/screenTemplates/newRowScreen.js b/packages/builder/src/builderStore/store/screenTemplates/newRowScreen.js index be399b4bb8..980f71742b 100644 --- a/packages/builder/src/builderStore/store/screenTemplates/newRowScreen.js +++ b/packages/builder/src/builderStore/store/screenTemplates/newRowScreen.js @@ -35,6 +35,7 @@ const createScreen = table => { const form = makeMainForm() .instanceName("Form") .customProps({ + actionType: "Create", theme: "spectrum--lightest", size: "spectrum--medium", dataSource: { diff --git a/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js b/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js index b0cf694a5d..f15f1de5a4 100644 --- a/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js +++ b/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js @@ -110,6 +110,7 @@ const createScreen = table => { const form = makeMainForm() .instanceName("Form") .customProps({ + actionType: "Update", theme: "spectrum--lightest", size: "spectrum--medium", dataSource: { From 4d8e878f6f6c24e47eb94189845cb5f236626353 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 9 Jun 2021 14:17:11 +0100 Subject: [PATCH 5/5] Allow string templates to handle null objects without failing --- packages/string-templates/src/index.cjs | 3 --- packages/string-templates/test/basic.spec.js | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/string-templates/src/index.cjs b/packages/string-templates/src/index.cjs index a14a3efcda..0b4515b815 100644 --- a/packages/string-templates/src/index.cjs +++ b/packages/string-templates/src/index.cjs @@ -16,9 +16,6 @@ registerAll(hbsInstance) * utility function to check if the object is valid */ function testObject(object) { - if (object == null) { - throw "Unable to process null object" - } // JSON stringify will fail if there are any cycles, stops infinite recursion try { JSON.stringify(object) diff --git a/packages/string-templates/test/basic.spec.js b/packages/string-templates/test/basic.spec.js index f5c7c8be75..f9b994be99 100644 --- a/packages/string-templates/test/basic.spec.js +++ b/packages/string-templates/test/basic.spec.js @@ -81,14 +81,14 @@ describe("Test that the object processing works correctly", () => { expect(error).not.toBeNull() }) - it("should fail gracefully when wrong type is passed in", async () => { + it("should be able to handle null objects", async () => { let error = null try { await processObject(null, null) } catch (err) { error = err } - expect(error).not.toBeNull() + expect(error).toBeNull() }) })