Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: collection default value reset #229

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/examples/cypress/e2e/collection.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ describe("Collection", () => {
cy.field("members[1].company").hasValue("Initial Company (2)");
});

it("Default values", () => {
cy.get("button").contains("Display").click();

cy.field("conditioned[0]").hasValue("default value");
cy.field("conditioned[1]").should("not.exist");

cy.get("button").contains("Add item").click();

cy.field("conditioned[1]").should("exist");

cy.get("button").contains("Reset form").click();
cy.field("conditioned[1]").should("not.exist");
});

it("Reset with new members", () => {
cy.field("members[0].name").hasValue("Default name (1)");
cy.field("members[0].company").hasValue("Initial Company (1)");
Expand Down
5 changes: 3 additions & 2 deletions apps/examples/src/pages/collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { useState } from "react";
const DEFAULT_VALUES = {
members: [{ name: "Default name (1)" }, { company: "Default company (2)" }],
};

const INITIAL_VALUES = {
members: [
{ company: "Initial Company (1)" },
Expand Down Expand Up @@ -290,7 +289,9 @@ const Collection = () => {
};

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

return (
<Stack flex={1}>
Expand Down
16 changes: 12 additions & 4 deletions packages/formiz-core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,23 @@ export const createStore = <Values extends object = DefaultFormValues>(
collection.name
) as NullablePartial<Values>[];

const storeResetDefaultValue = getValueByFieldName(
state.resetDefaultValues,
collection.name
);

const resetKeys =
(collectionFields ?? storeResetDefaultValue)?.map(
(_, index) => collection.keys?.[index] ?? index.toString()
) ?? [];

state.collections.set(collectionId, {
name: collection.name,
isPristine: isResetAllowed("pristine", resetOptions)
? true
: state.collections.get(collectionId)?.isPristine ?? true,
keys: isResetAllowed("values", resetOptions)
? collectionFields?.map(
(_, index) => collection.keys?.[index] ?? index.toString()
) ?? []
? resetKeys
: state.collections.get(collectionId)?.keys ?? [],
});
});
Expand Down Expand Up @@ -713,7 +721,7 @@ export const createStore = <Values extends object = DefaultFormValues>(
};

state.collections.set(collectionId, {
...newCollection,
name: newCollection.name,
keys: getKeys(),
isPristine: oldCollectionById?.isPristine ?? true,
});
Expand Down
6 changes: 6 additions & 0 deletions packages/formiz-core/src/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export const useCollection = <Data = unknown>(
);
}

if (!initialValues && !!defaultValue && !Array.isArray(defaultValue)) {
console.error(
`Default value for the collection "${name}" is not an array! Fallback to an empty array.`
);
}

const initialValuesArray = Array.isArray(initialValues)
? initialValues
: Array.isArray(defaultValue)
Expand Down
6 changes: 5 additions & 1 deletion packages/formiz-core/src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ export const useForm = <Values extends object = any>(
}
}, [formActions, formConfig?.ready]);

const formStateRef = useRef(formState);
formStateRef.current = formState;
useEffect(() => {
formActions.updateConfig(formConfigRef);
if (formConfig?.id && formConfig.id !== formStateRef.current.id) {
formActions.updateConfig(formConfigRef);
}
}, [formActions, formConfig?.id]);

return formState;
Expand Down
Loading