@homebound/form-state 2.2.13 → 2.3.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/formState.d.ts +7 -2
- package/dist/formState.js +1 -1
- package/dist/formState.test.js +9 -0
- package/package.json +1 -1
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
|
};
|
|
@@ -74,12 +74,17 @@ export interface FieldState<T, V> {
|
|
|
74
74
|
focus(): void;
|
|
75
75
|
/** Blur essentially touches the field. */
|
|
76
76
|
blur(): void;
|
|
77
|
-
set(value: V): void;
|
|
77
|
+
set(value: V, opts?: SetOpts): void;
|
|
78
78
|
/** Reverts back to the original value and resets dirty/touched. */
|
|
79
79
|
reset(): void;
|
|
80
80
|
/** Accepts the current changed value (if any) as the original and resets dirty/touched. */
|
|
81
81
|
save(): void;
|
|
82
82
|
}
|
|
83
|
+
/** Public options for our `set` command. */
|
|
84
|
+
export interface SetOpts {
|
|
85
|
+
/** Whether this `set` should trigger an auto-save; defaults to true. */
|
|
86
|
+
autoSave?: boolean;
|
|
87
|
+
}
|
|
83
88
|
/** Form state for list of children, i.e. `U` is a `Book` in a form with a `books: Book[]`. */
|
|
84
89
|
export interface ListFieldState<T, U> extends Omit<FieldState<T, U[]>, "originalValue"> {
|
|
85
90
|
readonly rows: ReadonlyArray<ObjectState<U>>;
|
package/dist/formState.js
CHANGED
|
@@ -298,7 +298,7 @@ function newValueFieldState(parentInstance, parentState, key, rules, isIdKey, is
|
|
|
298
298
|
}
|
|
299
299
|
// If we're being set programmatically, i.e. we don't currently have focus,
|
|
300
300
|
// call blur to trigger any auto-saves.
|
|
301
|
-
if (!this._focused && !opts.refreshing && !opts.resetting && this.dirty && changed) {
|
|
301
|
+
if (!this._focused && !opts.refreshing && !opts.resetting && this.dirty && changed && opts.autoSave !== false) {
|
|
302
302
|
this.blur();
|
|
303
303
|
}
|
|
304
304
|
},
|
package/dist/formState.test.js
CHANGED
|
@@ -353,6 +353,15 @@ describe("formState", () => {
|
|
|
353
353
|
// Then we called onBlur again
|
|
354
354
|
expect(onBlur).toBeCalledTimes(2);
|
|
355
355
|
});
|
|
356
|
+
it("can skip onBlur when programmatically setting a value", () => {
|
|
357
|
+
const onBlur = jest.fn();
|
|
358
|
+
// Given an author listening for blur
|
|
359
|
+
const a1 = createAuthorInputState({ books: [{}] }, onBlur);
|
|
360
|
+
// When we programmatically set a field that isn't focused
|
|
361
|
+
a1.set({ firstName: "first" }, { autoSave: false });
|
|
362
|
+
// Then we don't call onBlur
|
|
363
|
+
expect(onBlur).toBeCalledTimes(0);
|
|
364
|
+
});
|
|
356
365
|
it("defers calling onBlur when setting a bound value", () => {
|
|
357
366
|
const onBlur = jest.fn();
|
|
358
367
|
// Given an author listening for blur
|