@homebound/form-state 2.26.0 → 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/config.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export type ValueFieldConfig<V> = {
|
|
|
38
38
|
computed?: boolean;
|
|
39
39
|
/** Marks a field as being initially read-only, i.e. `field.readOnly = true/false` can change this default. */
|
|
40
40
|
readOnly?: boolean;
|
|
41
|
-
/** Marks an array field to be order agnostic when determining changed/dirty states */
|
|
41
|
+
/** Marks an array field to be order agnostic when determining changed/dirty states, defaults true. */
|
|
42
42
|
strictOrder?: false;
|
|
43
43
|
};
|
|
44
44
|
/** Field configuration for list values, i.e. `U` is `Book` in a form with `books: Book[]`. */
|
|
@@ -60,7 +60,7 @@ export type ListFieldConfig<U> = {
|
|
|
60
60
|
* default behavior.
|
|
61
61
|
*/
|
|
62
62
|
update?: "exhaustive" | "incremental";
|
|
63
|
-
/** Set to not consider the order of the list when evaluating changed/dirty states. */
|
|
63
|
+
/** Set to not consider the order of the list when evaluating changed/dirty states, defaults true. */
|
|
64
64
|
strictOrder?: false;
|
|
65
65
|
};
|
|
66
66
|
export type ObjectFieldConfig<U> = {
|
package/dist/fields/listField.js
CHANGED
|
@@ -50,8 +50,10 @@ function newListFieldState(parentCopy, parentInstance, parentState, key, rules,
|
|
|
50
50
|
key: key,
|
|
51
51
|
// Our fundamental state of wrapped Us
|
|
52
52
|
get value() {
|
|
53
|
-
|
|
53
|
+
var _a;
|
|
54
|
+
return _tick.value > 0 && _childTick.value > 0 ? ((_a = parentInstance[key]) !== null && _a !== void 0 ? _a : []) : (0, utils_1.fail)();
|
|
54
55
|
},
|
|
56
|
+
_kind: "list",
|
|
55
57
|
_focused: false,
|
|
56
58
|
_readOnly: false,
|
|
57
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, {
|
|
@@ -336,6 +336,23 @@ describe("useFormState", () => {
|
|
|
336
336
|
// Then we auto save again
|
|
337
337
|
expect(autoSave).toBeCalledTimes(2);
|
|
338
338
|
});
|
|
339
|
+
it("returns empty lists even when inputs are undefined", async () => {
|
|
340
|
+
const autoSave = jest.fn();
|
|
341
|
+
// Given a component
|
|
342
|
+
function TestComponent() {
|
|
343
|
+
// And there is no initial data (i.e. we're creating the author)
|
|
344
|
+
const form = (0, useFormState_1.useFormState)({
|
|
345
|
+
config: authorWithBooksConfig,
|
|
346
|
+
init: { input: undefined, map: (d) => d },
|
|
347
|
+
autoSave,
|
|
348
|
+
});
|
|
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
|
+
}
|
|
351
|
+
// Then we can still read it as empty
|
|
352
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
353
|
+
expect(r.len).toHaveTextContent("0");
|
|
354
|
+
expect(r.dirty).toHaveTextContent("false");
|
|
355
|
+
});
|
|
339
356
|
it("does not infinite loop when calling set inside of auto-save", async () => {
|
|
340
357
|
// Given a component
|
|
341
358
|
let autoSaves = 0;
|