@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.
- package/dist/useFormStates.d.ts +3 -3
- package/dist/useFormStates.js +2 -2
- package/dist/useFormStates.test.js +16 -16
- package/package.json +1 -1
package/dist/useFormStates.d.ts
CHANGED
|
@@ -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 `
|
|
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 `
|
|
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
|
-
|
|
39
|
+
getFormState: (input: I) => ObjectState<T>;
|
|
40
40
|
};
|
|
41
41
|
export {};
|
package/dist/useFormStates.js
CHANGED
|
@@ -33,7 +33,7 @@ function useFormStates(opts) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
const
|
|
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 {
|
|
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 {
|
|
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:
|
|
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 `
|
|
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 {
|
|
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(() =>
|
|
37
|
-
const state =
|
|
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 `
|
|
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 {
|
|
65
|
-
const state =
|
|
66
|
-
const state2 =
|
|
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 {
|
|
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(() =>
|
|
123
|
-
const state =
|
|
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 {
|
|
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:
|
|
147
|
-
jsx_runtime_1.jsx(ChildComponent, { os:
|
|
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));
|