Skip to content

Commit d09bcee

Browse files
committed
fix(catalogue): remove some function to utils
1 parent 1cb0c29 commit d09bcee

File tree

8 files changed

+208
-143
lines changed

8 files changed

+208
-143
lines changed

src/catalogue/components/catalogue.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type readOnlyTab = readonly Tab[];
1717
type TabKey<T extends readOnlyTab> = T[number]['key'];
1818

1919
interface NormalCatalogueProps<U extends Record<string, any> = {}>
20-
extends Pick<IBlockHeaderProps, 'tooltip' | 'addonAfter' | 'addonBefore' | 'title'>,
20+
extends Partial<Pick<IBlockHeaderProps, 'tooltip' | 'addonAfter' | 'addonBefore' | 'title'>>,
2121
ICatalogueTree<U> {
2222
showSearch?: boolean;
2323
edit?: boolean;

src/catalogue/demos/async.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React, { useEffect } from 'react';
2+
import { Catalogue } from 'dt-react-component';
3+
4+
import { useTreeData } from '../useTreeData';
5+
import { insertChildIntoNode } from '../utils';
6+
7+
interface DataNode {
8+
title: string;
9+
key: string;
10+
isLeaf?: boolean;
11+
children?: DataNode[];
12+
}
13+
14+
const initTreeData: DataNode[] = [
15+
{ title: 'Expand to load', key: '0' },
16+
{ title: 'Expand to load', key: '1' },
17+
{ title: 'Tree Node', key: '2', isLeaf: true },
18+
];
19+
20+
const App: React.FC = () => {
21+
const treeData = useTreeData();
22+
23+
useEffect(() => {
24+
treeData.onChange(initTreeData);
25+
}, []);
26+
27+
const onLoadData = ({ key, children }: any) =>
28+
new Promise<void>((resolve) => {
29+
if (children) {
30+
resolve();
31+
return;
32+
}
33+
setTimeout(() => {
34+
const newData = insertChildIntoNode(treeData.data, key, [
35+
{ title: 'Child Node', key: `${key}-0` },
36+
{ title: 'Child Node', key: `${key}-1` },
37+
]);
38+
treeData.onChange(newData);
39+
resolve();
40+
}, 1000);
41+
});
42+
43+
return <Catalogue loadData={onLoadData} treeData={treeData.data} />;
44+
};
45+
46+
export default App;

src/catalogue/demos/config.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import { cloneDeep } from 'lodash';
66
import shortid from 'shortid';
77

88
import { CatalogueProps } from '../components/catalogue';
9+
import { ITreeNode, useTreeData } from '../useTreeData';
910
import {
1011
appendNodeByKey,
1112
findNodeByKey,
1213
findParentNodeByKey,
13-
ITreeNode,
1414
removeEditNode,
1515
removeNodeByKey,
1616
updateTreeNodeEdit,
17-
useTreeData,
18-
} from '../useTreeData';
17+
} from '../utils';
1918

