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: Always include children of new entities. #114

Merged
merged 1 commit into from
Oct 24, 2024
Merged
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
1 change: 1 addition & 0 deletions src/fields/listField.ts
Original file line number Diff line number Diff line change
@@ -82,6 +82,7 @@ export function newListFieldState<T, K extends keyof T, U>(
return _tick.value > 0 && _childTick.value > 0 ? ((parentInstance[key] ?? []) as any as U[]) : fail();
},

_kind: "list",
_focused: false,
_readOnly: false,
_loading: false,
6 changes: 4 additions & 2 deletions src/fields/objectField.ts
Original file line number Diff line number Diff line change
@@ -341,8 +341,10 @@ export function newObjectState<T, P = any>(
// If the caller used useFormState.ifUndefined to provide some default values, then those keys may not
// look dirty, but if we're new we should include them anyway.
(this.isNewEntity && (f as any)._kind === "value" && f.value !== undefined) ||
// And unless they're empty sub-objects
(this.isNewEntity && (f as any)._kind === "object" && Object.entries(f.changedValue).length > 0)
// ...or they're non-empty sub-objects
(this.isNewEntity && (f as any)._kind === "object" && Object.entries(f.changedValue).length > 0) ||
// ...or they're non-empty sub-lists
(this.isNewEntity && (f as any)._kind === "list" && f.value?.length > 0)
) {
result[f.key] = f.changedValue;
}
19 changes: 19 additions & 0 deletions src/formState.test.tsx
Original file line number Diff line number Diff line change
@@ -1232,6 +1232,25 @@ describe("formState", () => {
expect(formState.changedValue).toEqual({ firstName: "f" });
});

it("changedValue includes initialized-and-unchanged list fields", () => {
// Given a new author that is initialized with a new book
const formState = createObjectState(authorWithBooksConfig, {
books: [{ title: "t1" }],
});
// Then changedValue realizes that even though `books` is not changed, it should be included
expect(formState.books.dirty).toBe(false);
expect(formState.changedValue).toEqual({ books: [{ title: "t1" }] });
});

it("changedValue skips initialized-and-unchanged list fields that are empty", () => {
// Given a new author that is initialized with no books
const formState = createObjectState(authorWithBooksConfig, {
books: [],
});
// Then changedValue doesn't include the books
expect(formState.changedValue).toEqual({});
});

it("changedValue includes new entity nested fields", () => {
// Given a new author with an address FK
const formState = createObjectState(authorWithAddressFkConfig, {