@hzab/utils 1.0.6 → 1.0.7
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/changelog.md +5 -1
- package/package.json +1 -1
- package/src/formily/cloneSchema.ts +63 -0
package/changelog.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 检测是否为原生 HTML DOM 节点
|
|
5
|
+
* @param {any} node 待检测对象
|
|
6
|
+
* @returns {boolean} 是否为原生 DOM 节点
|
|
7
|
+
*/
|
|
8
|
+
function isHtmlDomNode(node) {
|
|
9
|
+
// 先判断浏览器环境,避免 SSR 报错;兼容 Node/HTMLElement 类型
|
|
10
|
+
return !!(typeof window !== "undefined" && node && (node instanceof HTMLElement || node instanceof Node));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 增强版 Formily Schema 克隆函数
|
|
15
|
+
* 解决 lodash cloneDeep 丢失 React DOM/原生 DOM 的问题,兼容多种特殊类型
|
|
16
|
+
* @param {object} schema 待克隆的 Formily Schema
|
|
17
|
+
* @returns {object} 克隆后的 Schema
|
|
18
|
+
*/
|
|
19
|
+
export function cloneSchema(schema) {
|
|
20
|
+
// 1. 基础类型(null/undefined/字符串/数字/布尔)直接返回
|
|
21
|
+
if (schema === null || typeof schema !== "object") {
|
|
22
|
+
return schema;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 2. React 虚拟DOM:直接保留原引用,不拷贝
|
|
26
|
+
if (React.isValidElement(schema)) {
|
|
27
|
+
return schema;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 3. 原生 HTML DOM 节点:直接保留原引用
|
|
31
|
+
if (isHtmlDomNode(schema)) {
|
|
32
|
+
return schema;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 4. 函数:Formily Schema 中的校验/格式化函数等,保留原引用
|
|
36
|
+
if (typeof schema === "function") {
|
|
37
|
+
return schema;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 5. 日期对象:创建新的 Date 实例,保证值相同但引用不同
|
|
41
|
+
if (schema instanceof Date) {
|
|
42
|
+
return new Date(schema.getTime());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 6. 正则对象:创建新的 RegExp 实例,保留源和标志
|
|
46
|
+
if (schema instanceof RegExp) {
|
|
47
|
+
return new RegExp(schema.source, schema.flags);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 7. 数组:递归克隆每一项
|
|
51
|
+
if (Array.isArray(schema)) {
|
|
52
|
+
return schema.map((item) => cloneSchema(item));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 8. 普通对象:递归克隆自有属性
|
|
56
|
+
const clonedObj = {};
|
|
57
|
+
for (const key in schema) {
|
|
58
|
+
if (Object.prototype.hasOwnProperty.call(schema, key)) {
|
|
59
|
+
clonedObj[key] = cloneSchema(schema[key]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return clonedObj;
|
|
63
|
+
}
|