@hzab/form-render 0.5.0 → 0.5.2

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.
@@ -11,7 +11,7 @@
11
11
  [ , 22, ]
12
12
  [ , 23, ]
13
13
  */
14
- export function handleDataSource(treeData = [], opt = {}) {
14
+ export function handleDataSource(treeData = [], opt: optT = {}) {
15
15
  const { resList, maxCol } = transformTreeData(treeData, opt);
16
16
  resList.forEach((row) => {
17
17
  fillArrByPush(maxCol, row);
@@ -24,10 +24,13 @@ export type optT = {
24
24
  preCol?: number;
25
25
  // 最后一列是否合并到一个单元格
26
26
  mergeLast?: boolean;
27
- // children Key
28
- childrenKey?: string;
29
27
  // 当前递归的层级
30
28
  level?: number;
29
+ fieldNames?: {
30
+ value?: string;
31
+ label?: string;
32
+ children?: string;
33
+ };
31
34
  };
32
35
 
33
36
  /**
@@ -44,7 +47,8 @@ export type optT = {
44
47
  [ , 23, ]
45
48
  */
46
49
  export function transformTreeData(treeData = [], opt: optT = {}) {
47
- const { resList = [], preCol = 0, mergeLast, level = 0, childrenKey = "children" } = opt || {};
50
+ const { resList = [], preCol = 0, mergeLast, level = 0, fieldNames } = opt || {};
51
+ const { value = "value", children = "children", label = "label" } = fieldNames || {};
48
52
  let maxCol = preCol + 1;
49
53
  // 统计当前项包含的最大行数(当前项的行数等于所有子项行数之和),用于合并行的值
50
54
  let maxRow = 0;
@@ -52,14 +56,14 @@ export function transformTreeData(treeData = [], opt: optT = {}) {
52
56
  // 是否有子项
53
57
  let hasChildren = false;
54
58
  treeData.forEach((it) => {
55
- if (it && Array.isArray(it[childrenKey]) && it[childrenKey].length > 0) {
59
+ if (it && Array.isArray(it[children]) && it[children].length > 0) {
56
60
  hasChildren = true;
57
61
  }
58
62
  });
59
63
  let lastRowIdx = resList.length > 0 ? resList.length - 1 : 0;
60
64
  // mergeLast === true && hasChildren === false && level > 0 时,合并当前数据
61
65
  if (mergeLast === true && !hasChildren && level > 0) {
62
- resList[lastRowIdx].push([...treeData]);
66
+ resList[lastRowIdx].push([...treeData?.map((it) => ({ ...it, label: it[label], value: it[value] }))]);
63
67
  return {
64
68
  resList,
65
69
  maxCol,
@@ -69,9 +73,9 @@ export function transformTreeData(treeData = [], opt: optT = {}) {
69
73
  }
70
74
 
71
75
  treeData.forEach((it, i) => {
72
- const item = { ...it };
73
- const children = item[childrenKey] || [];
74
- const childrenLen = children?.length;
76
+ const item = { ...it, value: it[value], label: it[label] };
77
+ const childList = item[children] || [];
78
+ const childLen = childList?.length;
75
79
  // 当 mergeLast !== true && children 长度大于1时,子项下标大于 0 的项需要另起一行,同时补充之前空的列。
76
80
  if (i > 0) {
77
81
  resList.push(fillArrByPush(preCol, [], { content: false }));
@@ -82,8 +86,8 @@ export function transformTreeData(treeData = [], opt: optT = {}) {
82
86
  resList[lastRowIdx] = [];
83
87
  }
84
88
  resList[lastRowIdx].push(item);
85
- if (childrenLen > 0) {
86
- const res = transformTreeData(children, {
89
+ if (childLen > 0) {
90
+ const res = transformTreeData(childList, {
87
91
  ...opt,
88
92
  resList,
89
93
  preCol: preCol + 1,
@@ -109,6 +113,13 @@ export function transformTreeData(treeData = [], opt: optT = {}) {
109
113
  };
110
114
  }
111
115
 
116
+ /**
117
+ * 填充数组
118
+ * @param len
119
+ * @param arr
120
+ * @param opt
121
+ * @returns
122
+ */
112
123
  export function fillArrByPush(len, arr = [], opt: any = {}) {
113
124
  const { content } = opt || {};
114
125
  if (arr.length < len) {
@@ -0,0 +1,55 @@
1
+ import { Checkbox } from "antd";
2
+
3
+ export const CheckboxTable = (props) => {
4
+ const { data } = props;
5
+ return (
6
+ <table className="tree-checkbox-table">
7
+ <tbody>
8
+ {data?.map((row, rowIdx) => {
9
+ return (
10
+ <tr className={`tree-checkbox-row tree-checkbox-row-${rowIdx}`} key={rowIdx}>
11
+ {row?.map((col, colIdx) => {
12
+ // 隐藏合并项
13
+ if (col === false) {
14
+ return null;
15
+ }
16
+ // 注意:col 存在 undefined 的情况
17
+ const colKey = col?.value;
18
+ return (
19
+ <td
20
+ className={`tree-checkbox-col tree-checkbox-col-${colIdx}`}
21
+ key={colIdx}
22
+ rowSpan={col?._maxRow > 1 ? col?._maxRow : null}
23
+ >
24
+ {Array.isArray(col) ? (
25
+ <>
26
+ {col.map((it, itIdx) => {
27
+ const itKey = (it?.value ?? "") + itIdx;
28
+ return (
29
+ it && (
30
+ <Checkbox value={itKey} key={itKey}>
31
+ {it.label}
32
+ </Checkbox>
33
+ )
34
+ );
35
+ })}
36
+ </>
37
+ ) : (
38
+ col && (
39
+ <Checkbox value={colKey} key={colKey}>
40
+ {col.label}
41
+ </Checkbox>
42
+ )
43
+ )}
44
+ </td>
45
+ );
46
+ })}
47
+ </tr>
48
+ );
49
+ })}
50
+ </tbody>
51
+ </table>
52
+ );
53
+ };
54
+
55
+ export default CheckboxTable;
@@ -1,9 +1,12 @@
1
1
  import { useMemo } from "react";
2
- import { Checkbox } from "antd";
2
+ import { Checkbox, Tabs } from "antd";
3
3
  import { useField } from "@formily/react";
4
4
 
5
+ import CheckboxTable from "./components/CheckboxTable";
5
6
  import { handleDataSource } from "./common/utils";
6
7
 
8
+ const { TabPane } = Tabs;
9
+
7
10
  import "./index.less";
8
11
 
9
12
  export const TreeCheckbox = (props) => {
@@ -15,58 +18,49 @@ export const TreeCheckbox = (props) => {
15
18
  value,
16
19
  onChange,
17
20
  mergeLast = true,
18
- labelKey = "label",
19
- valueKey = "value",
20
- childrenKey = "children",
21
+ fieldNames = {
22
+ // 兼容老的字段,后续废弃
23
+ label: props.labelKey ?? "label",
24
+ value: props.valueKey ?? "value",
25
+ children: props.childrenKey ?? "children",
26
+ },
27
+ // 最外层使用 tab
28
+ tabBox = false,
21
29
  } = props;
22
30
 
23
- const showDataSource = useMemo(() => handleDataSource(dataSource, { mergeLast, childrenKey }), [dataSource]);
31
+ const showDataSource = useMemo(() => {
32
+ if (tabBox) {
33
+ return dataSource?.map((d) => {
34
+ return {
35
+ ...d,
36
+ tableData: handleDataSource(d?.[fieldNames?.children || "children"], { mergeLast, fieldNames }),
37
+ };
38
+ });
39
+ } else {
40
+ return handleDataSource(dataSource, { mergeLast, fieldNames });
41
+ }
42
+ }, [dataSource]);
43
+
44
+ let content = null;
45
+
46
+ if (tabBox) {
47
+ const { label: labelKey, value: valueKey } = fieldNames || {};
48
+ content = (
49
+ <Tabs defaultActiveKey="0">
50
+ {showDataSource?.map((it, i) => (
51
+ <TabPane tab={it[labelKey]} key={(valueKey || "") + i}>
52
+ <CheckboxTable data={it.tableData} />
53
+ </TabPane>
54
+ ))}
55
+ </Tabs>
56
+ );
57
+ } else {
58
+ content = <CheckboxTable data={showDataSource} />;
59
+ }
24
60
 
25
61
  return (
26
62
  <Checkbox.Group className="tree-checkbox" disabled={disabled || readOnly} value={value} onChange={onChange}>
27
- <table className="tree-checkbox-table">
28
- <tbody>
29
- {showDataSource?.map((row, rowIdx) => {
30
- return (
31
- <tr className={`tree-checkbox-row tree-checkbox-row-${rowIdx}`} key={rowIdx}>
32
- {row?.map((col, colIdx) => {
33
- // 隐藏合并项
34
- if (col === false) {
35
- return null;
36
- }
37
- return (
38
- <td
39
- className={`tree-checkbox-col tree-checkbox-col-${colIdx}`}
40
- key={colIdx}
41
- rowSpan={col?._maxRow > 1 ? col?._maxRow : null}
42
- >
43
- {Array.isArray(col) ? (
44
- <>
45
- {col.map((it) => {
46
- return (
47
- it && (
48
- <Checkbox value={it[valueKey]} key={it[valueKey]}>
49
- {it[labelKey]}
50
- </Checkbox>
51
- )
52
- );
53
- })}
54
- </>
55
- ) : (
56
- col && (
57
- <Checkbox value={col[valueKey]} key={col[valueKey]}>
58
- {col[labelKey]}
59
- </Checkbox>
60
- )
61
- )}
62
- </td>
63
- );
64
- })}
65
- </tr>
66
- );
67
- })}
68
- </tbody>
69
- </table>
63
+ {content}
70
64
  </Checkbox.Group>
71
65
  );
72
66
  };