@homebound/form-state 2.25.2 → 2.26.1

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> = {
@@ -50,7 +50,8 @@ function newListFieldState(parentCopy, parentInstance, parentState, key, rules,
50
50
  key: key,
51
51
  // Our fundamental state of wrapped Us
52
52
  get value() {
53
- return _tick.value > 0 && _childTick.value > 0 ? parentInstance[key] : (0, utils_1.fail)();
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
  },
55
56
  _focused: false,
56
57
  _readOnly: false,
@@ -9,6 +9,7 @@ export type InputAndMap<T, I> = {
9
9
  input: I;
10
10
  map?: (input: Exclude<I, null | undefined>) => T;
11
11
  ifUndefined?: T;
12
+ onlyOnce?: boolean;
12
13
  };
13
14
  export type QueryAndMap<T, I> = {
14
15
  query: Query<I>;
@@ -25,7 +25,13 @@ function useFormState(opts) {
25
25
  const [firstInitValue] = (0, react_1.useState)(() => (0, utils_1.initValue)(config, init));
26
26
  const isWrappingMobxProxy = !(0, is_plain_object_1.isPlainObject)(firstInitValue);
27
27
  // If they're using init.input, useMemo on it (and it might be an array), otherwise allow the identity of init be unstable
28
- const dep = (0, utils_1.isInput)(init) ? makeArray(init.input) : (0, utils_1.isQuery)(init) ? [init.query.data, init.query.loading] : [];
28
+ const dep = (0, utils_1.isInput)(init)
29
+ ? init.onlyOnce
30
+ ? []
31
+ : makeArray(init.input)
32
+ : (0, utils_1.isQuery)(init)
33
+ ? [init.query.data, init.query.loading]
34
+ : [];
29
35
  const form = (0, react_1.useMemo)(() => {
30
36
  function maybeAutoSave() {
31
37
  if (isAutoSaving === "in-flight") {
@@ -92,6 +92,23 @@ describe("useFormState", () => {
92
92
  const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
93
93
  expect(r.baseElement.textContent).toEqual("");
94
94
  });
95
+ it("uses init.onlyOnce to not react to identity changes", async () => {
96
+ const config = { firstName: { type: "value" } };
97
+ const TestComponent = (0, mobx_react_1.observer)(() => {
98
+ // And we have an `init` value that is dynamic, so we can observe whether init reruns
99
+ const renderCount = (0, react_2.useRef)(0).current++;
100
+ const form = (0, useFormState_1.useFormState)({
101
+ config,
102
+ init: { input: { firstName: `${renderCount}` }, onlyOnce: true },
103
+ });
104
+ const [, setTick] = (0, react_2.useState)(0);
105
+ return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("button", { "data-testid": "change", onClick: () => setTick(1) }), (0, jsx_runtime_1.jsx)("div", { "data-testid": "firstName", children: form.firstName.value })] }));
106
+ });
107
+ const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
108
+ expect(r.firstName).toHaveTextContent("0");
109
+ (0, rtl_utils_1.click)(r.change);
110
+ expect(r.firstName).toHaveTextContent("0");
111
+ });
95
112
  it("keeps local changed values when a query refreshes", async () => {
96
113
  // Given a component
97
114
  function TestComponent() {
@@ -319,6 +336,22 @@ describe("useFormState", () => {
319
336
  // Then we auto save again
320
337
  expect(autoSave).toBeCalledTimes(2);
321
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.jsx)("div", { "data-testid": "len", children: form.books.value.length });
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
+ });
322
355
  it("does not infinite loop when calling set inside of auto-save", async () => {
323
356
  // Given a component
324
357
  let autoSaves = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.25.2",
3
+ "version": "2.26.1",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",