@homebound/form-state 2.25.2 → 2.26.0

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.
@@ -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() {
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.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",