@homebound/form-state 2.19.0 → 2.20.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/FormStateApp.d.ts +2 -3
- package/dist/FormStateApp.js +8 -47
- package/dist/FormStateApp.test.js +14 -14
- package/dist/config.d.ts +6 -6
- package/dist/fields/fragmentField.js +3 -3
- package/dist/fields/listField.js +12 -12
- package/dist/fields/objectField.d.ts +4 -4
- package/dist/fields/objectField.js +10 -10
- package/dist/fields/valueField.d.ts +13 -0
- package/dist/fields/valueField.js +48 -14
- package/dist/formState.stories.d.ts +2 -4
- package/dist/formState.stories.js +1 -1
- package/dist/formState.test.js +146 -79
- package/dist/formStateDomain.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/proxies.d.ts +5 -0
- package/dist/proxies.js +21 -0
- package/dist/rules.d.ts +1 -1
- package/dist/rules.js +1 -1
- package/dist/setupTests.js +2 -2
- package/dist/useFormState.d.ts +4 -4
- package/dist/useFormState.js +13 -13
- package/dist/useFormState.test.js +141 -169
- package/dist/useFormStates.d.ts +3 -3
- package/dist/useFormStates.js +7 -7
- package/dist/useFormStates.test.js +56 -63
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js +8 -8
- package/dist/utils.test.js +49 -49
- package/package.json +44 -40
package/dist/useFormStates.js
CHANGED
|
@@ -6,19 +6,19 @@ const objectField_1 = require("./fields/objectField");
|
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
7
|
function useFormStates(opts) {
|
|
8
8
|
const { config, autoSave, getId, map, addRules, readOnly = false } = opts;
|
|
9
|
-
const objectStateCache = react_1.useMemo(() => ({}),
|
|
9
|
+
const objectStateCache = (0, react_1.useMemo)(() => ({}),
|
|
10
10
|
// Force resetting the cache if config changes
|
|
11
11
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
12
12
|
[config]);
|
|
13
13
|
// Keep track of ObjectStates that triggered auto-save when a save was already in progress.
|
|
14
|
-
const pendingAutoSaves = react_1.useRef(new Set());
|
|
14
|
+
const pendingAutoSaves = (0, react_1.useRef)(new Set());
|
|
15
15
|
// Use a ref so our memo'ized `autoSave` always see the latest value
|
|
16
|
-
const autoSaveRef = react_1.useRef(autoSave);
|
|
16
|
+
const autoSaveRef = (0, react_1.useRef)(autoSave);
|
|
17
17
|
autoSaveRef.current = autoSave;
|
|
18
18
|
// Use a ref b/c we're memod
|
|
19
|
-
const readOnlyRef = react_1.useRef(readOnly);
|
|
19
|
+
const readOnlyRef = (0, react_1.useRef)(readOnly);
|
|
20
20
|
readOnlyRef.current = readOnly;
|
|
21
|
-
const getFormState = react_1.useCallback((input, opts = {}) => {
|
|
21
|
+
const getFormState = (0, react_1.useCallback)((input, opts = {}) => {
|
|
22
22
|
const existing = objectStateCache[getId(input)];
|
|
23
23
|
let form = existing === null || existing === void 0 ? void 0 : existing[0];
|
|
24
24
|
async function maybeAutoSave(form) {
|
|
@@ -54,7 +54,7 @@ function useFormStates(opts) {
|
|
|
54
54
|
}
|
|
55
55
|
// If it didn't exist, then add to the cache.
|
|
56
56
|
if (!form) {
|
|
57
|
-
form = objectField_1.createObjectState(config, utils_1.initValue(config, map ? { map, input } : input), {
|
|
57
|
+
form = (0, objectField_1.createObjectState)(config, (0, utils_1.initValue)(config, map ? { map, input } : input), {
|
|
58
58
|
maybeAutoSave: () => maybeAutoSave(form),
|
|
59
59
|
});
|
|
60
60
|
if (addRules) {
|
|
@@ -64,7 +64,7 @@ function useFormStates(opts) {
|
|
|
64
64
|
}
|
|
65
65
|
// If the source of truth changed, then update the existing state and return it.
|
|
66
66
|
if (existing && existing[1] !== input) {
|
|
67
|
-
form.set(utils_1.initValue(config, map ? { map, input } : input), { refreshing: true });
|
|
67
|
+
form.set((0, utils_1.initValue)(config, map ? { map, input } : input), { refreshing: true });
|
|
68
68
|
existing[1] = input;
|
|
69
69
|
}
|
|
70
70
|
form.readOnly = readOnlyRef.current || !!opts.readOnly;
|
|
@@ -12,45 +12,43 @@ describe("useFormStates", () => {
|
|
|
12
12
|
const autoSave = jest.fn();
|
|
13
13
|
// Given a parent and child component, where the formState is created only for the child component.
|
|
14
14
|
function ChildComponent({ os }) {
|
|
15
|
-
return jsx_runtime_1.jsx("div",
|
|
15
|
+
return (0, jsx_runtime_1.jsx)("div", { "data-testid": "firstName", children: os.firstName.value });
|
|
16
16
|
}
|
|
17
17
|
function TestComponent() {
|
|
18
18
|
const config = { id: { type: "value" }, firstName: { type: "value" } };
|
|
19
|
-
const { getFormState } = useFormStates_1.useFormStates({
|
|
19
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({
|
|
20
20
|
config,
|
|
21
21
|
autoSave,
|
|
22
22
|
getId: (o) => o.id,
|
|
23
23
|
});
|
|
24
|
-
return (jsx_runtime_1.jsx("div", { children: jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }
|
|
24
|
+
return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }) }));
|
|
25
25
|
}
|
|
26
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}
|
|
26
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
27
27
|
// And the child component has defined state
|
|
28
|
-
expect(r.firstName
|
|
28
|
+
expect(r.firstName).toHaveTextContent("Brandon");
|
|
29
29
|
});
|
|
30
30
|
it("can update existing object state from cache with new values", async () => {
|
|
31
31
|
const autoSave = jest.fn();
|
|
32
32
|
const config = { id: { type: "value" }, firstName: { type: "value" } };
|
|
33
33
|
// Given a component using `getFormState` for lazily creating ObjectStates
|
|
34
34
|
function TestComponent() {
|
|
35
|
-
const [apiData, setApiData] = react_1.useState({ id: "a:1", firstName: "Brandon" });
|
|
36
|
-
const { getFormState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
|
|
35
|
+
const [apiData, setApiData] = (0, react_1.useState)({ id: "a:1", firstName: "Brandon" });
|
|
36
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({ config, autoSave, getId: (o) => o.id });
|
|
37
37
|
// Memoize an original for comparing the update against.
|
|
38
|
-
const originalState = react_1.useMemo(() => getFormState(apiData), [getFormState]);
|
|
38
|
+
const originalState = (0, react_1.useMemo)(() => getFormState(apiData), [getFormState]);
|
|
39
39
|
const state = getFormState(apiData);
|
|
40
|
-
return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("div",
|
|
41
|
-
jsx_runtime_1.jsx("div", Object.assign({ "data-testid": "statesEqual" }, { children: JSON.stringify(state === originalState) }), void 0),
|
|
42
|
-
jsx_runtime_1.jsx("button", { "data-testid": "updateApiData", onClick: () => setApiData((prevState) => ({ ...prevState, firstName: "Bob" })) }, void 0)] }, void 0));
|
|
40
|
+
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("div", { "data-testid": "firstName", children: state.firstName.value }), (0, jsx_runtime_1.jsx)("div", { "data-testid": "statesEqual", children: JSON.stringify(state === originalState) }), (0, jsx_runtime_1.jsx)("button", { "data-testid": "updateApiData", onClick: () => setApiData((prevState) => ({ ...prevState, firstName: "Bob" })) })] }));
|
|
43
41
|
}
|
|
44
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}
|
|
42
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
45
43
|
// And the initial values for the form state display
|
|
46
|
-
expect(r.firstName
|
|
47
|
-
expect(r.statesEqual
|
|
44
|
+
expect(r.firstName).toHaveTextContent("Brandon");
|
|
45
|
+
expect(r.statesEqual).toHaveTextContent("true");
|
|
48
46
|
// When updating the API data
|
|
49
|
-
await rtl_utils_1.clickAndWait(r.updateApiData
|
|
47
|
+
await (0, rtl_utils_1.clickAndWait)(r.updateApiData);
|
|
50
48
|
// Then the new value is shown in the component
|
|
51
|
-
expect(r.firstName
|
|
49
|
+
expect(r.firstName).toHaveTextContent("Bob");
|
|
52
50
|
// And the two states are using the same reference
|
|
53
|
-
expect(r.statesEqual
|
|
51
|
+
expect(r.statesEqual).toHaveTextContent("true");
|
|
54
52
|
});
|
|
55
53
|
it("can queue up changes for auto save if a save is already in progress - works across multiple states", async () => {
|
|
56
54
|
const autoSaveStub = jest.fn();
|
|
@@ -61,9 +59,9 @@ describe("useFormStates", () => {
|
|
|
61
59
|
};
|
|
62
60
|
// Given a component using `getFormState` for lazily creating ObjectStates
|
|
63
61
|
function TestComponent() {
|
|
64
|
-
const [apiData, setApiData] = react_1.useState({ id: "a:1", firstName: "Tony", lastName: "Stark" });
|
|
65
|
-
const [apiData2, setApiData2] = react_1.useState({ id: "a:2", firstName: "Steve", lastName: "Rogers" });
|
|
66
|
-
const { getFormState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
|
|
62
|
+
const [apiData, setApiData] = (0, react_1.useState)({ id: "a:1", firstName: "Tony", lastName: "Stark" });
|
|
63
|
+
const [apiData2, setApiData2] = (0, react_1.useState)({ id: "a:2", firstName: "Steve", lastName: "Rogers" });
|
|
64
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({ config, autoSave, getId: (o) => o.id });
|
|
67
65
|
const state = getFormState(apiData);
|
|
68
66
|
const state2 = getFormState(apiData2);
|
|
69
67
|
async function autoSave(form) {
|
|
@@ -77,30 +75,27 @@ describe("useFormStates", () => {
|
|
|
77
75
|
}
|
|
78
76
|
await Promise.resolve(1);
|
|
79
77
|
}
|
|
80
|
-
return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("div",
|
|
81
|
-
jsx_runtime_1.jsx("button", { "data-testid": "focusSetAndSaveField", onClick: () => {
|
|
78
|
+
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("div", { "data-testid": "firstName", children: state.firstName.value }), (0, jsx_runtime_1.jsx)("button", { "data-testid": "focusSetAndSaveField", onClick: () => {
|
|
82
79
|
state.firstName.focus();
|
|
83
80
|
state.firstName.set("Foo");
|
|
84
81
|
state.firstName.maybeAutoSave();
|
|
85
|
-
} },
|
|
86
|
-
jsx_runtime_1.jsx("button", { "data-testid": "focusSetAndSaveFieldLastName", onClick: () => {
|
|
82
|
+
} }), (0, jsx_runtime_1.jsx)("button", { "data-testid": "focusSetAndSaveFieldLastName", onClick: () => {
|
|
87
83
|
state.lastName.focus();
|
|
88
84
|
state.lastName.set("Bar");
|
|
89
85
|
state.lastName.maybeAutoSave();
|
|
90
|
-
} },
|
|
91
|
-
jsx_runtime_1.jsx("button", { "data-testid": "focusSetAndSaveField2", onClick: () => {
|
|
86
|
+
} }), (0, jsx_runtime_1.jsx)("button", { "data-testid": "focusSetAndSaveField2", onClick: () => {
|
|
92
87
|
state2.lastName.focus();
|
|
93
88
|
state2.lastName.set("Bar");
|
|
94
89
|
state2.lastName.maybeAutoSave();
|
|
95
|
-
} }
|
|
90
|
+
} })] }));
|
|
96
91
|
}
|
|
97
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}
|
|
92
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
98
93
|
// And triggering the auto save behavior before awaiting the initial promise to resolve so we have pending changes.
|
|
99
|
-
rtl_utils_1.click(r.focusSetAndSaveField
|
|
100
|
-
rtl_utils_1.click(r.focusSetAndSaveFieldLastName
|
|
101
|
-
rtl_utils_1.click(r.focusSetAndSaveField2
|
|
94
|
+
(0, rtl_utils_1.click)(r.focusSetAndSaveField);
|
|
95
|
+
(0, rtl_utils_1.click)(r.focusSetAndSaveFieldLastName);
|
|
96
|
+
(0, rtl_utils_1.click)(r.focusSetAndSaveField2);
|
|
102
97
|
// Awaits the promises for all methods triggered above
|
|
103
|
-
await rtl_utils_1.wait();
|
|
98
|
+
await (0, rtl_utils_1.wait)();
|
|
104
99
|
// Then expect the auto save to only have been called two times. Once with each set of changedValues.
|
|
105
100
|
expect(autoSaveStub).toBeCalledTimes(2);
|
|
106
101
|
expect(autoSaveStub).toBeCalledWith({ id: "a:1", firstName: "Foo", lastName: "Bar" });
|
|
@@ -117,39 +112,37 @@ describe("useFormStates", () => {
|
|
|
117
112
|
firstName: { type: "value", rules: [rules_1.required] },
|
|
118
113
|
};
|
|
119
114
|
function TestComponent() {
|
|
120
|
-
const [config, setConfig] = react_1.useState(originalConfig);
|
|
121
|
-
const { getFormState } = useFormStates_1.useFormStates({ config, autoSave, getId: (o) => o.id });
|
|
115
|
+
const [config, setConfig] = (0, react_1.useState)(originalConfig);
|
|
116
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({ config, autoSave, getId: (o) => o.id });
|
|
122
117
|
// Memoize an original for comparing the update against.
|
|
123
118
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
124
|
-
const originalState = react_1.useMemo(() => getFormState(apiData), []);
|
|
119
|
+
const originalState = (0, react_1.useMemo)(() => getFormState(apiData), []);
|
|
125
120
|
const state = getFormState(apiData);
|
|
126
|
-
return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("div",
|
|
127
|
-
jsx_runtime_1.jsx("button", { "data-testid": "updateConfig", onClick: () => setConfig(updatedConfig) }, void 0)] }, void 0));
|
|
121
|
+
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("div", { "data-testid": "statesEqual", children: JSON.stringify(state === originalState) }), (0, jsx_runtime_1.jsx)("button", { "data-testid": "updateConfig", onClick: () => setConfig(updatedConfig) })] }));
|
|
128
122
|
}
|
|
129
123
|
// When rendered with the original configuration
|
|
130
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}
|
|
124
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
131
125
|
// Then the two form-states generated are equal
|
|
132
|
-
expect(r.statesEqual
|
|
126
|
+
expect(r.statesEqual).toHaveTextContent("true");
|
|
133
127
|
// When updating the configuration object
|
|
134
|
-
rtl_utils_1.click(r.updateConfig);
|
|
128
|
+
(0, rtl_utils_1.click)(r.updateConfig);
|
|
135
129
|
// Then the two form-states are no longer equal.
|
|
136
|
-
expect(r.statesEqual
|
|
130
|
+
expect(r.statesEqual).toHaveTextContent("false");
|
|
137
131
|
});
|
|
138
132
|
it("calls addRules once per form state", async () => {
|
|
139
133
|
// Given a user wants to use addRules
|
|
140
134
|
const addRules = jest.fn();
|
|
141
135
|
function TestComponent() {
|
|
142
136
|
const config = { id: { type: "value" }, firstName: { type: "value" } };
|
|
143
|
-
const { getFormState } = useFormStates_1.useFormStates({
|
|
137
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({
|
|
144
138
|
config,
|
|
145
139
|
addRules,
|
|
146
140
|
getId: (o) => o.id,
|
|
147
141
|
});
|
|
148
|
-
return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) },
|
|
149
|
-
jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }, void 0)] }, void 0));
|
|
142
|
+
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }), (0, jsx_runtime_1.jsx)(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) })] }));
|
|
150
143
|
}
|
|
151
144
|
// When we render
|
|
152
|
-
await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}
|
|
145
|
+
await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
153
146
|
// Then addRules was only called once
|
|
154
147
|
expect(addRules).toHaveBeenCalledTimes(1);
|
|
155
148
|
});
|
|
@@ -157,12 +150,12 @@ describe("useFormStates", () => {
|
|
|
157
150
|
// Given a user wants to use auto save
|
|
158
151
|
const autoSave = jest.fn();
|
|
159
152
|
function TestComponent() {
|
|
160
|
-
const { getFormState } = useFormStates_1.useFormStates({
|
|
153
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({
|
|
161
154
|
config,
|
|
162
155
|
getId: (o) => o.id,
|
|
163
156
|
addRules(fs) {
|
|
164
157
|
// And they have a reactive true that calculates last name
|
|
165
|
-
mobx_1.reaction(() => fs.firstName.value, (curr) => {
|
|
158
|
+
(0, mobx_1.reaction)(() => fs.firstName.value, (curr) => {
|
|
166
159
|
fs.lastName.set(curr);
|
|
167
160
|
});
|
|
168
161
|
},
|
|
@@ -170,12 +163,12 @@ describe("useFormStates", () => {
|
|
|
170
163
|
autoSave(fs.changedValue);
|
|
171
164
|
},
|
|
172
165
|
});
|
|
173
|
-
return jsx_runtime_1.jsx(FormStateApp_1.TextField, { field: getFormState({ id: "a:1", firstName: "Brandon" }).firstName }
|
|
166
|
+
return (0, jsx_runtime_1.jsx)(FormStateApp_1.TextField, { field: getFormState({ id: "a:1", firstName: "Brandon" }).firstName });
|
|
174
167
|
}
|
|
175
168
|
// When we render
|
|
176
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}
|
|
169
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, {}));
|
|
177
170
|
// And update the firstName
|
|
178
|
-
await rtl_utils_1.typeAndWait(r.firstName
|
|
171
|
+
await (0, rtl_utils_1.typeAndWait)(r.firstName, "first");
|
|
179
172
|
// Then autoSave was called once with both input+calc'd values
|
|
180
173
|
expect(autoSave).toHaveBeenCalledTimes(1);
|
|
181
174
|
expect(autoSave).toHaveBeenCalledWith({ id: "a:1", firstName: "first", lastName: "first" });
|
|
@@ -183,41 +176,41 @@ describe("useFormStates", () => {
|
|
|
183
176
|
it("can set readOnly via the hook opt", async () => {
|
|
184
177
|
// Given a test component
|
|
185
178
|
function TestComponent({ readOnly }) {
|
|
186
|
-
const { getFormState } = useFormStates_1.useFormStates({
|
|
179
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({
|
|
187
180
|
config,
|
|
188
181
|
getId: (o) => o.id,
|
|
189
182
|
// And it passes readOnly directly to useFormStates
|
|
190
183
|
readOnly,
|
|
191
184
|
});
|
|
192
|
-
return jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) }
|
|
185
|
+
return (0, jsx_runtime_1.jsx)(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }) });
|
|
193
186
|
}
|
|
194
187
|
// When we render
|
|
195
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, { readOnly: true }
|
|
188
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, { readOnly: true }));
|
|
196
189
|
// Then it's read only
|
|
197
|
-
expect(r.firstName
|
|
190
|
+
expect(r.firstName).toHaveAttribute("data-readonly", "true");
|
|
198
191
|
// And when we rerender
|
|
199
|
-
await r.rerender(jsx_runtime_1.jsx(TestComponent, { readOnly: false }
|
|
192
|
+
await r.rerender((0, jsx_runtime_1.jsx)(TestComponent, { readOnly: false }));
|
|
200
193
|
// Then it's not read only
|
|
201
|
-
expect(r.firstName
|
|
194
|
+
expect(r.firstName).toHaveAttribute("data-readonly", "false");
|
|
202
195
|
});
|
|
203
196
|
it("can set readOnly via the getFormState function", async () => {
|
|
204
197
|
// Given a test component
|
|
205
198
|
function TestComponent({ readOnly }) {
|
|
206
|
-
const { getFormState } = useFormStates_1.useFormStates({
|
|
199
|
+
const { getFormState } = (0, useFormStates_1.useFormStates)({
|
|
207
200
|
config,
|
|
208
201
|
getId: (o) => o.id,
|
|
209
202
|
});
|
|
210
203
|
// And it passes readOnly directly to getFormState
|
|
211
|
-
return jsx_runtime_1.jsx(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }, { readOnly }) }
|
|
204
|
+
return (0, jsx_runtime_1.jsx)(ChildComponent, { os: getFormState({ id: "a:1", firstName: "Brandon" }, { readOnly }) });
|
|
212
205
|
}
|
|
213
206
|
// When we render
|
|
214
|
-
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, { readOnly: true }
|
|
207
|
+
const r = await (0, rtl_utils_1.render)((0, jsx_runtime_1.jsx)(TestComponent, { readOnly: true }));
|
|
215
208
|
// Then it's read only
|
|
216
|
-
expect(r.firstName
|
|
209
|
+
expect(r.firstName).toHaveAttribute("data-readonly", "true");
|
|
217
210
|
// And when we rerender
|
|
218
|
-
await r.rerender(jsx_runtime_1.jsx(TestComponent, { readOnly: false }
|
|
211
|
+
await r.rerender((0, jsx_runtime_1.jsx)(TestComponent, { readOnly: false }));
|
|
219
212
|
// Then it's not read only
|
|
220
|
-
expect(r.firstName
|
|
213
|
+
expect(r.firstName).toHaveAttribute("data-readonly", "false");
|
|
221
214
|
});
|
|
222
215
|
});
|
|
223
216
|
const config = {
|
|
@@ -226,5 +219,5 @@ const config = {
|
|
|
226
219
|
lastName: { type: "value" },
|
|
227
220
|
};
|
|
228
221
|
function ChildComponent({ os }) {
|
|
229
|
-
return (jsx_runtime_1.jsx("div",
|
|
222
|
+
return ((0, jsx_runtime_1.jsx)("div", { "data-testid": "firstName", "data-readonly": os.firstName.readOnly, children: os.firstName.value }));
|
|
230
223
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ObjectConfig } from "./config";
|
|
2
2
|
import { InputAndMap, QueryAndMap, UseFormStateOpts } from "./useFormState";
|
|
3
|
-
export
|
|
4
|
-
export
|
|
3
|
+
export type Builtin = Date | Function | Uint8Array | string | number | boolean;
|
|
4
|
+
export type Primitive = undefined | null | boolean | string | number | Function | Date | {
|
|
5
5
|
toJSON(): any;
|
|
6
6
|
};
|
|
7
7
|
/** Makes the keys in `T` required while keeping the values undefined. */
|
|
8
|
-
export
|
|
8
|
+
export type DeepRequired<T> = T extends Primitive ? T : {
|
|
9
9
|
[P in keyof Required<T>]: T[P] extends Array<infer U> ? Array<DeepRequired<U>> : T[P] extends ReadonlyArray<infer U2> ? ReadonlyArray<DeepRequired<U2>> : DeepRequired<T[P]>;
|
|
10
10
|
};
|
|
11
11
|
export declare function fail(message?: string): never;
|
package/dist/utils.js
CHANGED
|
@@ -31,11 +31,11 @@ function initValue(config, init) {
|
|
|
31
31
|
}
|
|
32
32
|
exports.initValue = initValue;
|
|
33
33
|
function isInput(init) {
|
|
34
|
-
return !!init && "input" in init && "map" in init;
|
|
34
|
+
return !!init && typeof init === "object" && "input" in init && "map" in init;
|
|
35
35
|
}
|
|
36
36
|
exports.isInput = isInput;
|
|
37
37
|
function isQuery(init) {
|
|
38
|
-
return !!init && "query" in init && "map" in init;
|
|
38
|
+
return !!init && typeof init === "object" && "query" in init && "map" in init;
|
|
39
39
|
}
|
|
40
40
|
exports.isQuery = isQuery;
|
|
41
41
|
/**
|
|
@@ -49,7 +49,7 @@ exports.isQuery = isQuery;
|
|
|
49
49
|
*/
|
|
50
50
|
function pickFields(formConfig, instance) {
|
|
51
51
|
// If the caller is using classes, i.e. with their own custom observable behavior, then just use as-is
|
|
52
|
-
if (!is_plain_object_1.isPlainObject(instance)) {
|
|
52
|
+
if (!(0, is_plain_object_1.isPlainObject)(instance)) {
|
|
53
53
|
return instance;
|
|
54
54
|
}
|
|
55
55
|
return Object.fromEntries(Object.entries(formConfig).map(([key, _keyConfig]) => {
|
|
@@ -64,7 +64,7 @@ function pickFields(formConfig, instance) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
else if (keyConfig.type === "list") {
|
|
67
|
-
if (mobx_1.isObservable(value)) {
|
|
67
|
+
if ((0, mobx_1.isObservable)(value)) {
|
|
68
68
|
// If we hit an observable array, leave it as the existing proxy so the our
|
|
69
69
|
// ListFieldState will react to changes in the original array.
|
|
70
70
|
return [key, value];
|
|
@@ -103,17 +103,17 @@ exports.isEmpty = isEmpty;
|
|
|
103
103
|
* - arrays
|
|
104
104
|
*/
|
|
105
105
|
function areEqual(a, b, strictOrder) {
|
|
106
|
-
if (is_plain_object_1.isPlainObject(a)) {
|
|
107
|
-
return fast_deep_equal_1.default(mobx_1.toJS(a), mobx_1.toJS(b));
|
|
106
|
+
if ((0, is_plain_object_1.isPlainObject)(a)) {
|
|
107
|
+
return (0, fast_deep_equal_1.default)((0, mobx_1.toJS)(a), (0, mobx_1.toJS)(b));
|
|
108
108
|
}
|
|
109
109
|
if (hasToJSON(a) || hasToJSON(b)) {
|
|
110
110
|
const a1 = hasToJSON(a) ? a.toJSON() : a;
|
|
111
111
|
const b1 = hasToJSON(b) ? b.toJSON() : b;
|
|
112
|
-
return fast_deep_equal_1.default(a1, b1);
|
|
112
|
+
return (0, fast_deep_equal_1.default)(a1, b1);
|
|
113
113
|
}
|
|
114
114
|
if (a && b && a instanceof Array && b instanceof Array) {
|
|
115
115
|
if (strictOrder !== false) {
|
|
116
|
-
return fast_deep_equal_1.default(a, b);
|
|
116
|
+
return (0, fast_deep_equal_1.default)(a, b);
|
|
117
117
|
}
|
|
118
118
|
if (a.length !== b.length)
|
|
119
119
|
return false;
|
package/dist/utils.test.js
CHANGED
|
@@ -6,72 +6,72 @@ const utils_1 = require("./utils");
|
|
|
6
6
|
describe("utils", () => {
|
|
7
7
|
describe("pickFields", () => {
|
|
8
8
|
it("can pick a value field", () => {
|
|
9
|
-
const a = utils_1.pickFields(
|
|
9
|
+
const a = (0, utils_1.pickFields)(
|
|
10
10
|
//
|
|
11
11
|
{ firstName: { type: "value" } }, { firstName: "a", b: "ignored" });
|
|
12
12
|
expect(a).toMatchInlineSnapshot(`
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
{
|
|
14
|
+
"firstName": "a",
|
|
15
|
+
}
|
|
16
|
+
`);
|
|
17
17
|
});
|
|
18
18
|
it("can pick an unset object fields", () => {
|
|
19
|
-
const a = utils_1.pickFields(authorWithAddressConfig, { firstName: "a", b: "ignored" });
|
|
19
|
+
const a = (0, utils_1.pickFields)(authorWithAddressConfig, { firstName: "a", b: "ignored" });
|
|
20
20
|
expect(a).toMatchInlineSnapshot(`
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
{
|
|
22
|
+
"address": undefined,
|
|
23
|
+
"firstName": "a",
|
|
24
|
+
"id": undefined,
|
|
25
|
+
"lastName": undefined,
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
28
|
});
|
|
29
29
|
it("can pick a set object field", () => {
|
|
30
|
-
const a = utils_1.pickFields(authorWithAddressConfig, { firstName: "a", b: "ignored", address: {} });
|
|
30
|
+
const a = (0, utils_1.pickFields)(authorWithAddressConfig, { firstName: "a", b: "ignored", address: {} });
|
|
31
31
|
expect(a).toMatchInlineSnapshot(`
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
{
|
|
33
|
+
"address": {
|
|
34
|
+
"city": undefined,
|
|
35
|
+
"street": undefined,
|
|
36
|
+
},
|
|
37
|
+
"firstName": "a",
|
|
38
|
+
"id": undefined,
|
|
39
|
+
"lastName": undefined,
|
|
40
|
+
}
|
|
41
|
+
`);
|
|
42
42
|
});
|
|
43
43
|
it("can pick an unset list field", () => {
|
|
44
|
-
const a = utils_1.pickFields(authorWithBooksConfig, { firstName: "a", b: "ignored" });
|
|
44
|
+
const a = (0, utils_1.pickFields)(authorWithBooksConfig, { firstName: "a", b: "ignored" });
|
|
45
45
|
expect(a).toMatchInlineSnapshot(`
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
{
|
|
47
|
+
"books": undefined,
|
|
48
|
+
"firstName": "a",
|
|
49
|
+
"id": undefined,
|
|
50
|
+
"lastName": undefined,
|
|
51
|
+
}
|
|
52
|
+
`);
|
|
53
53
|
});
|
|
54
54
|
it("can pick a set list field", () => {
|
|
55
|
-
const a = utils_1.pickFields(authorWithBooksConfig, { firstName: "a", b: "ignored", books: [{}] });
|
|
55
|
+
const a = (0, utils_1.pickFields)(authorWithBooksConfig, { firstName: "a", b: "ignored", books: [{}] });
|
|
56
56
|
expect(a).toMatchInlineSnapshot(`
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
57
|
+
{
|
|
58
|
+
"books": [
|
|
59
|
+
{
|
|
60
|
+
"classification": undefined,
|
|
61
|
+
"id": undefined,
|
|
62
|
+
"title": undefined,
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
"firstName": "a",
|
|
66
|
+
"id": undefined,
|
|
67
|
+
"lastName": undefined,
|
|
68
|
+
}
|
|
69
|
+
`);
|
|
70
70
|
});
|
|
71
71
|
it("can pick a set observable list field", () => {
|
|
72
|
-
const books = mobx_1.observable([]);
|
|
73
|
-
const a = utils_1.pickFields(authorWithBooksConfig, { firstName: "a", b: "ignored", books });
|
|
74
|
-
expect(mobx_1.isObservable(a.books)).toEqual(true);
|
|
72
|
+
const books = (0, mobx_1.observable)([]);
|
|
73
|
+
const a = (0, utils_1.pickFields)(authorWithBooksConfig, { firstName: "a", b: "ignored", books });
|
|
74
|
+
expect((0, mobx_1.isObservable)(a.books)).toEqual(true);
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@homebound/form-state",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.20.1",
|
|
4
4
|
"author": "Homebound",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "
|
|
17
|
-
"build:storybook": "build
|
|
16
|
+
"build": "tspc",
|
|
17
|
+
"build:storybook": "storybook build",
|
|
18
18
|
"test": "jest",
|
|
19
19
|
"lint": "eslint . --cache --fix --ext .js,.ts,.tsx",
|
|
20
|
-
"storybook": "
|
|
20
|
+
"storybook": "storybook dev -p 9000",
|
|
21
21
|
"format": "prettier --loglevel warn --write \"**/*.{ts,tsx,css,md}\"",
|
|
22
22
|
"semantic-release": "semantic-release"
|
|
23
23
|
},
|
|
@@ -31,31 +31,32 @@
|
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"fast-deep-equal": "^3.1.3",
|
|
34
|
-
"is-plain-object": "^5.0.0"
|
|
35
|
-
"mobx": "^6.3.2",
|
|
36
|
-
"mobx-react": "^7.2.0"
|
|
34
|
+
"is-plain-object": "^5.0.0"
|
|
37
35
|
},
|
|
38
36
|
"peerDependencies": {
|
|
37
|
+
"mobx": ">=6",
|
|
39
38
|
"react": ">=16"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
41
|
"@babel/core": "^7.13.1",
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
46
|
-
"@
|
|
47
|
-
"@
|
|
48
|
-
"@
|
|
49
|
-
"@storybook/
|
|
50
|
-
"@storybook/
|
|
51
|
-
"@
|
|
52
|
-
"@
|
|
53
|
-
"@
|
|
54
|
-
"@testing-library/
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"@types/
|
|
42
|
+
"@babel/preset-env": "^7.22.15",
|
|
43
|
+
"@babel/preset-react": "^7.22.15",
|
|
44
|
+
"@babel/preset-typescript": "^7.22.15",
|
|
45
|
+
"@homebound/rtl-utils": "2.65.0",
|
|
46
|
+
"@semantic-release/exec": "^6.0.3",
|
|
47
|
+
"@semantic-release/git": "^10.0.1",
|
|
48
|
+
"@storybook/addon-essentials": "^7.4.0",
|
|
49
|
+
"@storybook/addon-links": "^7.4.0",
|
|
50
|
+
"@storybook/addons": "^7.4.0",
|
|
51
|
+
"@storybook/react": "^7.4.0",
|
|
52
|
+
"@storybook/react-webpack5": "^7.4.0",
|
|
53
|
+
"@testing-library/dom": "^9.3.1",
|
|
54
|
+
"@testing-library/jest-dom": "^6.1.3",
|
|
55
|
+
"@testing-library/react": "^14.0.0",
|
|
56
|
+
"@tsconfig/recommended": "^1.0.2",
|
|
57
|
+
"@types/jest": "^29.5.4",
|
|
58
|
+
"@types/react": "^18.2.21",
|
|
59
|
+
"@types/react-dom": "^18.2.7",
|
|
59
60
|
"@typescript-eslint/eslint-plugin": "^4.17.0",
|
|
60
61
|
"@typescript-eslint/parser": "^4.17.0",
|
|
61
62
|
"babel-eslint": "^10.1.0",
|
|
@@ -69,21 +70,24 @@
|
|
|
69
70
|
"eslint-plugin-jsx-a11y": "^6.4.1",
|
|
70
71
|
"eslint-plugin-react": "^7.22.0",
|
|
71
72
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
72
|
-
"husky": "^3.0
|
|
73
|
-
"jest": "^
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"mobx
|
|
77
|
-
"
|
|
78
|
-
"prettier
|
|
79
|
-
"
|
|
80
|
-
"react
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
|
|
73
|
+
"husky": "^3.1.0",
|
|
74
|
+
"jest": "^29.6.4",
|
|
75
|
+
"jest-environment-jsdom": "^29.6.4",
|
|
76
|
+
"lint-staged": "^14.0.1",
|
|
77
|
+
"mobx": "^6.10.2",
|
|
78
|
+
"mobx-react": "^9.0.1",
|
|
79
|
+
"prettier": "2.8.8",
|
|
80
|
+
"prettier-plugin-organize-imports": "^3.2.3",
|
|
81
|
+
"react": "^18.2.0",
|
|
82
|
+
"react-dom": "^18.2.0",
|
|
83
|
+
"semantic-release": "^21.1.1",
|
|
84
|
+
"storybook": "^7.4.0",
|
|
85
|
+
"ts-jest": "^29.1.1",
|
|
86
|
+
"ts-node": "^10.9.1",
|
|
87
|
+
"ts-patch": "^3.0.2",
|
|
88
|
+
"tslib": "^2.6.2",
|
|
89
|
+
"typescript": "^5.2.2",
|
|
90
|
+
"typescript-transform-paths": "^3.4.6"
|
|
91
|
+
},
|
|
92
|
+
"packageManager": "yarn@3.6.3"
|
|
89
93
|
}
|