@homebound/form-state 2.7.1 → 2.8.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.
@@ -23,19 +23,19 @@ declare type UseFormStatesOpts<T, I> = {
23
23
  */
24
24
  addRules?: (state: ObjectState<T>) => void;
25
25
  /**
26
- * Given an input to `getObjectState`, returns the identity value that we'll cache that value's form state on.
26
+ * Given an input to `getFormState`, returns the identity value that we'll cache that value's form state on.
27
27
  *
28
28
  * Does not need to be stable/useMemo'd.
29
29
  */
30
30
  getId: (v: I) => string;
31
31
  /**
32
- * Maps an input to `getObjectState` to the actual form shape `T`.
32
+ * Maps an input to `getFormState` to the actual form shape `T`.
33
33
  *
34
34
  * Does not need to be stable/useMemo'd.
35
35
  */
36
36
  map?: (input: Exclude<I, null | undefined>) => T;
37
37
  };
38
38
  export declare function useFormStates<T, I>(opts: UseFormStatesOpts<T, I>): {
39
- getObjectState: (input: I) => ObjectState<T>;
39
+ getFormState: (input: I) => ObjectState<T>;
40
40
  };
41
41
  export {};
@@ -33,7 +33,7 @@ function useFormStates(opts) {
33
33
  }
34
34
  }
35
35
  }
36
- const getObjectState = react_1.useCallback((input) => {
36
+ const getFormState = react_1.useCallback((input) => {
37
37
  const existing = objectStateCache[getId(input)];
38
38
  let form = existing === null || existing === void 0 ? void 0 : existing[0];
39
39
  // If it didn't exist, then add to the cache.
@@ -56,7 +56,7 @@ function useFormStates(opts) {
56
56
  // Allow the user to not stable-ize getId, map, addRules, and autoSave
57
57
  // eslint-disable-next-line react-hooks/exhaustive-deps
58
58
  [objectStateCache, config]);
59
- return { getObjectState };
59
+ return { getFormState };
60
60
  }
61
61
  exports.useFormStates = useFormStates;
62
62
  // If the user's autoSave hook makes some last-minute `.set` calls to sneak
@@ -14,12 +14,12 @@ describe("useFormStates", () => {
14
14
  }
15
15
  function TestComponent() {
16
16
  const config = { id: { type: "value" }, firstName: { type: "value" } };
17
- const { getObjectState } = useFormStates_1.useFormStates({
17
+ const { getFormState } = useFormStates_1.useFormStates({
18
18
  config,
19
19
  autoSave,
20
20
  getId: (o) => o.id,
21
21
  });
22
- return (jsx_runtime_1.jsx("div", { children: jsx_runtime_1.jsx(ChildComponent, { os: getObjectState({ id: "a:1", firstName: "Brandon" }) }, void 0) }, void 0));
22
+ return (jsx_runtime_1.jsx("div", { children: jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }, void 0) }, void 0));
23
23
  }
24
24
  const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}, void 0));
25
25
  // And the child component has defined state
@@ -28,13 +28,13 @@ describe("useFormStates", () => {
28
28
  it("can update existing object state from cache with new values", async () => {
29
29
  const autoSave = jest.fn();
30
30
  const config = { id: { type: "value" }, firstName: { type: "value" } };
31
- // Given a component using `getObjectState` for lazily creating ObjectStates
31
+ // Given a component using `getFormState` for lazily creating ObjectStates
32
32
  function TestComponent() {
33
33
  const [apiData, setApiData] = react_1.useState({ id: "a:1", firstName: "Brandon" });
34
- const { getObjectState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
34
+ const { getFormState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
35
35
  // Memoize an original for comparing the update against.
36
- const originalState = react_1.useMemo(() => getObjectState(apiData), []);
37
- const state = getObjectState(apiData);
36
+ const originalState = react_1.useMemo(() => getFormState(apiData), []);
37
+ const state = getFormState(apiData);
38
38
  return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("div", Object.assign({ "data-testid": "firstName" }, { children: state.firstName.value }), void 0),
39
39
  jsx_runtime_1.jsx("div", Object.assign({ "data-testid": "statesEqual" }, { children: JSON.stringify(state === originalState) }), void 0),
40
40
  jsx_runtime_1.jsx("button", { "data-testid": "updateApiData", onClick: () => setApiData((prevState) => ({ ...prevState, firstName: "Bob" })) }, void 0)] }, void 0));
@@ -57,13 +57,13 @@ describe("useFormStates", () => {
57
57
  firstName: { type: "value" },
58
58
  lastName: { type: "value" },
59
59
  };
60
- // Given a component using `getObjectState` for lazily creating ObjectStates
60
+ // Given a component using `getFormState` for lazily creating ObjectStates
61
61
  function TestComponent() {
62
62
  const [apiData, setApiData] = react_1.useState({ id: "a:1", firstName: "Tony", lastName: "Stark" });
63
63
  const [apiData2, setApiData2] = react_1.useState({ id: "a:2", firstName: "Steve", lastName: "Rogers" });
64
- const { getObjectState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
65
- const state = getObjectState(apiData);
66
- const state2 = getObjectState(apiData2);
64
+ const { getFormState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
65
+ const state = getFormState(apiData);
66
+ const state2 = getFormState(apiData2);
67
67
  async function autoSave(form) {
68
68
  autoSaveStub(form.changedValue);
69
69
  // Pretend to make an API call and update the local state
@@ -117,10 +117,10 @@ describe("useFormStates", () => {
117
117
  };
118
118
  function TestComponent() {
119
119
  const [config, setConfig] = react_1.useState(originalConfig);
120
- const { getObjectState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
120
+ const { getFormState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
121
121
  // Memoize an original for comparing the update against.
122
- const originalState = react_1.useMemo(() => getObjectState(apiData), []);
123
- const state = getObjectState(apiData);
122
+ const originalState = react_1.useMemo(() => getFormState(apiData), []);
123
+ const state = getFormState(apiData);
124
124
  return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("div", Object.assign({ "data-testid": "statesEqual" }, { children: JSON.stringify(state === originalState) }), void 0),
125
125
  jsx_runtime_1.jsx("button", { "data-testid": "updateConfig", onClick: () => setConfig(updatedConfig) }, void 0)] }, void 0));
126
126
  }
@@ -138,13 +138,13 @@ describe("useFormStates", () => {
138
138
  const addRules = jest.fn();
139
139
  function TestComponent() {
140
140
  const config = { id: { type: "value" }, firstName: { type: "value" } };
141
- const { getObjectState } = useFormStates_1.useFormStates({
141
+ const { getFormState } = useFormStates_1.useFormStates({
142
142
  config,
143
143
  addRules,
144
144
  getId: (o) => o.id,
145
145
  });
146
- return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx(ChildComponent, { os: getObjectState({ id: "a:1", firstName: "Brandon" }) }, void 0),
147
- jsx_runtime_1.jsx(ChildComponent, { os: getObjectState({ id: "a:1", firstName: "Brandon" }) }, void 0)] }, void 0));
146
+ return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }, void 0),
147
+ jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }, void 0)] }, void 0));
148
148
  }
149
149
  // When we render
150
150
  const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}, void 0));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.7.1",
3
+ "version": "2.8.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",