formo-react 0.2.0 → 0.4.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/core/store.d.ts +1 -0
- package/dist/core/store.js +5 -1
- package/dist/core/utils.d.ts +2 -0
- package/dist/core/utils.js +21 -0
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/core/store.d.ts
CHANGED
|
@@ -124,6 +124,7 @@ export declare class FormoStore {
|
|
|
124
124
|
setValue(path: string, value: unknown): void;
|
|
125
125
|
load(next: Record<string, any>): void;
|
|
126
126
|
reset(next?: Record<string, any>): void;
|
|
127
|
+
getDirty(): Record<string, unknown>;
|
|
127
128
|
touch(path: string): void;
|
|
128
129
|
addRow(path: string, rowValues?: Record<string, unknown>): void;
|
|
129
130
|
removeRow(path: string, index: number): void;
|
package/dist/core/store.js
CHANGED
|
@@ -4,7 +4,7 @@ import { buildInitialValues, normalize, rowDefaults } from './normalize';
|
|
|
4
4
|
import { settleValue } from './options';
|
|
5
5
|
import { evaluateChecklist, parseRules } from './rules';
|
|
6
6
|
import { buildTree, flattenErrors, flattenTree, leavesOf, rowIndexIn, shiftRowPaths, toFormPath, valuePathOf, } from './tree';
|
|
7
|
-
import { ancestorPaths, deepClone, deepEqual, deepMerge, getPath, humanize, interpolate, sameLeafValue, setIn, } from './utils';
|
|
7
|
+
import { ancestorPaths, deepClone, deepEqual, deepMerge, diffDirty, getPath, humanize, interpolate, sameLeafValue, setIn, } from './utils';
|
|
8
8
|
const EMPTY = Object.freeze([]);
|
|
9
9
|
/** Equal item by item (`sameLeafValue`: arrays of scalars compare by content) */
|
|
10
10
|
function sameDeps(a, b) {
|
|
@@ -335,6 +335,9 @@ export class FormoStore {
|
|
|
335
335
|
// Like formo-vue: the given values over the initial ones (a partial reset keeps the other fields' initial values)
|
|
336
336
|
this.replaceValues(next ? fitValues(this.root, deepMerge(deepClone(this.baseline), deepClone(next)), this.config) : deepClone(this.baseline));
|
|
337
337
|
}
|
|
338
|
+
getDirty() {
|
|
339
|
+
return diffDirty(this.values, this.baseline);
|
|
340
|
+
}
|
|
338
341
|
touch(path) {
|
|
339
342
|
if (this.touched[path])
|
|
340
343
|
return;
|
|
@@ -819,6 +822,7 @@ function createController(store) {
|
|
|
819
822
|
val,
|
|
820
823
|
load: (values) => store.load(values),
|
|
821
824
|
reset: (values) => store.reset(values),
|
|
825
|
+
getDirty: () => store.getDirty(),
|
|
822
826
|
setErrors: (errors) => store.setErrors(errors),
|
|
823
827
|
validate: () => store.validate(),
|
|
824
828
|
submit: (handler) => store.submit(handler),
|
package/dist/core/utils.d.ts
CHANGED
|
@@ -33,3 +33,5 @@ export declare function setIn<T>(obj: T, path: string | Array<string | number>,
|
|
|
33
33
|
export declare function sameLeafValue(a: unknown, b: unknown): boolean;
|
|
34
34
|
/** `a.b[2].c` → `['a.b[2]', 'a.b', 'a', '']` — every group above a path, nearest first */
|
|
35
35
|
export declare function ancestorPaths(path: string): string[];
|
|
36
|
+
/** Return a nested object containing only entries that differ from `base` */
|
|
37
|
+
export declare function diffDirty(current: Record<string, any>, base: Record<string, any>): Record<string, unknown>;
|
package/dist/core/utils.js
CHANGED
|
@@ -204,3 +204,24 @@ export function ancestorPaths(path) {
|
|
|
204
204
|
out.push('');
|
|
205
205
|
return out;
|
|
206
206
|
}
|
|
207
|
+
const UNCHANGED = Symbol('unchanged');
|
|
208
|
+
function diffDirtyNode(current, base) {
|
|
209
|
+
if (deepEqual(current, base))
|
|
210
|
+
return UNCHANGED;
|
|
211
|
+
if (isPlainObject(current) && isPlainObject(base)) {
|
|
212
|
+
const out = {};
|
|
213
|
+
const keys = new Set([...Object.keys(current), ...Object.keys(base)]);
|
|
214
|
+
for (const key of keys) {
|
|
215
|
+
const diff = diffDirtyNode(current[key], base[key]);
|
|
216
|
+
if (diff !== UNCHANGED)
|
|
217
|
+
out[key] = diff;
|
|
218
|
+
}
|
|
219
|
+
return Object.keys(out).length ? out : UNCHANGED;
|
|
220
|
+
}
|
|
221
|
+
return deepClone(current);
|
|
222
|
+
}
|
|
223
|
+
/** Return a nested object containing only entries that differ from `base` */
|
|
224
|
+
export function diffDirty(current, base) {
|
|
225
|
+
const diff = diffDirtyNode(current, base);
|
|
226
|
+
return diff === UNCHANGED ? {} : diff;
|
|
227
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -390,6 +390,8 @@ export interface FormoController {
|
|
|
390
390
|
val: (path: string, value?: unknown) => any;
|
|
391
391
|
load: (values: Record<string, any>) => void;
|
|
392
392
|
reset: (values?: Record<string, any>) => void;
|
|
393
|
+
/** Return a nested object containing only fields whose current value differs from the baseline */
|
|
394
|
+
getDirty: () => Record<string, unknown>;
|
|
393
395
|
/**
|
|
394
396
|
* Set errors, by path — server keys work too (`phones.0.number`), and nested
|
|
395
397
|
* objects (`{ address: { zip: '…', ':self': '…' } }`)
|