@ftjs/core 0.5.7 → 1.1.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/index.js +55 -3
- package/dist/utils.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -97,6 +97,43 @@ const setStorage = (key, value, cache) => {
|
|
|
97
97
|
localStorage.setItem(key, JSON.stringify(obj));
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
|
+
const isEqual = (a, b) => {
|
|
101
|
+
if (a === b) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (a == null || b == null) {
|
|
105
|
+
return a === b;
|
|
106
|
+
}
|
|
107
|
+
if (typeof a !== typeof b) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (Number.isNaN(a) && Number.isNaN(b)) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
if (typeof a !== "object") {
|
|
114
|
+
return a === b;
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
117
|
+
return a.length === b.length && a.every((item, index) => isEqual(item, b[index]));
|
|
118
|
+
}
|
|
119
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
if (a instanceof Date && b instanceof Date) {
|
|
123
|
+
return a.getTime() === b.getTime();
|
|
124
|
+
}
|
|
125
|
+
if (a instanceof Date || b instanceof Date) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const keysA = Object.keys(a);
|
|
129
|
+
const keysB = Object.keys(b);
|
|
130
|
+
if (keysA.length !== keysB.length) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return keysA.every(
|
|
134
|
+
(key) => Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key])
|
|
135
|
+
);
|
|
136
|
+
};
|
|
100
137
|
const provideFormKey = Symbol("@ftjs/core-form-provide");
|
|
101
138
|
const useFormInject = () => {
|
|
102
139
|
return inject(provideFormKey);
|
|
@@ -278,6 +315,8 @@ const useForm = (props) => {
|
|
|
278
315
|
const control = column.control;
|
|
279
316
|
if (!watchObj && !control) return;
|
|
280
317
|
const field = getField(column);
|
|
318
|
+
const watchArr = column.fields ?? [column.field];
|
|
319
|
+
const isSingleWatch = watchArr.length === 1;
|
|
281
320
|
const cancel = [];
|
|
282
321
|
if (typeof watchObj === "function") {
|
|
283
322
|
watchObj = {
|
|
@@ -288,9 +327,13 @@ const useForm = (props) => {
|
|
|
288
327
|
cancel.push(
|
|
289
328
|
watch(
|
|
290
329
|
() => {
|
|
291
|
-
return get(form.value,
|
|
330
|
+
return watchArr.map((f) => get(form.value, f));
|
|
292
331
|
},
|
|
293
332
|
(val, oldVal) => {
|
|
333
|
+
if (isSingleWatch) {
|
|
334
|
+
val = val == null ? void 0 : val[0];
|
|
335
|
+
oldVal = oldVal == null ? void 0 : oldVal[0];
|
|
336
|
+
}
|
|
294
337
|
watchObj.handler({ val, oldVal, form: form.value });
|
|
295
338
|
},
|
|
296
339
|
{
|
|
@@ -303,7 +346,7 @@ const useForm = (props) => {
|
|
|
303
346
|
if (control) {
|
|
304
347
|
cancel.push(
|
|
305
348
|
watch(
|
|
306
|
-
() => get(form.value,
|
|
349
|
+
() => watchArr.map((f) => get(form.value, f)),
|
|
307
350
|
(val) => {
|
|
308
351
|
control.forEach(({ field: targetField, value }) => {
|
|
309
352
|
let show = true;
|
|
@@ -313,7 +356,15 @@ const useForm = (props) => {
|
|
|
313
356
|
val
|
|
314
357
|
});
|
|
315
358
|
} else {
|
|
316
|
-
|
|
359
|
+
if (Array.isArray(value)) {
|
|
360
|
+
if (isSingleWatch) {
|
|
361
|
+
show = value.some((v) => isEqual(v, val[0]));
|
|
362
|
+
} else {
|
|
363
|
+
show = isEqual(value, val);
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
show = isEqual(value, val[0]);
|
|
367
|
+
}
|
|
317
368
|
}
|
|
318
369
|
if (!fieldControlMap.value.has(targetField)) {
|
|
319
370
|
fieldControlMap.value.set(targetField, /* @__PURE__ */ new Map());
|
|
@@ -496,6 +547,7 @@ export {
|
|
|
496
547
|
has,
|
|
497
548
|
isBrowser,
|
|
498
549
|
isEmptyStrOrNull,
|
|
550
|
+
isEqual,
|
|
499
551
|
set,
|
|
500
552
|
setStorage,
|
|
501
553
|
unrefs,
|
package/dist/utils.d.ts
CHANGED
|
@@ -29,3 +29,9 @@ export declare const getStorage: (key: string, cache?: string) => {};
|
|
|
29
29
|
* @param cache 缓存名称
|
|
30
30
|
*/
|
|
31
31
|
export declare const setStorage: (key: string, value: any, cache?: string) => void;
|
|
32
|
+
/**
|
|
33
|
+
* 简单判断两个值是否相等
|
|
34
|
+
*
|
|
35
|
+
* 不考虑循环引用
|
|
36
|
+
*/
|
|
37
|
+
export declare const isEqual: (a: any, b: any) => any;
|