@homebound/form-state 2.26.1 → 2.26.3
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,
|
|
@@ -57,6 +57,10 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
57
57
|
field = (0, listField_1.newListFieldState)(originalCopy, instance, getObjectState, key, config.rules || [], config, { type: "object", config: config.config }, (_d = config.strictOrder) !== null && _d !== void 0 ? _d : true, maybeAutoSave);
|
|
58
58
|
}
|
|
59
59
|
else if (config.type === "object") {
|
|
60
|
+
// Because our objectField will fundamentally want to do `child.firstName.set(...)` or
|
|
61
|
+
// even `child.firstName !== undefined`, etc., we "simplify" things by always setting
|
|
62
|
+
// an empty object, for our child valueFields/listFields to use/read from, although
|
|
63
|
+
// we then have to "oh right ignore {}" in places like `dirty`.
|
|
60
64
|
if (!instance[key]) {
|
|
61
65
|
instance[key] = {};
|
|
62
66
|
}
|
|
@@ -131,7 +135,10 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
131
135
|
return getFields(this).flatMap((f) => f.errors.map((e) => `${f.key}: ${e}`));
|
|
132
136
|
},
|
|
133
137
|
get dirty() {
|
|
134
|
-
return getFields(this).some((f) => f.dirty) ||
|
|
138
|
+
return (getFields(this).some((f) => f.dirty) ||
|
|
139
|
+
// `isUnset` checks if our `parent[key] === undefined`, which can mean "surely we're dirty",
|
|
140
|
+
// but as long as we've got some keys actually set
|
|
141
|
+
(this.isUnset() && !(0, utils_1.areEqual)(this.value, {})));
|
|
135
142
|
},
|
|
136
143
|
get isNewEntity() {
|
|
137
144
|
const idField = getFields(this).find((f) => f._isIdKey);
|
|
@@ -196,6 +203,7 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
196
203
|
return null;
|
|
197
204
|
}
|
|
198
205
|
getFields(this).forEach((f) => {
|
|
206
|
+
var _a;
|
|
199
207
|
if (f._computed)
|
|
200
208
|
return;
|
|
201
209
|
// References only include the id key below
|
|
@@ -205,8 +213,14 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
205
213
|
// If the caller used useFormState.ifUndefined to provide some default values, then those keys may not
|
|
206
214
|
// look dirty, but if we're new we should include them anyway.
|
|
207
215
|
(this.isNewEntity && f._kind === "value" && f.value !== undefined) ||
|
|
208
|
-
//
|
|
209
|
-
(this.isNewEntity &&
|
|
216
|
+
// ...or they're non-empty sub-objects
|
|
217
|
+
(this.isNewEntity &&
|
|
218
|
+
f._kind === "object" &&
|
|
219
|
+
// Child objects that are unset will have `changedValue = null`
|
|
220
|
+
f.changedValue &&
|
|
221
|
+
Object.entries(f.changedValue).length > 0) ||
|
|
222
|
+
// ...or they're non-empty sub-lists
|
|
223
|
+
(this.isNewEntity && f._kind === "list" && ((_a = f.value) === null || _a === void 0 ? void 0 : _a.length) > 0)) {
|
|
210
224
|
result[f.key] = f.changedValue;
|
|
211
225
|
}
|
|
212
226
|
});
|
package/dist/formState.test.js
CHANGED
|
@@ -595,6 +595,19 @@ describe("formState", () => {
|
|
|
595
595
|
a1.firstName.set("a1");
|
|
596
596
|
expect(a1.dirty).toBeFalsy();
|
|
597
597
|
});
|
|
598
|
+
it("realizes object fields set to null aren't actually dirty", () => {
|
|
599
|
+
// Given we started off with a `object: undefined`
|
|
600
|
+
const a1 = createAuthorWithAddressInputState({ address: undefined });
|
|
601
|
+
expect(a1.address.dirty).toBe(false);
|
|
602
|
+
expect(a1.address.value).toEqual({});
|
|
603
|
+
// And it's later set to null, i.e. from a mutation returning `{ address: null }`
|
|
604
|
+
a1.set({ firstName: "a1", address: null });
|
|
605
|
+
// Then we convert it to `{}` to potentially hold data
|
|
606
|
+
expect(a1.address.value).toEqual({});
|
|
607
|
+
// But we don't consider the address dirty
|
|
608
|
+
expect(a1.address.dirty).toBe(false);
|
|
609
|
+
expect(a1.changedValue).toEqual({ firstName: "a1" });
|
|
610
|
+
});
|
|
598
611
|
it("knows an object's field of type object is dirty", () => {
|
|
599
612
|
const a1 = createAuthorInputState({
|
|
600
613
|
books: [{ title: "b1", classification: formStateDomain_1.dd100 }],
|
|
@@ -1069,6 +1082,23 @@ describe("formState", () => {
|
|
|
1069
1082
|
// because this might trigger creating a throw-away entity
|
|
1070
1083
|
expect(formState.changedValue).toEqual({ firstName: "f" });
|
|
1071
1084
|
});
|
|
1085
|
+
it("changedValue includes initialized-and-unchanged list fields", () => {
|
|
1086
|
+
// Given a new author that is initialized with a new book
|
|
1087
|
+
const formState = (0, objectField_1.createObjectState)(authorWithBooksConfig, {
|
|
1088
|
+
books: [{ title: "t1" }],
|
|
1089
|
+
});
|
|
1090
|
+
// Then changedValue realizes that even though `books` is not changed, it should be included
|
|
1091
|
+
expect(formState.books.dirty).toBe(false);
|
|
1092
|
+
expect(formState.changedValue).toEqual({ books: [{ title: "t1" }] });
|
|
1093
|
+
});
|
|
1094
|
+
it("changedValue skips initialized-and-unchanged list fields that are empty", () => {
|
|
1095
|
+
// Given a new author that is initialized with no books
|
|
1096
|
+
const formState = (0, objectField_1.createObjectState)(authorWithBooksConfig, {
|
|
1097
|
+
books: [],
|
|
1098
|
+
});
|
|
1099
|
+
// Then changedValue doesn't include the books
|
|
1100
|
+
expect(formState.changedValue).toEqual({});
|
|
1101
|
+
});
|
|
1072
1102
|
it("changedValue includes new entity nested fields", () => {
|
|
1073
1103
|
// Given a new author with an address FK
|
|
1074
1104
|
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
|