Skip to content

Commit

Permalink
fix: Always include children of new entities. (#114)
Browse files Browse the repository at this point in the history
...if the list isn't empty.
  • Loading branch information
stephenh authored Oct 24, 2024
1 parent bb4f42d commit 08e4148
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/fields/listField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/fields/objectField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions src/formState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 08e4148

Please sign in to comment.