@homebound/form-state 2.26.2 → 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/objectField.js +13 -2
- package/dist/formState.test.js +13 -0
- package/package.json +1 -1
|
@@ -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);
|
|
@@ -207,7 +214,11 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
|
|
|
207
214
|
// look dirty, but if we're new we should include them anyway.
|
|
208
215
|
(this.isNewEntity && f._kind === "value" && f.value !== undefined) ||
|
|
209
216
|
// ...or they're non-empty sub-objects
|
|
210
|
-
(this.isNewEntity &&
|
|
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) ||
|
|
211
222
|
// ...or they're non-empty sub-lists
|
|
212
223
|
(this.isNewEntity && f._kind === "list" && ((_a = f.value) === null || _a === void 0 ? void 0 : _a.length) > 0)) {
|
|
213
224
|
result[f.key] = f.changedValue;
|
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 }],
|