@homebound/form-state 2.26.1 → 2.26.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fields/listField.js
CHANGED
|
@@ -53,6 +53,7 @@ function newListFieldState(parentCopy, parentInstance, parentState, key, rules,
|
|
|
53
53
|
var _a;
|
|
54
54
|
return _tick.value > 0 && _childTick.value > 0 ? ((_a = parentInstance[key]) !== null && _a !== void 0 ? _a : []) : (0, utils_1.fail)();
|
|
55
55
|
},
|
|
56
|
+
_kind: "list",
|
|
56
57
|
_focused: false,
|
|
57
58
|
_readOnly: false,
|
|
58
59
|
_loading: false,
|
|
@@ -196,6 +196,7 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
196
196
|
return null;
|
|
197
197
|
}
|
|
198
198
|
getFields(this).forEach((f) => {
|
|
199
|
+
var _a;
|
|
199
200
|
if (f._computed)
|
|
200
201
|
return;
|
|
201
202
|
// References only include the id key below
|
|
@@ -205,8 +206,10 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
205
206
|
// If the caller used useFormState.ifUndefined to provide some default values, then those keys may not
|
|
206
207
|
// look dirty, but if we're new we should include them anyway.
|
|
207
208
|
(this.isNewEntity && f._kind === "value" && f.value !== undefined) ||
|
|
208
|
-
//
|
|
209
|
-
(this.isNewEntity && f._kind === "object" && Object.entries(f.changedValue).length > 0)
|
|
209
|
+
// ...or they're non-empty sub-objects
|
|
210
|
+
(this.isNewEntity && f._kind === "object" && Object.entries(f.changedValue).length > 0) ||
|
|
211
|
+
// ...or they're non-empty sub-lists
|
|
212
|
+
(this.isNewEntity && f._kind === "list" && ((_a = f.value) === null || _a === void 0 ? void 0 : _a.length) > 0)) {
|
|
210
213
|
result[f.key] = f.changedValue;
|
|
211
214
|
}
|
|
212
215
|
});
|
package/dist/formState.test.js
CHANGED
|
@@ -1069,6 +1069,23 @@ describe("formState", () => {
|
|
|
1069
1069
|
// because this might trigger creating a throw-away entity
|
|
1070
1070
|
expect(formState.changedValue).toEqual({ firstName: "f" });
|
|
1071
1071
|
});
|
|
1072
|
+
it("changedValue includes initialized-and-unchanged list fields", () => {
|
|
1073
|
+
// Given a new author that is initialized with a new book
|
|
1074
|
+
const formState = (0, objectField_1.createObjectState)(authorWithBooksConfig, {
|
|
1075
|
+
books: [{ title: "t1" }],
|
|
1076
|
+
});
|
|
1077
|
+
// Then changedValue realizes that even though `books` is not changed, it should be included
|
|
1078
|
+
expect(formState.books.dirty).toBe(false);
|
|
1079
|
+
expect(formState.changedValue).toEqual({ books: [{ title: "t1" }] });
|
|
1080
|
+
});
|
|
1081
|
+
it("changedValue skips initialized-and-unchanged list fields that are empty", () => {
|
|
1082
|
+
// Given a new author that is initialized with no books
|
|
1083
|
+
const formState = (0, objectField_1.createObjectState)(authorWithBooksConfig, {
|
|
1084
|
+
books: [],
|
|
1085
|
+
});
|
|
1086
|
+
// Then changedValue doesn't include the books
|
|
1087
|
+
expect(formState.changedValue).toEqual({});
|
|
1088
|
+
});
|
|
1072
1089
|
it("changedValue includes new entity nested fields", () => {
|
|
1073
1090
|
// Given a new author with an address FK
|
|
1074
1091
|
const formState = (0, objectField_1.createObjectState)(authorWithAddressFkConfig, {
|
|
@@ -346,11 +346,12 @@ describe("useFormState", () => {
|
|
|
346
346
|
init: { input: undefined, map: (d) => d },
|
|
347
347
|
autoSave,
|
|
348
348
|
});
|
|
349
|
-
return (0, jsx_runtime_1.jsx)("div", { "data-testid": "len", children: form.books.value.length });
|
|
349
|
+
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("div", { "data-testid": "len", children: form.books.value.length }), (0, jsx_runtime_1.jsx)("div", { "data-testid": "dirty", children: String(form.books.dirty) })] }));
|
|
350
350
|
}
|
|
351
351
|
// Then we can still read it as empty
|
|
352
352
|
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
353
353
|
expect(r.len).toHaveTextContent("0");
|
|
354
|
+
expect(r.dirty).toHaveTextContent("false");
|
|
354
355
|
});
|
|
355
356
|
it("does not infinite loop when calling set inside of auto-save", async () => {
|
|
356
357
|
// Given a component
|