From a4125a97c57d9ee1c8d621454534721fc5f659ad Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 26 Oct 2022 15:13:27 +0100 Subject: [PATCH] Refactored approach to parsing multiselect values and ensuring any parsed values trigger a field update --- .../bbui/src/Form/Core/Multiselect.svelte | 31 ++++++++++++++++++- .../src/components/app/forms/InnerForm.svelte | 25 ++------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/packages/bbui/src/Form/Core/Multiselect.svelte b/packages/bbui/src/Form/Core/Multiselect.svelte index eb39e39042..784cee70dc 100644 --- a/packages/bbui/src/Form/Core/Multiselect.svelte +++ b/packages/bbui/src/Form/Core/Multiselect.svelte @@ -1,6 +1,6 @@ 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, @@ -170,10 +153,8 @@ 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) ?? parsedDefault + let initialValue = Helpers.deepGet(initialValues, field) ?? defaultValue let initialError = null let fieldId = `id-${Helpers.uuid()}` const existingField = getField(field) @@ -206,11 +187,11 @@ error: initialError, disabled: disabled || fieldDisabled || (isAutoColumn && !editAutoColumns), - defaultValue: parsedDefault, + defaultValue, validator, lastUpdate: Date.now(), }, - fieldApi: makeFieldApi(field, parsedDefault), + fieldApi: makeFieldApi(field, defaultValue), fieldSchema: schema?.[field] ?? {}, })