@homebound/form-state 2.26.5 → 2.26.7

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.
@@ -173,7 +173,7 @@ function newListFieldState(parentCopy, parentInstance, parentState, key, rules,
173
173
  // passing us cloned rows from the parentCopy, but `getOrCreateChildState` will
174
174
  // use the `copyMap` to recover the non-cloned rows, to avoid promoting the clone
175
175
  // into a real row.
176
- parentInstance[key] = values.map((child) => getOrCreateChildState(child, opts).value);
176
+ parentInstance[key] = (values !== null && values !== void 0 ? values : []).map((child) => getOrCreateChildState(child, opts).value);
177
177
  // Make sure to tick first so that `setOriginalValue` sees the latest `rows`
178
178
  _tick.value++;
179
179
  // Reset originalCopy so that our dirty checks have the right # of rows.
@@ -152,6 +152,19 @@ describe("formState", () => {
152
152
  expect(state.books.rows[2].value === b1).toEqual(true);
153
153
  expect(ticks).toEqual(3);
154
154
  });
155
+ it("list value can refresh on undefined", () => {
156
+ // Given a list field that is currently []
157
+ const a1 = { firstName: "a1", books: [] };
158
+ const state = createAuthorInputState(a1);
159
+ // When we refresh it with undefined
160
+ state.set({ books: undefined }, { refreshing: true });
161
+ // Then it doesn't blow up, and stays []
162
+ expect(state.books.value).toEqual([]);
163
+ expect(state.value).toEqual({ firstName: "a1", books: [] });
164
+ // And it's not considered changed
165
+ expect(state.books.dirty).toBe(false);
166
+ expect(state.books.originalValue).toEqual([]);
167
+ });
155
168
  it("list value can observe changes", () => {
156
169
  const b1 = { title: "t1" };
157
170
  const a1 = { firstName: "a1", books: [b1] };
@@ -7,6 +7,7 @@ const mobx_1 = require("mobx");
7
7
  const mobx_react_1 = require("mobx-react");
8
8
  const react_2 = require("react");
9
9
  const FormStateApp_1 = require("./FormStateApp");
10
+ const objectField_1 = require("./fields/objectField");
10
11
  const rules_1 = require("./rules");
11
12
  const useFormState_1 = require("./useFormState");
12
13
  describe("useFormState", () => {
@@ -109,6 +110,25 @@ describe("useFormState", () => {
109
110
  (0, rtl_utils_1.click)(r.change);
110
111
  expect(r.firstName).toHaveTextContent("0");
111
112
  });
113
+ it("can init with fragments", async () => {
114
+ // Given a component
115
+ function TestComponent() {
116
+ var _a, _b, _c, _d;
117
+ // And we have query data that may or may not be defined
118
+ const form = (0, useFormState_1.useFormState)({
119
+ config: authorWithBookFragments,
120
+ init: {
121
+ input: {},
122
+ map: () => ({ firstName: "a1", books: [{ id: "b:1", data: (0, objectField_1.fragment)({ misc: "data" }) }] }),
123
+ },
124
+ });
125
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { "data-testid": "fieldValue", children: form.books.rows[0].data.value.misc }), (0, jsx_runtime_1.jsx)("div", { "data-testid": "rowValue", children: (_b = (_a = form.books.rows[0].value.data) === null || _a === void 0 ? void 0 : _a.misc) !== null && _b !== void 0 ? _b : "unset" }), (0, jsx_runtime_1.jsx)("div", { "data-testid": "listValue", children: (_d = (_c = form.books.value[0].data) === null || _c === void 0 ? void 0 : _c.misc) !== null && _d !== void 0 ? _d : "unset" })] }));
126
+ }
127
+ const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
128
+ expect(r.fieldValue).toHaveTextContent("data");
129
+ expect(r.rowValue).toHaveTextContent("unset");
130
+ expect(r.listValue).toHaveTextContent("unset");
131
+ });
112
132
  it("keeps local changed values when a query refreshes", async () => {
113
133
  // Given a component
114
134
  function TestComponent() {
@@ -567,7 +587,7 @@ describe("useFormState", () => {
567
587
  });
568
588
  return (0, jsx_runtime_1.jsx)("button", { "data-testid": "set", onClick: () => form.firstName.set("f2") });
569
589
  }
570
- const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
590
+ await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
571
591
  // When we change a field, and try to commit, it blows up
572
592
  // await expect(clickAndWait(r.set)).rejects.toThrow(
573
593
  // "When using autoSave, you should not manually call commitChanges",
@@ -620,6 +640,17 @@ const authorWithAddressAndBooksConfig = {
620
640
  },
621
641
  },
622
642
  };
643
+ const authorWithBookFragments = {
644
+ id: { type: "value" },
645
+ firstName: { type: "value" },
646
+ books: {
647
+ type: "list",
648
+ config: {
649
+ id: { type: "value" },
650
+ data: { type: "fragment" },
651
+ },
652
+ },
653
+ };
623
654
  /** Emulates a user focusing and then blurring a field. */
624
655
  function focusAndBlur(state) {
625
656
  state.focus();
package/dist/utils.js CHANGED
@@ -85,6 +85,9 @@ function pickFields(formConfig, instance) {
85
85
  else if (keyConfig.type === "value") {
86
86
  return [key, value];
87
87
  }
88
+ else if (keyConfig.type === "fragment") {
89
+ return [key, value];
90
+ }
88
91
  else {
89
92
  return assertNever(keyConfig);
90
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.26.5",
3
+ "version": "2.26.7",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",