From 64c3f79dc9ebaf0de49bdada5a2f58bd966f6e0f Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 1 Nov 2022 17:46:20 +0000 Subject: [PATCH 1/2] General approach not suitable at the component level. Backing out change --- .../bbui/src/Form/Core/Multiselect.svelte | 29 +------------------ 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/packages/bbui/src/Form/Core/Multiselect.svelte b/packages/bbui/src/Form/Core/Multiselect.svelte index 784cee70dc..d6c4dc23ac 100644 --- a/packages/bbui/src/Form/Core/Multiselect.svelte +++ b/packages/bbui/src/Form/Core/Multiselect.svelte @@ -1,6 +1,6 @@ Date: Wed, 2 Nov 2022 00:37:08 +0000 Subject: [PATCH 2/2] Moved array value parsing back into the inner form --- .../src/components/app/forms/InnerForm.svelte | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/client/src/components/app/forms/InnerForm.svelte b/packages/client/src/components/app/forms/InnerForm.svelte index 6b7e1a84d0..0c512bbe0f 100644 --- a/packages/client/src/components/app/forms/InnerForm.svelte +++ b/packages/client/src/components/app/forms/InnerForm.svelte @@ -128,6 +128,23 @@ return fields.find(field => get(field).name === name) } + const getDefault = (defaultValue, schema, type) => { + // Remove any values not present in the field schema + // Convert any values supplied to string + if (Array.isArray(defaultValue) && type == "array") { + return defaultValue.reduce((acc, entry) => { + let processedOption = String(entry) + let schemaOptions = schema.constraints.inclusion + if (schemaOptions.indexOf(processedOption) > -1) { + acc.push(processedOption) + } + return acc + }, []) + } else { + return defaultValue + } + } + const formApi = { registerField: ( field, @@ -153,8 +170,10 @@ table ) + const parsedDefault = getDefault(defaultValue, schema?.[field], type) + // If we've already registered this field then keep some existing state - let initialValue = Helpers.deepGet(initialValues, field) ?? defaultValue + let initialValue = Helpers.deepGet(initialValues, field) ?? parsedDefault let initialError = null let fieldId = `id-${Helpers.uuid()}` const existingField = getField(field) @@ -187,11 +206,11 @@ error: initialError, disabled: disabled || fieldDisabled || (isAutoColumn && !editAutoColumns), - defaultValue, + defaultValue: parsedDefault, validator, lastUpdate: Date.now(), }, - fieldApi: makeFieldApi(field, defaultValue), + fieldApi: makeFieldApi(field, parsedDefault), fieldSchema: schema?.[field] ?? {}, })