Skip to content

Commit

Permalink
FIX: issue with common type data on collection item deletion + collec…
Browse files Browse the repository at this point in the history
…tion initial values clean after reset (#231)

* fix: use undefined as collection actions default value to prevent issues

* fix: field first value from null oldFieldById value

* fix: collection initialValues after reset issue
  • Loading branch information
HugoPerard authored Dec 10, 2024
1 parent 85b3879 commit 86d5b5d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
32 changes: 28 additions & 4 deletions apps/examples/cypress/e2e/collection.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ describe("Collection", () => {
});

it("Remove all items", () => {
cy.get('[data-test="collection-item[1]"] [aria-label="Delete"]').click();
cy.get('[aria-label="Delete member 1"]').click();
cy.get('[data-test="collection-item[1]').should("not.exist");

cy.get('[data-test="collection-item[0]"] [aria-label="Delete"]').click();
cy.get('[aria-label="Delete member 0"]').click();
cy.get('[data-test="collection-item[0]').should("not.exist");

cy.formSubmit();
Expand All @@ -60,7 +60,7 @@ describe("Collection", () => {
cy.field("members[1].name").fill("John");
cy.field("members[2].name").fill("Doe");

cy.get('[data-test="collection-item[2]"] [aria-label="Delete"]').click();
cy.get('[aria-label="Delete member 2"]').click();
cy.field("members[1].name").hasValue("John");
});

Expand All @@ -69,7 +69,7 @@ describe("Collection", () => {
cy.field("members[1].name").fill("John");
cy.field("members[2].name").fill("Doe");

cy.get('[data-test="collection-item[1]"] [aria-label="Delete"]').click();
cy.get('[aria-label="Delete member 1"]').click();
cy.field("members[1].name").hasValue("Doe");
});

Expand All @@ -90,6 +90,13 @@ describe("Collection", () => {
cy.field("members[0].company").hasValue("Initial Company (1)");
cy.field("members[1].name").hasValue("Initial Name (2)");
cy.field("members[1].company").hasValue("Initial Company (2)");

// Check that new item don't recover already consumed initial values
cy.get('[data-test="collection-item[0]"] [aria-label="Add"]').click();
cy.field("members[2].name").hasValue("Initial Name (2)");
cy.field("members[2].company").hasValue("Initial Company (2)");
cy.field("members[1].name").hasValue("");
cy.field("members[1].company").hasValue("Default company (2)");
});

it("Mounted/Unmounted collection", () => {
Expand Down Expand Up @@ -148,4 +155,21 @@ describe("Collection", () => {
cy.field("items[2]").should("exist");
cy.field("items[3]").should("not.exist");
});

it("Remove primary data collection item", () => {
cy.get("button").contains("Display").click();

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

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

cy.field("conditioned[3]").should("exist").type("new value");

cy.get('[aria-label="Delete conditioned 2"]').click();

cy.field("conditioned[2]").hasValue("new value");
});
});
4 changes: 2 additions & 2 deletions apps/examples/src/pages/collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const Collection = () => {
</Box>
<Box pt="8">
<IconButton
aria-label="Delete"
aria-label={`Delete member ${index}`}
icon={<Icon as={FiTrash2} />}
onClick={() => collection.remove(index)}
variant="ghost"
Expand Down Expand Up @@ -303,7 +303,7 @@ const ConditionedCollection = () => {
label={`Conditioned ${index}`}
/>
<IconButton
aria-label="Delete"
aria-label={`Delete conditioned ${index}`}
icon={<Icon as={FiTrash2} />}
onClick={() => conditionedCollection.remove(index)}
variant="ghost"
Expand Down
1 change: 0 additions & 1 deletion packages/formiz-core/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const formInterfaceSelector = <
},
reset: (options?: ResetOptions) => {
state.actions.reset(options);
state.actions.resetInitialValues();
},
submitStep: state.actions.submitStep,
goToStep: state.actions.goToStep,
Expand Down
26 changes: 4 additions & 22 deletions packages/formiz-core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,24 +364,6 @@ export const createStore = <Values extends object = DefaultFormValues>(
});
},

resetInitialValues: () => {
set((state) => {
let initialValues = cloneDeep(
state.formConfigRef.current?.initialValues
);

setTimeout(() => {
state.fields.forEach((field) => {
initialValues = omitValueByFieldName(initialValues, field.name);
});
});

return {
initialValues,
};
});
},

// FIELDS
registerField: (
fieldId,
Expand Down Expand Up @@ -770,7 +752,7 @@ export const createStore = <Values extends object = DefaultFormValues>(
if (collectionName && typeof keys === "object") {
return {
externalValues: lodashMerge(cloneDeep(state.externalValues), {
[collectionName]: keys.map(() => null),
[collectionName]: keys.map(() => undefined),
}),
};
}
Expand Down Expand Up @@ -910,7 +892,7 @@ export const createStore = <Values extends object = DefaultFormValues>(
state.actions.insertMultipleCollectionValues({
collectionId,
collectionName,
} as CollectionActionProps)(0, [value ?? null], options);
} as CollectionActionProps)(0, [value ?? undefined], options);
return { collections: state.collections };
});
},
Expand All @@ -925,7 +907,7 @@ export const createStore = <Values extends object = DefaultFormValues>(
collectionName
);
const { newValues } = insertItemsAtIndex({
source: [value ?? null],
source: [value ?? undefined],
target: currentExternalValues,
index: -1,
});
Expand All @@ -942,7 +924,7 @@ export const createStore = <Values extends object = DefaultFormValues>(
state.actions.insertMultipleCollectionValues({
collectionId,
collectionName,
})(-1, [value ?? null], options);
})(-1, [value ?? undefined], options);

return { collections: state.collections };
});
Expand Down
1 change: 0 additions & 1 deletion packages/formiz-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ export interface Store<Values extends object = DefaultFormValues> {
>
): void;
reset(options?: ResetOptions): void;
resetInitialValues(): void;

registerField<Value = unknown, FormattedValue = Value>(
fieldId: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/formiz-core/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const getFieldFirstValue = ({
if (!!newField.value) {
return newField.value;
}
if (!!oldFieldById?.value) {
if (oldFieldById?.value !== undefined) {
return oldFieldById.value;
}
if (keepValue !== undefined) {
Expand Down

0 comments on commit 86d5b5d

Please sign in to comment.