@hzab/utils 1.0.5 → 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 CHANGED
@@ -1,3 +1,11 @@
1
+ # @hzab/utils@1.0.7
2
+
3
+ feat: formily schema clone fn
4
+
5
+ # @hzab/utils@1.0.6
6
+
7
+ feat: formily global components management
8
+
1
9
  # @hzab/utils@1.0.5
2
10
 
3
11
  fix: upload 文件名简化
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/utils",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "utils",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -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
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * formily 全局组件
3
+ * 项目初始化时进行 formily 组件的挂载,解决 form render 组件日益庞大的问题。
4
+ */
5
+ window.formilyGlobalComponents = {};
6
+
7
+ /**
8
+ * 获取全局 formily 组件对象
9
+ * @returns {Object} formilyGlobalComponents
10
+ */
11
+ export const getFormilyGlobalComponents = function () {
12
+ return window.formilyGlobalComponents || {};
13
+ };
14
+
15
+ /**
16
+ * 批量设置全局 formily 组件
17
+ * @param {Object} components
18
+ * @returns {Object} formilyGlobalComponents
19
+ */
20
+ export const setFormilyGlobalComponents = function (components) {
21
+ Object.assign(window.formilyGlobalComponents, components);
22
+ return window.formilyGlobalComponents;
23
+ };
24
+
25
+ /**
26
+ * 单个设置全局 formily 组件
27
+ * @returns {Object} formilyGlobalComponents
28
+ */
29
+ /**
30
+ * 单个设置全局 formily 组件
31
+ * @param {string} key
32
+ * @param {ReactComponent} component
33
+ * @returns {Object} formilyGlobalComponents
34
+ */
35
+ export const setFormilyGlobalComponent = function (key, component) {
36
+ window.formilyGlobalComponents[key] = component;
37
+ return window.formilyGlobalComponents;
38
+ };
39
+
40
+ /**
41
+ * 单个删除全局 formily 组件
42
+ * @param {string} key
43
+ * @returns {Object} formilyGlobalComponents
44
+ */
45
+ export const rmFormilyGlobalComponents = function (key) {
46
+ delete window.formilyGlobalComponents[key];
47
+ return window.formilyGlobalComponents;
48
+ };