Skip to content

Commit

Permalink
manage collection of primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPerard committed Oct 9, 2024
1 parent d2a0b03 commit e80ef63
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion apps/examples/src/pages/collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -290,7 +291,7 @@ const Collection = () => {

const ConditionedCollection = () => {
const conditionedCollection = useCollection("conditioned", {
defaultValue: ["default value"],
defaultValue: ["Default value"],
});

return (
Expand Down
11 changes: 5 additions & 6 deletions packages/formiz-core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,17 +730,16 @@ export const createStore = <Values extends object = DefaultFormValues>(
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,
Expand Down
2 changes: 1 addition & 1 deletion packages/formiz-core/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions packages/formiz-core/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -84,6 +85,18 @@ export const omitValueByFieldName = <Values = any>(
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
Expand Down

0 comments on commit e80ef63

Please sign in to comment.