diff --git a/apps/examples/src/pages/collection.tsx b/apps/examples/src/pages/collection.tsx index 7d495e5c..41991f77 100644 --- a/apps/examples/src/pages/collection.tsx +++ b/apps/examples/src/pages/collection.tsx @@ -28,6 +28,7 @@ const INITIAL_VALUES = { { company: "Initial Company (1)" }, { name: "Initial Name (2)", company: "Initial Company (2)" }, ], + conditioned: ["Initial value (1)", "Initial value (2)"], }; type FormValues = any; @@ -290,7 +291,7 @@ const Collection = () => { const ConditionedCollection = () => { const conditionedCollection = useCollection("conditioned", { - defaultValue: ["default value"], + defaultValue: ["Default value"], }); return ( diff --git a/packages/formiz-core/src/store.ts b/packages/formiz-core/src/store.ts index 6162f3f8..c46fa289 100644 --- a/packages/formiz-core/src/store.ts +++ b/packages/formiz-core/src/store.ts @@ -730,17 +730,16 @@ export const createStore = ( isPristine: oldCollectionById?.isPristine ?? true, }); - let newInitialValues = state.initialValues; let newExternalValues = state.externalValues; - - if (Array.isArray(initialValues) && isArrayEmpty(initialValues)) { - newInitialValues = - omitValueByFieldName(newInitialValues, newCollection.name) ?? {}; - } if (Array.isArray(externalValues) && isArrayEmpty(externalValues)) { newExternalValues = omitValueByFieldName(newExternalValues, newCollection.name) ?? {}; } + let newInitialValues = state.initialValues; + if (Array.isArray(initialValues) && isArrayEmpty(initialValues)) { + newInitialValues = + omitValueByFieldName(newInitialValues, newCollection.name) ?? {}; + } return { collections: state.collections, diff --git a/packages/formiz-core/src/useField.ts b/packages/formiz-core/src/useField.ts index 15069d8b..b35f683b 100644 --- a/packages/formiz-core/src/useField.ts +++ b/packages/formiz-core/src/useField.ts @@ -44,7 +44,7 @@ export const useField = < required: false, validations: [], validationsAsync: [], - formatValue: (v) => v as FormattedValue, // TODO: replace as + formatValue: (v) => v as FormattedValue, ...config, }; const configRef = useRef(_config); diff --git a/packages/formiz-core/src/utils/form.ts b/packages/formiz-core/src/utils/form.ts index f0b8c4c8..43215736 100644 --- a/packages/formiz-core/src/utils/form.ts +++ b/packages/formiz-core/src/utils/form.ts @@ -13,6 +13,7 @@ import type { } from "@/types"; import { isObject } from "@/utils/global"; +import lodashSet from "lodash/set"; import lodashIsEmpty from "lodash/isEmpty"; import cloneDeep from "lodash/cloneDeep"; import lodashGet from "lodash/get"; @@ -84,6 +85,18 @@ export const omitValueByFieldName = ( if (!values || lodashIsEmpty(values)) { return undefined; } + + const isArraySyntax = fieldName.match(/\[([0-9]*)\]$/g); + if (isArraySyntax) { + // To manage case of collection where typeof collection[index] === "string" and the case of omit remove an item of the array + const currentValue = parseValues(cloneDeep(values)); + + // If there is a value, we replace it by undefined instead of remove item + return lodashGet(currentValue, fieldName) + ? lodashSet(currentValue, fieldName, undefined) + : currentValue; + } + return lodashOmit( parseValues(cloneDeep(values)), fieldName