@homebound/form-state 2.26.6 → 2.26.8

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.
@@ -1,4 +1,4 @@
1
1
  export interface FragmentField<V> {
2
2
  value: V;
3
3
  }
4
- export declare function newFragmentField<T, K extends keyof T>(parentInstance: T, key: K): FragmentField<T[K]>;
4
+ export declare function newFragmentField<T extends object, K extends keyof T & string>(parentInstance: T, key: K): FragmentField<T[K]>;
@@ -12,7 +12,33 @@ function newFragmentField(parentInstance, key) {
12
12
  let value = parentInstance[key];
13
13
  delete parentInstance[key];
14
14
  const obj = {
15
+ key,
16
+ _isIdKey: false,
17
+ _isDeleteKey: false,
18
+ _isReadOnlyKey: false,
19
+ touched: false,
20
+ valid: true,
21
+ readOnly: true,
22
+ required: false,
23
+ loading: false,
24
+ dirty: false,
25
+ focused: false,
26
+ originalValue: undefined,
27
+ changedValue: undefined,
28
+ errors: [],
29
+ rules: [],
30
+ isNewEntity: false,
31
+ focus: () => { },
32
+ blur: () => { },
33
+ maybeAutoSave: () => { },
34
+ commitChanges: () => { },
35
+ revertChanges: () => { },
15
36
  get value() {
37
+ // Watch for our parentInstance changing
38
+ if (key in parentInstance) {
39
+ value = parentInstance[key];
40
+ delete parentInstance[key];
41
+ }
16
42
  _tick.value > 0 || (0, utils_1.fail)();
17
43
  return value;
18
44
  },
@@ -20,6 +46,12 @@ function newFragmentField(parentInstance, key) {
20
46
  value = v;
21
47
  _tick.value++;
22
48
  },
49
+ set() {
50
+ throw new Error("FragmentField is read-only");
51
+ },
52
+ adapt() {
53
+ throw new Error("FragmentField is read-only");
54
+ },
23
55
  };
24
56
  return (0, mobx_1.makeAutoObservable)(obj, { value: false });
25
57
  }
@@ -129,7 +129,10 @@ function newObjectState(config, parentState, parentInstance, parentListState, in
129
129
  this._loading = loading;
130
130
  },
131
131
  get valid() {
132
- return getFields(this).every((f) => f.valid);
132
+ return getFields(this).every((f) => {
133
+ console.log(`Looking at ${f.key} valid=${f.valid}`);
134
+ return f.valid;
135
+ });
133
136
  },
134
137
  get errors() {
135
138
  return getFields(this).flatMap((f) => f.errors.map((e) => `${f.key}: ${e}`));
@@ -1671,6 +1671,10 @@ describe("formState", () => {
1671
1671
  }
1672
1672
  `);
1673
1673
  });
1674
+ it("are always considered valid", () => {
1675
+ const a = (0, objectField_1.createObjectState)({ data: { type: "fragment" } }, { data: (0, objectField_1.fragment)({ foo: "1" }) });
1676
+ expect(a.valid).toBe(true);
1677
+ });
1674
1678
  });
1675
1679
  it("sets id key correctly", () => {
1676
1680
  // Given an author
@@ -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.6",
3
+ "version": "2.26.8",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",