@homebound/form-state 2.2.7 → 2.2.11
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.js +5 -5
- package/dist/formState.d.ts +3 -59
- package/dist/formState.js +29 -119
- package/dist/formState.test.js +72 -394
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/useFormState.d.ts +57 -0
- package/dist/useFormState.js +126 -0
- package/dist/useFormState.test.d.ts +1 -0
- package/dist/useFormState.test.js +474 -0
- package/package.json +1 -1
package/dist/FormStateApp.js
CHANGED
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FormStateApp = void 0;
|
|
4
4
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
5
|
const mobx_react_1 = require("mobx-react");
|
|
6
|
-
const
|
|
6
|
+
const index_1 = require("./index");
|
|
7
7
|
function FormStateApp() {
|
|
8
|
-
const formState =
|
|
8
|
+
const formState = index_1.useFormState({
|
|
9
9
|
config: formConfig,
|
|
10
10
|
// Simulate getting the initial form state back from a server call
|
|
11
11
|
init: {
|
|
@@ -60,13 +60,13 @@ function FormStateApp() {
|
|
|
60
60
|
exports.FormStateApp = FormStateApp;
|
|
61
61
|
// Configure the fields/behavior for AuthorInput's fields
|
|
62
62
|
const formConfig = {
|
|
63
|
-
firstName: { type: "value", rules: [
|
|
64
|
-
lastName: { type: "value", rules: [
|
|
63
|
+
firstName: { type: "value", rules: [index_1.required] },
|
|
64
|
+
lastName: { type: "value", rules: [index_1.required], readOnly: true },
|
|
65
65
|
books: {
|
|
66
66
|
type: "list",
|
|
67
67
|
rules: [({ value: list }) => ((list || []).length === 0 ? "Empty" : undefined)],
|
|
68
68
|
config: {
|
|
69
|
-
title: { type: "value", rules: [
|
|
69
|
+
title: { type: "value", rules: [index_1.required] },
|
|
70
70
|
},
|
|
71
71
|
},
|
|
72
72
|
};
|
package/dist/formState.d.ts
CHANGED
|
@@ -1,47 +1,3 @@
|
|
|
1
|
-
export declare type UseFormStateOpts<T, I> = {
|
|
2
|
-
/** The form configuration, should be a module-level const or useMemo'd. */
|
|
3
|
-
config: ObjectConfig<T>;
|
|
4
|
-
/**
|
|
5
|
-
* Provides the form's initial value.
|
|
6
|
-
*
|
|
7
|
-
* User's can either:
|
|
8
|
-
*
|
|
9
|
-
* - Provide no initial value (don't set `init` at all)
|
|
10
|
-
* - Provide an initial value that already matches the form type `T` (i.e. set `init: data`)
|
|
11
|
-
* - Provide an initial value from an object that _almost_ matches the form type `T`,
|
|
12
|
-
* but needs to be mapped from it's input type `I` to the form type
|
|
13
|
-
* (i.e. set `init: { input: data, map: (data) => ...}`).
|
|
14
|
-
*
|
|
15
|
-
* The value of using the 3rd option is that: a) we internally `useMemo` on the identity of the
|
|
16
|
-
* `init.input` (i.e. a response from an Apollo hook) and don't require the `map` function
|
|
17
|
-
* to have a stable identity, and also b) we will null-check/undefined-check `init.input` and
|
|
18
|
-
* only call `init.map` if it's set, otherwise we'll use `init.ifDefined` or `{}`, saving you
|
|
19
|
-
* from having to null check within your `init.map` function.
|
|
20
|
-
*/
|
|
21
|
-
init?: T | {
|
|
22
|
-
input: I;
|
|
23
|
-
map: (input: Exclude<I, null | undefined>) => T;
|
|
24
|
-
ifUndefined?: T;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* A hook to add custom, cross-field validation rules that can be difficult to setup directly in the config DSL.
|
|
28
|
-
*
|
|
29
|
-
* Does not need to be stable/useMemo'd.
|
|
30
|
-
*/
|
|
31
|
-
addRules?: (state: ObjectState<T>) => void;
|
|
32
|
-
/** Whether the form should be read only, when changed it won't re-create the whole form. */
|
|
33
|
-
readOnly?: boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Fired when the form should auto-save, i.e. after a) blur and b) all fields are valid.
|
|
36
|
-
*
|
|
37
|
-
* Does not need to be stable/useMemo'd.
|
|
38
|
-
*/
|
|
39
|
-
autoSave?: (state: ObjectState<T>) => void;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Creates a formState instance for editing in a form.
|
|
43
|
-
*/
|
|
44
|
-
export declare function useFormState<T, I>(opts: UseFormStateOpts<T, I>): ObjectState<T>;
|
|
45
1
|
/**
|
|
46
2
|
* Wraps a given input/on-the-wire type `T` for editing in a form.
|
|
47
3
|
*
|
|
@@ -143,7 +99,7 @@ declare type OmitIf<Base, Condition> = Pick<Base, {
|
|
|
143
99
|
[Key in keyof Base]: Base[Key] extends Condition ? never : Key;
|
|
144
100
|
}[keyof Base]>;
|
|
145
101
|
/** Field configuration for primitive values, i.e. strings/numbers/Dates/user-defined types. */
|
|
146
|
-
declare type ValueFieldConfig<T, V> = {
|
|
102
|
+
export declare type ValueFieldConfig<T, V> = {
|
|
147
103
|
type: "value";
|
|
148
104
|
rules?: Rule<T, V | null | undefined>[];
|
|
149
105
|
/**
|
|
@@ -168,7 +124,7 @@ declare type ValueFieldConfig<T, V> = {
|
|
|
168
124
|
readOnly?: boolean;
|
|
169
125
|
};
|
|
170
126
|
/** Field configuration for list values, i.e. `U` is `Book` in a form with `books: Book[]`. */
|
|
171
|
-
declare type ListFieldConfig<T, U> = {
|
|
127
|
+
export declare type ListFieldConfig<T, U> = {
|
|
172
128
|
type: "list";
|
|
173
129
|
/** Rules that can run on the full list of children. */
|
|
174
130
|
rules?: Rule<T, readonly ObjectState<U>[]>[];
|
|
@@ -187,7 +143,7 @@ declare type ListFieldConfig<T, U> = {
|
|
|
187
143
|
*/
|
|
188
144
|
update?: "exhaustive" | "incremental";
|
|
189
145
|
};
|
|
190
|
-
declare type ObjectFieldConfig<U> = {
|
|
146
|
+
export declare type ObjectFieldConfig<U> = {
|
|
191
147
|
type: "object";
|
|
192
148
|
/** Config for the child's form state, i.e. each book. */
|
|
193
149
|
config: ObjectConfig<U>;
|
|
@@ -202,18 +158,6 @@ declare type ObjectFieldConfig<U> = {
|
|
|
202
158
|
export declare function createObjectState<T>(config: ObjectConfig<T>, instance: T, opts?: {
|
|
203
159
|
onBlur?: () => void;
|
|
204
160
|
}): ObjectState<T>;
|
|
205
|
-
/**
|
|
206
|
-
* Picks ony fields out of `instance` that are in the `formConfig`.
|
|
207
|
-
*
|
|
208
|
-
* This is useful for creating a form input from a GraphQL query result,
|
|
209
|
-
* but ignoring extra data in the query that we don't want/need in the form.
|
|
210
|
-
*
|
|
211
|
-
* (Especially because the form.value will become our mutation's input type
|
|
212
|
-
* that goes on the wire, we don't want superfluous data in it.)
|
|
213
|
-
*/
|
|
214
|
-
export declare function pickFields<T, I>(formConfig: ObjectConfig<T>, instance: I): {
|
|
215
|
-
[K in keyof T]: K extends keyof I ? I[K] : never;
|
|
216
|
-
};
|
|
217
161
|
declare type Primitive = undefined | null | boolean | string | number | Function | Date | {
|
|
218
162
|
toJSON(): any;
|
|
219
163
|
};
|
package/dist/formState.js
CHANGED
|
@@ -3,64 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.createObjectState = exports.required = void 0;
|
|
7
7
|
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
8
8
|
const is_plain_object_1 = __importDefault(require("is-plain-object"));
|
|
9
9
|
const mobx_1 = require("mobx");
|
|
10
|
-
const react_1 = require("react");
|
|
11
10
|
const utils_1 = require("./utils");
|
|
12
|
-
/**
|
|
13
|
-
* Creates a formState instance for editing in a form.
|
|
14
|
-
*/
|
|
15
|
-
function useFormState(opts) {
|
|
16
|
-
const { config, init, addRules, readOnly = false, autoSave } = opts;
|
|
17
|
-
// Use a ref so our memo'ized `onBlur` always see the latest value
|
|
18
|
-
const autoSaveRef = react_1.useRef(autoSave);
|
|
19
|
-
autoSaveRef.current = autoSave;
|
|
20
|
-
const firstRunRef = react_1.useRef(true);
|
|
21
|
-
// This is a little weird, but we need to know ahead of time, before the form useMemo, if we're working with classes/mobx proxies
|
|
22
|
-
const [firstInitValue] = react_1.useState(() => initValue(config, init));
|
|
23
|
-
const isWrappingMobxProxy = !is_plain_object_1.default(firstInitValue);
|
|
24
|
-
// If they're using init.input, useMemo on it (and it might be an array), otherwise allow the identity of init be unstable
|
|
25
|
-
const dep = init && "input" in init && "map" in init ? (Array.isArray(init.input) ? init.input : [init.input]) : [];
|
|
26
|
-
const form = react_1.useMemo(() => {
|
|
27
|
-
function onBlur() {
|
|
28
|
-
// Don't use canSave() because we don't want to set touched for all of the field
|
|
29
|
-
if (autoSaveRef.current && form.dirty && form.valid) {
|
|
30
|
-
autoSaveRef.current(form);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
const value = firstRunRef.current ? firstInitValue : initValue(config, init);
|
|
34
|
-
const form = createObjectState(config, value, { onBlur });
|
|
35
|
-
form.readOnly = readOnly;
|
|
36
|
-
// The identity of `addRules` is not stable, but assume that it is for better UX.
|
|
37
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
38
|
-
(addRules || (() => { }))(form);
|
|
39
|
-
firstRunRef.current = true;
|
|
40
|
-
return form;
|
|
41
|
-
},
|
|
42
|
-
// For this useMemo, we (almost) never re-run so that we can have a stable `form` identity across query refreshes.
|
|
43
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
44
|
-
[config, ...(isWrappingMobxProxy ? dep : [])]);
|
|
45
|
-
// We use useEffect so that any mutations to the proxies, which will call `setState`s on any observers to
|
|
46
|
-
// queue their components' render), don't happen during our render, per https://fb.me/setstate-in-render.
|
|
47
|
-
react_1.useEffect(() => {
|
|
48
|
-
// Ignore the 1st run b/c our 1st useMemo already initialized `form` with the current `init` value.
|
|
49
|
-
// Also for mobx proxies, we recreate a new form-state every time init changes, so that our
|
|
50
|
-
// fields fundamentally pointing to the right proxy. So just defer to the ^ useMemo.
|
|
51
|
-
if (firstRunRef.current || isWrappingMobxProxy) {
|
|
52
|
-
firstRunRef.current = false;
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
form.set(initValue(config, init), { refreshing: true });
|
|
56
|
-
}, [form, ...dep]);
|
|
57
|
-
// Use useEffect so that we don't touch the form.init proxy during a render
|
|
58
|
-
react_1.useEffect(() => {
|
|
59
|
-
form.readOnly = readOnly;
|
|
60
|
-
}, [form, readOnly]);
|
|
61
|
-
return form;
|
|
62
|
-
}
|
|
63
|
-
exports.useFormState = useFormState;
|
|
64
11
|
/** A rule that validates `value` is not `undefined`, `null`, or empty string. */
|
|
65
12
|
// We pre-emptively make this a mobx action so that it's identity doesn't change when proxy-ified
|
|
66
13
|
// and breaks our ability to do `rules.some(r => r === required)`.
|
|
@@ -180,7 +127,7 @@ function newObjectState(config, parentState, instance, key, onBlur) {
|
|
|
180
127
|
throw new Error(`${key || "formState"} is currently readOnly`);
|
|
181
128
|
}
|
|
182
129
|
getFields(this).forEach((field) => {
|
|
183
|
-
if (field.key in value
|
|
130
|
+
if (field.key in value) {
|
|
184
131
|
field.set(value[field.key], opts);
|
|
185
132
|
}
|
|
186
133
|
});
|
|
@@ -314,11 +261,8 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
314
261
|
},
|
|
315
262
|
blur() {
|
|
316
263
|
// Now that the user is done editing the field, we sneak in some trim logic
|
|
317
|
-
if (
|
|
318
|
-
this.
|
|
319
|
-
if (this.value === "") {
|
|
320
|
-
this.set(undefined);
|
|
321
|
-
}
|
|
264
|
+
if (this._focused) {
|
|
265
|
+
this.maybeTrim();
|
|
322
266
|
}
|
|
323
267
|
this._focused = false;
|
|
324
268
|
// touched is readonly, but we're allowed to change it
|
|
@@ -329,8 +273,10 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
329
273
|
if (this.readOnly && !opts.resetting && !opts.refreshing) {
|
|
330
274
|
throw new Error(`${key} is currently readOnly`);
|
|
331
275
|
}
|
|
332
|
-
if (opts.refreshing && this.dirty && value !==
|
|
333
|
-
// Ignore
|
|
276
|
+
if (opts.refreshing && this.dirty && this.value !== value) {
|
|
277
|
+
// Ignore incoming values if we have changes (this.dirty) unless our latest change (this.value)
|
|
278
|
+
// matches the incoming value (value), b/c if it does we should accept it and reset originalValue
|
|
279
|
+
// so that we're not longer dirty.
|
|
334
280
|
return;
|
|
335
281
|
}
|
|
336
282
|
else if (computed && (opts.resetting || opts.refreshing)) {
|
|
@@ -344,11 +290,17 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
344
290
|
const coerceEmptyList = value && value instanceof Array && value.length === 0 && isEmpty(this.originalValue);
|
|
345
291
|
const newValue = keepNull ? null : isEmpty(value) || coerceEmptyList ? undefined : value;
|
|
346
292
|
// Set the value on our parent object
|
|
293
|
+
const changed = !areEqual(newValue, this.value);
|
|
347
294
|
parentInstance[key] = newValue;
|
|
348
295
|
_tick.value++;
|
|
349
296
|
if (opts.refreshing) {
|
|
350
297
|
this.originalValue = newValue;
|
|
351
298
|
}
|
|
299
|
+
// If we're being set programmatically, i.e. we don't currently have focus,
|
|
300
|
+
// call blur to trigger any auto-saves.
|
|
301
|
+
if (!this._focused && !opts.refreshing && !opts.resetting && this.dirty && changed) {
|
|
302
|
+
this.blur();
|
|
303
|
+
}
|
|
352
304
|
},
|
|
353
305
|
reset() {
|
|
354
306
|
if (!computed) {
|
|
@@ -373,6 +325,17 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
373
325
|
_originalValue = v;
|
|
374
326
|
_originalValueTick.value++;
|
|
375
327
|
},
|
|
328
|
+
maybeTrim() {
|
|
329
|
+
if (typeof this.value === "string") {
|
|
330
|
+
let newValue = this.value.trim();
|
|
331
|
+
if (newValue === "") {
|
|
332
|
+
newValue = undefined;
|
|
333
|
+
}
|
|
334
|
+
if (newValue !== this.value) {
|
|
335
|
+
this.set(newValue);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
},
|
|
376
339
|
};
|
|
377
340
|
return field;
|
|
378
341
|
}
|
|
@@ -554,7 +517,10 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
554
517
|
_tick.value++;
|
|
555
518
|
},
|
|
556
519
|
};
|
|
557
|
-
return list
|
|
520
|
+
return mobx_1.makeAutoObservable(list, {
|
|
521
|
+
// See other makeAutoObservable comment
|
|
522
|
+
value: mobx_1.computed({ equals: () => false }),
|
|
523
|
+
});
|
|
558
524
|
}
|
|
559
525
|
function isNotUndefined(value) {
|
|
560
526
|
return value !== undefined;
|
|
@@ -588,59 +554,3 @@ function areEqual(a, b) {
|
|
|
588
554
|
function hasToJSON(o) {
|
|
589
555
|
return !!(o && typeof o === "object" && "toJSON" in o);
|
|
590
556
|
}
|
|
591
|
-
/**
|
|
592
|
-
* Picks ony fields out of `instance` that are in the `formConfig`.
|
|
593
|
-
*
|
|
594
|
-
* This is useful for creating a form input from a GraphQL query result,
|
|
595
|
-
* but ignoring extra data in the query that we don't want/need in the form.
|
|
596
|
-
*
|
|
597
|
-
* (Especially because the form.value will become our mutation's input type
|
|
598
|
-
* that goes on the wire, we don't want superfluous data in it.)
|
|
599
|
-
*/
|
|
600
|
-
function pickFields(formConfig, instance) {
|
|
601
|
-
// If the caller is using classes, i.e. with their own custom observable behavior, then just use as-is
|
|
602
|
-
if (!is_plain_object_1.default(instance)) {
|
|
603
|
-
return instance;
|
|
604
|
-
}
|
|
605
|
-
return Object.fromEntries(Object.entries(formConfig).map(([key, _keyConfig]) => {
|
|
606
|
-
const keyConfig = _keyConfig;
|
|
607
|
-
const value = instance[key];
|
|
608
|
-
if (keyConfig.type === "object") {
|
|
609
|
-
if (value) {
|
|
610
|
-
return [key, pickFields(keyConfig.config, value)];
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
return [key, value];
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
else if (keyConfig.type === "list") {
|
|
617
|
-
if (mobx_1.isObservable(value)) {
|
|
618
|
-
// If we hit an observable array, leave it as the existing proxy so the our
|
|
619
|
-
// ListFieldState will react to changes in the original array.
|
|
620
|
-
return [key, value];
|
|
621
|
-
}
|
|
622
|
-
else if (value) {
|
|
623
|
-
return [key, value.map((u) => pickFields(keyConfig.config, u))];
|
|
624
|
-
}
|
|
625
|
-
else {
|
|
626
|
-
return [key, value];
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
else if (keyConfig.type === "value") {
|
|
630
|
-
return [key, value];
|
|
631
|
-
}
|
|
632
|
-
else {
|
|
633
|
-
return utils_1.assertNever(keyConfig);
|
|
634
|
-
}
|
|
635
|
-
}));
|
|
636
|
-
}
|
|
637
|
-
exports.pickFields = pickFields;
|
|
638
|
-
/** Introspects the `init` prop to see if has a `map` function/etc. and returns the form value. */
|
|
639
|
-
function initValue(config, init) {
|
|
640
|
-
const initValue = init && "input" in init && "map" in init
|
|
641
|
-
? init.input
|
|
642
|
-
? init.map(init.input)
|
|
643
|
-
: init.ifUndefined || {}
|
|
644
|
-
: init || {};
|
|
645
|
-
return pickFields(config, initValue);
|
|
646
|
-
}
|