From fd21503f3e665c826e4174b957945e3e229df1aa Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 24 Sep 2024 12:43:41 +0100 Subject: [PATCH 01/11] Enable default values on options columns with validation and fix default values being available on primary display columns --- .../DataTable/modals/CreateEditColumn.svelte | 57 ++++++++++++------- packages/shared-core/src/table.ts | 2 +- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index a956d09ee6..b04ccdb6a9 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -136,9 +136,8 @@ } $: initialiseField(field, savingColumn) $: checkConstraints(editableColumn) - $: required = hasDefault - ? false - : !!editableColumn?.constraints?.presence || primaryDisplay + $: required = + primaryDisplay || (!hasDefault && !!editableColumn?.constraints?.presence) $: uneditable = $tables.selected?._id === TableNames.USERS && UNEDITABLE_USER_FIELDS.includes(editableColumn.name) @@ -168,7 +167,9 @@ $: canBeDisplay = canBeDisplayColumn(editableColumn.type) && !editableColumn.autocolumn $: canHaveDefault = - isEnabled("DEFAULT_VALUES") && canHaveDefaultColumn(editableColumn.type) + !required && + isEnabled("DEFAULT_VALUES") && + canHaveDefaultColumn(editableColumn.type) $: canBeRequired = editableColumn?.type !== LINK_TYPE && !uneditable && @@ -187,11 +188,11 @@ (originalName && SWITCHABLE_TYPES[field.type] && !editableColumn?.autocolumn) - $: allowedTypes = getAllowedTypes(datasource).map(t => ({ fieldId: makeFieldId(t.type, t.subtype), ...t, })) + $: bindings = getBindings({ table }) const fieldDefinitions = Object.values(FIELDS).reduce( // Storing the fields by complex field id @@ -281,6 +282,20 @@ delete saveColumn.fieldName } + // Ensure the field is not required if we have a default value + if (saveColumn.default) { + saveColumn.constraints.presence = false + } + + // Delete default value for options fields if the option is no longer available + if ( + saveColumn.type === FieldType.OPTIONS && + saveColumn.default && + !saveColumn.constraints.inclusion?.includes(saveColumn.default) + ) { + delete saveColumn.default + } + try { await tables.saveField({ originalName, @@ -727,7 +742,7 @@ formula: e.detail, } }} - bindings={getBindings({ table })} + {bindings} allowJS context={rowGoldenSample} /> @@ -766,27 +781,27 @@ {/if} {#if canHaveDefault} -
+ {#if editableColumn.type === FieldType.OPTIONS} + {:else} Date: Fri, 27 Sep 2024 08:33:09 +0100 Subject: [PATCH 05/11] Account for both shapes of the required constraint and ensure required trumps default values --- .../backend/DataTable/modals/CreateEditColumn.svelte | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index c4c3d661a3..05e0eba042 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -137,7 +137,9 @@ $: initialiseField(field, savingColumn) $: checkConstraints(editableColumn) $: required = - primaryDisplay || (!hasDefault && !!editableColumn?.constraints?.presence) + primaryDisplay || + editableColumn?.constraints?.presence === true || + editableColumn?.constraints?.presence?.allowEmpty === false $: uneditable = $tables.selected?._id === TableNames.USERS && UNEDITABLE_USER_FIELDS.includes(editableColumn.name) From f6a783a9f13a7c0cfc601ba23664e010f9b29a70 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 2 Oct 2024 10:25:22 +0100 Subject: [PATCH 06/11] Update submodules --- .../components/backend/DataTable/modals/CreateEditColumn.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index c7a9fecd65..7172689d69 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -767,7 +767,7 @@ formula: e.detail, } }} - {bindings} + bindings={getBindings({ table })} allowJS context={rowGoldenSample} /> From a49e779d111f051f42a718debdea2e1a58c8d43f Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 2 Oct 2024 10:50:22 +0100 Subject: [PATCH 07/11] Add default value for multi-selects and improve default value validation --- packages/account-portal | 2 +- .../DataTable/modals/CreateEditColumn.svelte | 39 +++++++---- packages/pro | 2 +- .../src/utilities/rowProcessor/index.ts | 6 +- packages/shared-core/src/table.ts | 2 +- yarn.lock | 64 ++++++++++++++++++- 6 files changed, 93 insertions(+), 22 deletions(-) diff --git a/packages/account-portal b/packages/account-portal index 905773d708..3e24f6293f 160000 --- a/packages/account-portal +++ b/packages/account-portal @@ -1 +1 @@ -Subproject commit 905773d70854a43c6ef2461c7a49671bff56fedc +Subproject commit 3e24f6293ff5ee5f9b42822e001504e3bbf19cc0 diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 7172689d69..19cc4ec1c0 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -207,6 +207,11 @@ }, ...getUserBindings(), ] + $: sanitiseDefaultValue( + editableColumn.type, + editableColumn.constraints?.inclusion || [], + editableColumn.default + ) const fieldDefinitions = Object.values(FIELDS).reduce( // Storing the fields by complex field id @@ -301,15 +306,6 @@ delete saveColumn.default } - // Delete default value for options fields if the option is no longer available - if ( - saveColumn.type === FieldType.OPTIONS && - saveColumn.default && - !saveColumn.constraints.inclusion?.includes(saveColumn.default) - ) { - delete saveColumn.default - } - // Ensure primary display columns are always required and don't have default values if (primaryDisplay) { saveColumn.constraints.presence = { allowEmpty: false } @@ -318,7 +314,7 @@ // Ensure the field is not required if we have a default value if (saveColumn.default) { - saveColumn.constraints.presence = { allowEmpty: true } + saveColumn.constraints.presence = false } try { @@ -567,6 +563,20 @@ return newError } + const sanitiseDefaultValue = (type, options, defaultValue) => { + if (!defaultValue?.length) { + return + } + // Delete default value for options fields if the option is no longer available + if (type === FieldType.OPTIONS && !options.includes(defaultValue)) { + delete editableColumn.default + } + // Filter array default values to only valid options + if (type === FieldType.ARRAY) { + editableColumn.default = defaultValue.filter(x => options.includes(x)) + } + } + onMount(() => { mounted = true }) @@ -774,9 +784,9 @@
{:else if editableColumn.type === JSON_TYPE} - + {/if} {#if editableColumn.type === AUTO_TYPE || editableColumn.autocolumn}