@homebound/form-state 2.2.12 → 2.4.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/formState.d.ts +11 -4
- package/dist/formState.js +26 -21
- package/dist/formState.test.js +118 -34
- package/dist/useFormState.js +6 -3
- package/dist/useFormState.test.js +7 -2
- package/package.json +4 -2
package/dist/formState.d.ts
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
*/
|
|
26
26
|
export declare type ObjectState<T, P = any> = FieldStates<T> & FieldState<P, T> & {
|
|
27
27
|
/** Sets the state of fields in `state`. */
|
|
28
|
-
set(state: Partial<T
|
|
28
|
+
set(state: Partial<T>, opts?: SetOpts): void;
|
|
29
29
|
/** Returns whether the object can be saved, i.e. is valid, but also as a side-effect marks touched. */
|
|
30
30
|
canSave(): boolean;
|
|
31
31
|
};
|
|
@@ -72,14 +72,21 @@ export interface FieldState<T, V> {
|
|
|
72
72
|
readonly changedValue: V;
|
|
73
73
|
/** Focuses the field. Disables changes from `ObjectState.set` calls. */
|
|
74
74
|
focus(): void;
|
|
75
|
-
/** Blur
|
|
75
|
+
/** Blur marks the field as touched and triggers an auto-save. */
|
|
76
76
|
blur(): void;
|
|
77
|
-
|
|
77
|
+
/** Triggers an auto-save; the caller (i.e. useFormState) should still dirty & valid check. */
|
|
78
|
+
maybeAutoSave(): void;
|
|
79
|
+
set(value: V, opts?: SetOpts): void;
|
|
78
80
|
/** Reverts back to the original value and resets dirty/touched. */
|
|
79
81
|
reset(): void;
|
|
80
82
|
/** Accepts the current changed value (if any) as the original and resets dirty/touched. */
|
|
81
83
|
save(): void;
|
|
82
84
|
}
|
|
85
|
+
/** Public options for our `set` command. */
|
|
86
|
+
export interface SetOpts {
|
|
87
|
+
/** Whether this `set` should trigger an auto-save; defaults to true. */
|
|
88
|
+
autoSave?: boolean;
|
|
89
|
+
}
|
|
83
90
|
/** Form state for list of children, i.e. `U` is a `Book` in a form with a `books: Book[]`. */
|
|
84
91
|
export interface ListFieldState<T, U> extends Omit<FieldState<T, U[]>, "originalValue"> {
|
|
85
92
|
readonly rows: ReadonlyArray<ObjectState<U>>;
|
|
@@ -156,7 +163,7 @@ export declare type ObjectFieldConfig<U> = {
|
|
|
156
163
|
* individual fields as well as the top-level form/object itself.
|
|
157
164
|
*/
|
|
158
165
|
export declare function createObjectState<T>(config: ObjectConfig<T>, instance: T, opts?: {
|
|
159
|
-
|
|
166
|
+
maybeAutoSave?: () => void;
|
|
160
167
|
}): ObjectState<T>;
|
|
161
168
|
declare type Primitive = undefined | null | boolean | string | number | Function | Date | {
|
|
162
169
|
toJSON(): any;
|
package/dist/formState.js
CHANGED
|
@@ -12,7 +12,8 @@ const utils_1 = require("./utils");
|
|
|
12
12
|
// We pre-emptively make this a mobx action so that it's identity doesn't change when proxy-ified
|
|
13
13
|
// and breaks our ability to do `rules.some(r => r === required)`.
|
|
14
14
|
exports.required = mobx_1.action(({ value: v }) => {
|
|
15
|
-
|
|
15
|
+
const isEmptyString = typeof v === "string" ? v.trim() === "" : false;
|
|
16
|
+
return v !== undefined && v !== null && !isEmptyString ? undefined : "Required";
|
|
16
17
|
});
|
|
17
18
|
/**
|
|
18
19
|
* Creates a new `ObjectState` for a given form object `T` given config rules in `config`.
|
|
@@ -23,10 +24,10 @@ exports.required = mobx_1.action(({ value: v }) => {
|
|
|
23
24
|
*/
|
|
24
25
|
function createObjectState(config, instance, opts = {}) {
|
|
25
26
|
const noop = () => { };
|
|
26
|
-
return newObjectState(config, undefined, instance, undefined, opts.
|
|
27
|
+
return newObjectState(config, undefined, instance, undefined, opts.maybeAutoSave || noop);
|
|
27
28
|
}
|
|
28
29
|
exports.createObjectState = createObjectState;
|
|
29
|
-
function newObjectState(config, parentState, instance, key,
|
|
30
|
+
function newObjectState(config, parentState, instance, key, maybeAutoSave) {
|
|
30
31
|
// This is what we return, but we only know it's value until we call `observable`, so
|
|
31
32
|
// we create a mutable variable to capture it so that we can create fields/call their
|
|
32
33
|
// constructors and give them a way to access it later.
|
|
@@ -42,16 +43,16 @@ function newObjectState(config, parentState, instance, key, onBlur) {
|
|
|
42
43
|
const config = _config;
|
|
43
44
|
let field;
|
|
44
45
|
if (config.type === "value") {
|
|
45
|
-
field = newValueFieldState(instance, getObjectState, key, config.rules || [], config.isIdKey || key === "id", config.isDeleteKey || false, config.isReadOnlyKey || false, config.computed || false, config.readOnly || false,
|
|
46
|
+
field = newValueFieldState(instance, getObjectState, key, config.rules || [], config.isIdKey || key === "id", config.isDeleteKey || false, config.isReadOnlyKey || false, config.computed || false, config.readOnly || false, maybeAutoSave);
|
|
46
47
|
}
|
|
47
48
|
else if (config.type === "list") {
|
|
48
|
-
field = newListFieldState(instance, getObjectState, key, config.rules || [], config, config.config,
|
|
49
|
+
field = newListFieldState(instance, getObjectState, key, config.rules || [], config, config.config, maybeAutoSave);
|
|
49
50
|
}
|
|
50
51
|
else if (config.type === "object") {
|
|
51
52
|
if (!instance[key]) {
|
|
52
53
|
instance[key] = {};
|
|
53
54
|
}
|
|
54
|
-
field = newObjectState(config.config, getObjectState, instance[key], key,
|
|
55
|
+
field = newObjectState(config.config, getObjectState, instance[key], key, maybeAutoSave);
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
57
58
|
throw new Error("Unsupported");
|
|
@@ -185,7 +186,7 @@ function newObjectState(config, parentState, instance, key, onBlur) {
|
|
|
185
186
|
mobx_1.reaction(() => getFields(proxy).map((f) => f.value), () => _tick.value++);
|
|
186
187
|
return proxy;
|
|
187
188
|
}
|
|
188
|
-
function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, isDeleteKey, isReadOnlyKey, computed, readOnly,
|
|
189
|
+
function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, isDeleteKey, isReadOnlyKey, computed, readOnly, maybeAutoSave) {
|
|
189
190
|
// keep a copy here for reference equality
|
|
190
191
|
const value = parentInstance[key];
|
|
191
192
|
let _originalValue = value === null ? undefined : is_plain_object_1.isPlainObject(value) ? mobx_1.toJS(value) : value;
|
|
@@ -260,14 +261,15 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
260
261
|
this._focused = true;
|
|
261
262
|
},
|
|
262
263
|
blur() {
|
|
263
|
-
|
|
264
|
-
if (this._focused) {
|
|
265
|
-
this.maybeTrim();
|
|
266
|
-
}
|
|
264
|
+
this.maybeAutoSave();
|
|
267
265
|
this._focused = false;
|
|
266
|
+
},
|
|
267
|
+
maybeAutoSave() {
|
|
268
|
+
// Now that the user is done editing the field (note maybe w/o blurring, i.e. if they hit enter), we sneak in some trim logic
|
|
269
|
+
this.maybeTrim();
|
|
268
270
|
// touched is readonly, but we're allowed to change it
|
|
269
271
|
this.touched = true;
|
|
270
|
-
|
|
272
|
+
maybeAutoSave();
|
|
271
273
|
},
|
|
272
274
|
set(value, opts = {}) {
|
|
273
275
|
if (this.readOnly && !opts.resetting && !opts.refreshing) {
|
|
@@ -298,8 +300,8 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
298
300
|
}
|
|
299
301
|
// If we're being set programmatically, i.e. we don't currently have focus,
|
|
300
302
|
// call blur to trigger any auto-saves.
|
|
301
|
-
if (!this._focused && !opts.refreshing && !opts.resetting && this.dirty && changed) {
|
|
302
|
-
this.
|
|
303
|
+
if (!this._focused && !opts.refreshing && !opts.resetting && this.dirty && changed && opts.autoSave !== false) {
|
|
304
|
+
this.maybeAutoSave();
|
|
303
305
|
}
|
|
304
306
|
},
|
|
305
307
|
reset() {
|
|
@@ -339,7 +341,7 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
339
341
|
};
|
|
340
342
|
return field;
|
|
341
343
|
}
|
|
342
|
-
function newListFieldState(parentInstance, parentState, key, rules, listConfig, config,
|
|
344
|
+
function newListFieldState(parentInstance, parentState, key, rules, listConfig, config, maybeAutoSave) {
|
|
343
345
|
// Keep a map of "item in the parent list" -> "that item's ObjectState"
|
|
344
346
|
const rowMap = new Map();
|
|
345
347
|
const _tick = mobx_1.observable({ value: 1 });
|
|
@@ -399,7 +401,7 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
399
401
|
// Because we're reading from this.value, child will be the proxy version
|
|
400
402
|
let childState = rowMap.get(child);
|
|
401
403
|
if (!childState) {
|
|
402
|
-
childState = newObjectState(config, parentState, child, undefined,
|
|
404
|
+
childState = newObjectState(config, parentState, child, undefined, maybeAutoSave);
|
|
403
405
|
rowMap.set(child, childState);
|
|
404
406
|
}
|
|
405
407
|
return childState;
|
|
@@ -442,8 +444,11 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
442
444
|
},
|
|
443
445
|
blur() {
|
|
444
446
|
this._focused = false;
|
|
447
|
+
this.maybeAutoSave();
|
|
448
|
+
},
|
|
449
|
+
maybeAutoSave() {
|
|
445
450
|
this.touched = true;
|
|
446
|
-
|
|
451
|
+
maybeAutoSave();
|
|
447
452
|
},
|
|
448
453
|
set(values, opts = {}) {
|
|
449
454
|
if (this.readOnly && !opts.resetting && !opts.refreshing) {
|
|
@@ -462,7 +467,7 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
462
467
|
}
|
|
463
468
|
}
|
|
464
469
|
// If we didn't have an existing child, just make a new object state
|
|
465
|
-
childState = createObjectState(config, value, {
|
|
470
|
+
childState = createObjectState(config, value, { maybeAutoSave });
|
|
466
471
|
rowMap.set(value, childState);
|
|
467
472
|
}
|
|
468
473
|
// Return the already-observable'd value so that our `parent.value[key] = values` doesn't re-proxy things
|
|
@@ -476,12 +481,12 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
476
481
|
},
|
|
477
482
|
add(value, spliceIndex) {
|
|
478
483
|
// This is called by the user, so value should be a non-proxy value we should keep
|
|
479
|
-
const childState = createObjectState(config, value, {
|
|
484
|
+
const childState = createObjectState(config, value, { maybeAutoSave });
|
|
480
485
|
rowMap.set(value, childState);
|
|
481
486
|
this.ensureSet();
|
|
482
487
|
this.value.splice(typeof spliceIndex === "number" ? spliceIndex : this.value.length, 0, childState.value);
|
|
483
488
|
_tick.value++;
|
|
484
|
-
|
|
489
|
+
maybeAutoSave();
|
|
485
490
|
},
|
|
486
491
|
remove(indexOrValue) {
|
|
487
492
|
this.ensureSet();
|
|
@@ -495,7 +500,7 @@ function newListFieldState(parentInstance, parentState, key, rules, listConfig,
|
|
|
495
500
|
}
|
|
496
501
|
}
|
|
497
502
|
_tick.value++;
|
|
498
|
-
|
|
503
|
+
maybeAutoSave();
|
|
499
504
|
},
|
|
500
505
|
reset() {
|
|
501
506
|
if (originalCopy) {
|
package/dist/formState.test.js
CHANGED
|
@@ -39,6 +39,21 @@ describe("formState", () => {
|
|
|
39
39
|
expect(a.title.required).toBeTruthy();
|
|
40
40
|
expect(a.isPublished.required).toBeFalsy();
|
|
41
41
|
});
|
|
42
|
+
it("trims whitespace within the required rule", () => {
|
|
43
|
+
const a = formState_1.createObjectState({ title: { type: "value", rules: [formState_1.required] } }, { title: "initial valid title" });
|
|
44
|
+
let numErrors = 0;
|
|
45
|
+
mobx_1.autorun(() => {
|
|
46
|
+
numErrors = a.title.errors.length;
|
|
47
|
+
});
|
|
48
|
+
expect(a.valid).toBeTruthy();
|
|
49
|
+
expect(numErrors).toEqual(0);
|
|
50
|
+
// When a value contains only whitespace
|
|
51
|
+
a.title.value = " ";
|
|
52
|
+
expect(a.title.valid).toBeFalsy();
|
|
53
|
+
expect(numErrors).toEqual(1);
|
|
54
|
+
expect(a.title.errors).toEqual(["Required"]);
|
|
55
|
+
expect(a.errors).toEqual(["title: Required"]);
|
|
56
|
+
});
|
|
42
57
|
it("can set values", () => {
|
|
43
58
|
const a = formState_1.createObjectState({ title: { type: "value" } }, { title: "b1" });
|
|
44
59
|
expect(a.title.value).toEqual("b1");
|
|
@@ -285,6 +300,52 @@ describe("formState", () => {
|
|
|
285
300
|
expect(a.firstName.valid).toBeTruthy();
|
|
286
301
|
expect(a.lastName.errors).toEqual(["Last name cannot be first name"]);
|
|
287
302
|
});
|
|
303
|
+
it("passes sanitized values to validation rules", () => {
|
|
304
|
+
// Given a field with a validation rule
|
|
305
|
+
const rule = jest.fn().mockReturnValue("Required");
|
|
306
|
+
const a = formState_1.createObjectState({ firstName: { type: "value", rules: [rule] } }, {});
|
|
307
|
+
expect(rule).toHaveBeenCalledTimes(0);
|
|
308
|
+
// When the field is set to empty string
|
|
309
|
+
a.firstName.value = "";
|
|
310
|
+
expect(a.firstName.errors).toEqual(["Required"]);
|
|
311
|
+
expect(rule).toHaveBeenCalledTimes(1);
|
|
312
|
+
// Then the validation rule was passed undefined
|
|
313
|
+
expect(rule).toHaveBeenCalledWith({
|
|
314
|
+
value: undefined,
|
|
315
|
+
originalValue: undefined,
|
|
316
|
+
key: "firstName",
|
|
317
|
+
object: expect.anything(),
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
it("passes trimmed values to validation rules on maybeAutoSave", () => {
|
|
321
|
+
// Given we have a required rule
|
|
322
|
+
const rule = jest.fn().mockReturnValue("Required");
|
|
323
|
+
const a = formState_1.createObjectState({ firstName: { type: "value", rules: [rule] } }, {});
|
|
324
|
+
expect(rule).toHaveBeenCalledTimes(0);
|
|
325
|
+
// When the user initially sets an empty string value
|
|
326
|
+
a.firstName.focus();
|
|
327
|
+
a.firstName.value = " ";
|
|
328
|
+
expect(a.firstName.errors).toEqual(["Required"]);
|
|
329
|
+
// Then initially the rule is called with the empty string
|
|
330
|
+
expect(rule).toHaveBeenCalledTimes(1);
|
|
331
|
+
expect(rule).toHaveBeenCalledWith({
|
|
332
|
+
value: " ",
|
|
333
|
+
originalValue: undefined,
|
|
334
|
+
key: "firstName",
|
|
335
|
+
object: expect.anything(),
|
|
336
|
+
});
|
|
337
|
+
// But then when the field maybeAutoSaves (i.e. via blur)
|
|
338
|
+
a.firstName.maybeAutoSave();
|
|
339
|
+
expect(a.firstName.errors).toEqual(["Required"]);
|
|
340
|
+
// Then the rule is called with the undefined
|
|
341
|
+
expect(rule).toHaveBeenCalledTimes(2);
|
|
342
|
+
expect(rule).toHaveBeenCalledWith({
|
|
343
|
+
value: undefined,
|
|
344
|
+
originalValue: undefined,
|
|
345
|
+
key: "firstName",
|
|
346
|
+
object: expect.anything(),
|
|
347
|
+
});
|
|
348
|
+
});
|
|
288
349
|
it("simple value changes trigger observers", () => {
|
|
289
350
|
const a = formState_1.createObjectState({
|
|
290
351
|
title: { type: "value", rules: [formState_1.required] },
|
|
@@ -330,69 +391,78 @@ describe("formState", () => {
|
|
|
330
391
|
a1.books.remove(0);
|
|
331
392
|
expect(a1.dirty).toBeFalsy();
|
|
332
393
|
});
|
|
333
|
-
it("calls
|
|
334
|
-
const
|
|
335
|
-
const a1 = createAuthorInputState({ books: [] },
|
|
394
|
+
it("calls maybeAutoSave when adding or removing to a list", () => {
|
|
395
|
+
const maybeAutoSave = jest.fn();
|
|
396
|
+
const a1 = createAuthorInputState({ books: [] }, maybeAutoSave);
|
|
336
397
|
expect(a1.books.dirty).toBeFalsy();
|
|
337
398
|
a1.books.add({ title: "t2" });
|
|
338
399
|
expect(a1.books.dirty).toBeTruthy();
|
|
339
400
|
a1.books.remove(0);
|
|
340
401
|
expect(a1.dirty).toBeFalsy();
|
|
341
|
-
expect(
|
|
402
|
+
expect(maybeAutoSave).toBeCalledTimes(2);
|
|
342
403
|
});
|
|
343
|
-
it("calls
|
|
344
|
-
const
|
|
404
|
+
it("calls maybeAutoSave when programmatically setting a value", () => {
|
|
405
|
+
const maybeAutoSave = jest.fn();
|
|
345
406
|
// Given an author listening for blur
|
|
346
|
-
const a1 = createAuthorInputState({ books: [{}] },
|
|
407
|
+
const a1 = createAuthorInputState({ books: [{}] }, maybeAutoSave);
|
|
347
408
|
// When we programmatically set a field that isn't focused
|
|
348
409
|
a1.firstName.value = "first";
|
|
349
|
-
// Then we call
|
|
350
|
-
expect(
|
|
410
|
+
// Then we call maybeAutoSave
|
|
411
|
+
expect(maybeAutoSave).toBeCalledTimes(1);
|
|
351
412
|
// And when we set a nested value
|
|
352
413
|
a1.books.rows[0].title.value = "title";
|
|
353
|
-
// Then we called
|
|
354
|
-
expect(
|
|
414
|
+
// Then we called maybeAutoSave again
|
|
415
|
+
expect(maybeAutoSave).toBeCalledTimes(2);
|
|
355
416
|
});
|
|
356
|
-
it("
|
|
357
|
-
const
|
|
417
|
+
it("can skip maybeAutoSave when programmatically setting a value", () => {
|
|
418
|
+
const maybeAutoSave = jest.fn();
|
|
358
419
|
// Given an author listening for blur
|
|
359
|
-
const a1 = createAuthorInputState({ books: [{}] },
|
|
420
|
+
const a1 = createAuthorInputState({ books: [{}] }, maybeAutoSave);
|
|
421
|
+
// When we programmatically set a field that isn't focused
|
|
422
|
+
a1.set({ firstName: "first" }, { autoSave: false });
|
|
423
|
+
// Then we don't call maybeAutoSave
|
|
424
|
+
expect(maybeAutoSave).toBeCalledTimes(0);
|
|
425
|
+
});
|
|
426
|
+
it("defers calling maybeAutoSave when setting a focused value", () => {
|
|
427
|
+
const maybeAutoSave = jest.fn();
|
|
428
|
+
// Given an author listening for blur
|
|
429
|
+
const a1 = createAuthorInputState({ books: [{}] }, maybeAutoSave);
|
|
360
430
|
// And the field is focused
|
|
361
431
|
a1.firstName.focus();
|
|
362
432
|
// When we we set the field
|
|
363
433
|
a1.firstName.value = "first";
|
|
364
|
-
// Then we don't call
|
|
365
|
-
expect(
|
|
434
|
+
// Then we don't call maybeAutoSave
|
|
435
|
+
expect(maybeAutoSave).toBeCalledTimes(0);
|
|
366
436
|
});
|
|
367
|
-
it("skips
|
|
368
|
-
const
|
|
437
|
+
it("skips maybeAutoSave when refreshing", () => {
|
|
438
|
+
const maybeAutoSave = jest.fn();
|
|
369
439
|
// Given an author listening for blur
|
|
370
|
-
const a1 = createAuthorInputState({ books: [{}] },
|
|
440
|
+
const a1 = createAuthorInputState({ books: [{}] }, maybeAutoSave);
|
|
371
441
|
// When we programmatically set a field that isn't focused
|
|
372
442
|
a1.set({ firstName: "first" }, { refreshing: true });
|
|
373
|
-
// Then we don't call
|
|
374
|
-
expect(
|
|
443
|
+
// Then we don't call maybeAutoSave
|
|
444
|
+
expect(maybeAutoSave).toBeCalledTimes(0);
|
|
375
445
|
});
|
|
376
|
-
it("skips
|
|
377
|
-
const
|
|
446
|
+
it("skips maybeAutoSave when resetting", () => {
|
|
447
|
+
const maybeAutoSave = jest.fn();
|
|
378
448
|
// Given an author listening for blur
|
|
379
|
-
const a1 = createAuthorInputState({ books: [{}] },
|
|
380
|
-
// And we called
|
|
449
|
+
const a1 = createAuthorInputState({ books: [{}] }, maybeAutoSave);
|
|
450
|
+
// And we called maybeAutoSave once
|
|
381
451
|
a1.set({ firstName: "first" });
|
|
382
|
-
expect(
|
|
452
|
+
expect(maybeAutoSave).toBeCalledTimes(1);
|
|
383
453
|
// When we reset
|
|
384
454
|
a1.reset();
|
|
385
455
|
// We don't call blur again
|
|
386
|
-
expect(
|
|
456
|
+
expect(maybeAutoSave).toBeCalledTimes(1);
|
|
387
457
|
});
|
|
388
|
-
it("skips
|
|
389
|
-
const
|
|
458
|
+
it("skips maybeAutoSave when not dirty", () => {
|
|
459
|
+
const maybeAutoSave = jest.fn();
|
|
390
460
|
// Given an author listening for blur
|
|
391
|
-
const a1 = createAuthorInputState({ firstName: "first", books: [{}] },
|
|
461
|
+
const a1 = createAuthorInputState({ firstName: "first", books: [{}] }, maybeAutoSave);
|
|
392
462
|
// When we programmatically set a field to it's existing valued
|
|
393
463
|
a1.firstName.value = "first";
|
|
394
|
-
// Then we don't call
|
|
395
|
-
expect(
|
|
464
|
+
// Then we don't call maybeAutoSave
|
|
465
|
+
expect(maybeAutoSave).toBeCalledTimes(0);
|
|
396
466
|
});
|
|
397
467
|
it("knows list of primitives are dirty", () => {
|
|
398
468
|
const a1 = formState_1.createObjectState({ favoriteColors: { type: "value" } }, {});
|
|
@@ -955,6 +1025,20 @@ describe("formState", () => {
|
|
|
955
1025
|
// Then we trim it
|
|
956
1026
|
expect(formState.firstName.value).toEqual("f");
|
|
957
1027
|
});
|
|
1028
|
+
it("trims string values on maybeAutoSave", () => {
|
|
1029
|
+
const formState = formState_1.createObjectState(authorWithBooksConfig, { firstName: "f", lastName: "l" });
|
|
1030
|
+
// Given the user is typing with spaces
|
|
1031
|
+
formState.firstName.focus();
|
|
1032
|
+
formState.firstName.set("f ");
|
|
1033
|
+
// And we initially keep the space
|
|
1034
|
+
expect(formState.firstName.value).toEqual("f ");
|
|
1035
|
+
// When the user hits Enter (which will call maybeAutoSave)
|
|
1036
|
+
formState.firstName.maybeAutoSave();
|
|
1037
|
+
// Then we trim it
|
|
1038
|
+
expect(formState.firstName.value).toEqual("f");
|
|
1039
|
+
// And the field is considered touched
|
|
1040
|
+
expect(formState.firstName.touched).toEqual(true);
|
|
1041
|
+
});
|
|
958
1042
|
it("trims empty values to undefined on blur", () => {
|
|
959
1043
|
const formState = formState_1.createObjectState(authorWithBooksConfig, { firstName: "f", lastName: "l" });
|
|
960
1044
|
// Given the user is typing with only spaces
|
|
@@ -1093,8 +1177,8 @@ const authorWithBooksConfig = {
|
|
|
1093
1177
|
},
|
|
1094
1178
|
},
|
|
1095
1179
|
};
|
|
1096
|
-
function createAuthorInputState(input,
|
|
1097
|
-
return formState_1.createObjectState(authorWithBooksConfig, input, {
|
|
1180
|
+
function createAuthorInputState(input, maybeAutoSave) {
|
|
1181
|
+
return formState_1.createObjectState(authorWithBooksConfig, input, { maybeAutoSave });
|
|
1098
1182
|
}
|
|
1099
1183
|
const authorWithAddressConfig = {
|
|
1100
1184
|
id: { type: "value" },
|
package/dist/useFormState.js
CHANGED
|
@@ -6,6 +6,9 @@ const mobx_1 = require("mobx");
|
|
|
6
6
|
const react_1 = require("react");
|
|
7
7
|
const formState_1 = require("./formState");
|
|
8
8
|
const utils_1 = require("./utils");
|
|
9
|
+
// If the user's autoSave hook makes some last-minute `.set` calls to sneak
|
|
10
|
+
// in some business logic right before their GraphQL mutation call, ignore it
|
|
11
|
+
// so that we don't infinite loop.
|
|
9
12
|
let isAutoSaving = false;
|
|
10
13
|
/**
|
|
11
14
|
* Creates a formState instance for editing in a form.
|
|
@@ -22,8 +25,8 @@ function useFormState(opts) {
|
|
|
22
25
|
// If they're using init.input, useMemo on it (and it might be an array), otherwise allow the identity of init be unstable
|
|
23
26
|
const dep = init && "input" in init && "map" in init ? (Array.isArray(init.input) ? init.input : [init.input]) : [];
|
|
24
27
|
const form = react_1.useMemo(() => {
|
|
25
|
-
function
|
|
26
|
-
// Don't use canSave() because we don't want to set touched for all
|
|
28
|
+
function maybeAutoSave() {
|
|
29
|
+
// Don't use canSave() because we don't want to set touched for all the fields
|
|
27
30
|
if (autoSaveRef.current && form.dirty && form.valid && !isAutoSaving) {
|
|
28
31
|
try {
|
|
29
32
|
isAutoSaving = true;
|
|
@@ -35,7 +38,7 @@ function useFormState(opts) {
|
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
const value = firstRunRef.current ? firstInitValue : initValue(config, init);
|
|
38
|
-
const form = formState_1.createObjectState(config, value, {
|
|
41
|
+
const form = formState_1.createObjectState(config, value, { maybeAutoSave });
|
|
39
42
|
form.readOnly = readOnly;
|
|
40
43
|
// The identity of `addRules` is not stable, but assume that it is for better UX.
|
|
41
44
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -306,8 +306,8 @@ describe("useFormState", () => {
|
|
|
306
306
|
});
|
|
307
307
|
return (jsx_runtime_1.jsx(mobx_react_1.Observer, { children: () => (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("button", { "data-testid": "refreshData", onClick: () => setData(data2) }, void 0),
|
|
308
308
|
jsx_runtime_1.jsx("button", { "data-testid": "add", onClick: () => form.books.add({ title: "New Book" }) }, void 0),
|
|
309
|
-
jsx_runtime_1.jsx("button", { "data-testid": "blurBookOne", onClick: () => form.books.rows[0].title
|
|
310
|
-
jsx_runtime_1.jsx("button", { "data-testid": "blurBookTwo", onClick: () => form.books.rows[1].title
|
|
309
|
+
jsx_runtime_1.jsx("button", { "data-testid": "blurBookOne", onClick: () => focusAndBlur(form.books.rows[0].title) }, void 0),
|
|
310
|
+
jsx_runtime_1.jsx("button", { "data-testid": "blurBookTwo", onClick: () => focusAndBlur(form.books.rows[1].title) }, void 0)] }, void 0)) }, void 0));
|
|
311
311
|
}
|
|
312
312
|
// Given a formState with `onBlur` set
|
|
313
313
|
const r = await rtl_utils_1.render(jsx_runtime_1.jsx(TestComponent, {}, void 0));
|
|
@@ -472,3 +472,8 @@ const authorWithAddressAndBooksConfig = {
|
|
|
472
472
|
},
|
|
473
473
|
},
|
|
474
474
|
};
|
|
475
|
+
/** Emulates a user focusing and then blurring a field. */
|
|
476
|
+
function focusAndBlur(state) {
|
|
477
|
+
state.focus();
|
|
478
|
+
state.blur();
|
|
479
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@homebound/form-state",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"author": "Homebound",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,7 +25,9 @@
|
|
|
25
25
|
"fast-deep-equal": "^3.1.3",
|
|
26
26
|
"is-plain-object": "^5.0.0",
|
|
27
27
|
"mobx": "^6.3.2",
|
|
28
|
-
"mobx-react": "^7.2.0"
|
|
28
|
+
"mobx-react": "^7.2.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
29
31
|
"react": ">=16"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|