2019
const DEFAULT_DATA: ITreeNode<IData>[] = [
2120
{

src/catalogue/demos/drag.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import { cloneDeep } from 'lodash';
66
import shortid from 'shortid';
77

88
import { CatalogueProps } from '../components/catalogue';
9+
import { ITreeNode, useTreeData } from '../useTreeData';
910
import {
1011
appendNodeByKey,
1112
findNodeByKey,
1213
findParentNodeByKey,
13-
ITreeNode,
1414
removeEditNode,
1515
removeNodeByKey,
1616
updateTreeNodeEdit,
17-
useTreeData,
18-
} from '../useTreeData';
17+
} from '../utils';
1918

2019
interface IData {
2120
edit?: boolean;

src/catalogue/demos/operator.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import { Catalogue } from 'dt-react-component';
55
import { cloneDeep } from 'lodash';
66
import shortid from 'shortid';
77

8+
import { ITreeNode, useTreeData } from '../useTreeData';
89
import {
910
appendNodeByKey,
1011
findNodeByKey,
1112
findParentNodeByKey,
12-
ITreeNode,
1313
removeEditNode,
1414
removeNodeByKey,
1515
updateTreeNodeEdit,
16-
useTreeData,
17-
} from '../useTreeData';
16+
} from '../utils';
1817

1918
const DEFAULT_DATA = [
2019
{

src/catalogue/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ demo:
2222
<code src="./demos/drag.tsx">可拖拽的目录树</code>
2323
<code src="./demos/config.tsx">配置操作项的目录树</code>
2424
<code src="./demos/tabs.tsx">配置操作项的目录树</code>
25+
<code src="./demos/async.tsx">异步加载数据</code>
2526

2627
## API
2728

src/catalogue/useTreeData.tsx

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ export type ITreeNode<U = {}> = Omit<DataNode, 'children' | 'title'> & {
1010

1111
export const useTreeData = <U extends Record<string, any> = {}>(): {
1212
data: ITreeNode<U>[];
13-
loading: boolean;
1413
expandedKeys: CatalogueProps['expandedKeys'];
1514
onChange: (node: ITreeNode<U>[]) => void;
16-
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
1715
onExpand: React.Dispatch<React.SetStateAction<CatalogueProps['expandedKeys']>>;
1816
} => {
1917
const [data, setData] = useState<ITreeNode<U>[]>([]);
20-
const [loading, setLoading] = useState(false);
2118
const [expandedKeys, setExpandedKeys] = useState<CatalogueProps['expandedKeys']>([]);
2219

2320
const onChange = (data: ITreeNode<U>[]) => {
@@ -26,138 +23,8 @@ export const useTreeData = <U extends Record<string, any> = {}>(): {
2623

2724
return {
2825
data,
29-
loading,
3026
expandedKeys,
3127
onChange,
32-
setLoading,
3328
onExpand: setExpandedKeys,
3429
};
3530
};
36-
37-
/**
38-
* 查找 key 对应的节点
39-
* @param data 遍历的数组
40-
* @param key 当前 key 值
41-
* @returns 找到的对应节点
42-
*/
43-
export const findNodeByKey = <U,>(
44-
data: ITreeNode<U>[],
45-
key: ITreeNode<U>['key']
46-
): ITreeNode<U> | null => {
47-
for (const node of data) {
48-
if (node.key === key) {
49-
return node;
50-
}
51-
if (node.children) {
52-
const result = findNodeByKey(node.children, key);
53-
if (result) return result;
54-
}
55-
}
56-
return null;
57-
};
58-
59-
/**
60-
* 更新 key 对应节点为编辑状态
61-
* @param data 遍历的数组
62-
* @param key 当前 key 值
63-
* @returns 更新之后 data
64-
*/
65-
export const updateTreeNodeEdit = <U,>(
66-
data: ITreeNode<U>[],
67-
key: ITreeNode<U>['key']
68-
): ITreeNode<U>[] => {
69-
return data.map((node) => {
70-
if (node.key === key) {
71-
return { ...node, edit: true };
72-
}
73-
if (node.children) {
74-
return { ...node, children: updateTreeNodeEdit(node.children, key) };
75-
}
76-
return node;
77-
});
78-
};
79-
80-
/**
81-
* 查找 key 对应的父级节点
82-
* @param data 遍历的数组
83-
* @param key 当前 key 值
84-
* @returns 当前找到父级节点
85-
*/
86-
export const findParentNodeByKey = <U extends { edit?: boolean }>(
87-
data: ITreeNode<U>[],
88-
key: ITreeNode<U>['key']
89-
): ITreeNode<U> | null => {
90-
for (const node of data) {
91-
if (node.children?.some((child) => child.key === key)) {
92-
return node;
93-
}
94-
// 如果有子节点,递归查找
95-
if (node.children) {
96-
const parent = findParentNodeByKey(node.children, key);
97-
if (parent) return parent;
98-
}
99-
}
100-
return null;
101-
};
102-
103-
/**
104-
* 在 key 节点中添加子节点
105-
* @param data 遍历的数组
106-
* @param key 当前 key 值
107-
* @returns 插入新数据之后的 data
108-
*/
109-
export const appendNodeByKey = <U extends { edit?: boolean }>(
110-
data: ITreeNode<U>[],
111-
key: ITreeNode<U>['key']
112-
): ITreeNode<U>[] => {
113-
const newNode = { key: 'new_', title: '', edit: true };
114-
return data.map((node) => {
115-
if (node.key === key) {
116-
const updatedChildren = node.children ? [...node.children, newNode] : [newNode];
117-
return { ...node, children: updatedChildren };
118-
}
119-
if (node.children) {
120-
return { ...node, children: appendNodeByKey(node.children, key) };
121-
}
122-
return node;
123-
});
124-
};
125-
126-
/**
127-
* 移除 key 节点
128-
* @param data 遍历的数组
129-
* @param key 当前 key 值
130-
* @returns 删除数据之后的 data
131-
*/
132-
export const removeNodeByKey = <U,>(
133-
data: ITreeNode<U>[],
134-
key: ITreeNode<U>['key']
135-
): ITreeNode<U>[] => {
136-
return data
137-
.filter((node) => node.key !== key)
138-
.map((node) => {
139-
if (node.children) {
140-
return {
141-
...node,
142-
children: removeNodeByKey(node.children, key),
143-
};
144-
}
145-
return node;
146-
});
147-
};
148-
149-
/**
150-
* 移除 edit 为 true 的节点
151-
* @param treeData
152-
* @returns 移除之后的数据
153-
*/
154-
export const removeEditNode = <U extends { edit?: boolean }>(
155-
treeData: ITreeNode<U>[]
156-
): ITreeNode<U>[] => {
157-
return treeData
158-
.filter((node) => !node.edit)
159-
.map((node) => ({
160-
...node,
161-
children: node.children ? removeEditNode(node.children) : undefined,
162-
}));
163-
};

0 commit comments

Comments
 (0)