|
1 | 1 | import * as React from 'react';
|
2 |
| -// import { useState } from 'react'; |
| 2 | +import { memo, useEffect, useState } from 'react'; |
3 | 3 | import Tree, { TreeNode } from 'mo/components/tree';
|
4 |
| -import { prefixClaName } from 'mo/common/className'; |
5 |
| -import { codIcon } from 'mo/common/className'; |
| 4 | +import { IMenuItem } from 'mo/components/menu'; |
| 5 | +import { prefixClaName, codIcon } from 'mo/common/className'; |
6 | 6 | import './style.scss';
|
| 7 | + |
| 8 | +export interface ITreeNodeItem { |
| 9 | + title?: string; |
| 10 | + key?: string; |
| 11 | + type?: string; |
| 12 | + contextMenu?: IMenuItem[] |
| 13 | + children?: ITreeNodeItem[] |
| 14 | + readonly id?: string; |
| 15 | + icon?: string | React.ReactNode; |
| 16 | +} |
7 | 17 | interface ITreeProps {
|
8 |
| - isActive?: boolean; |
| 18 | + data: ITreeNodeItem[] |
9 | 19 | }
|
10 |
| - |
11 |
| -// const initState = { |
12 |
| -// } |
13 |
| -export const TreeView: React.FunctionComponent<ITreeProps> = ( |
14 |
| - ITreeProps |
| 20 | +const TreeView: React.FunctionComponent<ITreeProps> = ( |
| 21 | + props: ITreeProps |
15 | 22 | ) => {
|
| 23 | + const { |
| 24 | + data, |
| 25 | + } = props; |
| 26 | + const [treeData, setTreeData] = useState<ITreeNodeItem[]>(data) |
| 27 | + |
| 28 | + /** |
| 29 | + * Refer to antd for details |
| 30 | + */ |
| 31 | + const onDrop = info => { |
| 32 | + console.log(info); |
| 33 | + const dropKey = info.node.props.eventKey; |
| 34 | + const dragKey = info.dragNode.props.eventKey; |
| 35 | + const dropPos = info.node.props.pos.split('-'); |
| 36 | + const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]); |
| 37 | + |
| 38 | + const loopTree = (data, key, callback) => { |
| 39 | + data.forEach((item, index, arr) => { |
| 40 | + if (item.key === key) { |
| 41 | + return callback(item, index, arr); |
| 42 | + } |
| 43 | + if (item.children) { |
| 44 | + return loopTree(item.children, key, callback); |
| 45 | + } |
| 46 | + }); |
| 47 | + }; |
| 48 | + const data = [...treeData]; |
| 49 | + |
| 50 | + // Find dragObject |
| 51 | + let dragObj; |
| 52 | + loopTree(data, dragKey, (item, index, arr) => { |
| 53 | + arr.splice(index, 1); |
| 54 | + dragObj = item; |
| 55 | + }); |
| 56 | + |
| 57 | + if (!info.dropToGap) { |
| 58 | + // Drop on the content |
| 59 | + loopTree(data, dropKey, item => { |
| 60 | + item.children = item.children || []; |
| 61 | + item.children.push(dragObj); |
| 62 | + }); |
| 63 | + } else if ( |
| 64 | + (info.node.props.children || []).length > 0 && |
| 65 | + info.node.props.expanded && |
| 66 | + dropPosition === 1 |
| 67 | + ) { |
| 68 | + loopTree(data, dropKey, item => { |
| 69 | + item.children = item.children || []; |
| 70 | + item.children.unshift(dragObj); |
| 71 | + }); |
| 72 | + } else { |
| 73 | + let ar, i; |
| 74 | + loopTree(data, dropKey, (item, index, arr) => { |
| 75 | + ar = arr; i = index; |
| 76 | + }); |
| 77 | + if (dropPosition === -1) { |
| 78 | + ar.splice(i, 0, dragObj); |
| 79 | + } else { |
| 80 | + ar.splice(i + 1, 0, dragObj); |
| 81 | + } |
| 82 | + } |
| 83 | + console.log('data', data) |
| 84 | + setTreeData(data); |
| 85 | + }; |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + return () => { |
| 89 | + console.log('clean effect') |
| 90 | + } |
| 91 | + }, data) |
| 92 | + |
| 93 | + const renderTreeNodes = data => |
| 94 | + data?.map(item => { |
| 95 | + return <TreeNode data={item} title={item.title} key={item.key} icon={codIcon(item.icon)}> |
| 96 | + {item.children && renderTreeNodes(item.children)} |
| 97 | + </TreeNode> |
| 98 | + }); |
16 | 99 | return (
|
| 100 | + /** |
| 101 | + * TODO: contextMenu、line |
| 102 | + */ |
17 | 103 | <div className={prefixClaName('tree', 'sidebar')}>
|
18 | 104 | <Tree
|
19 | 105 | prefixCls='rc-tree'
|
20 | 106 | draggable
|
| 107 | + onDrop={onDrop} |
21 | 108 | switcherIcon={codIcon('codicon-chevron-right')}
|
| 109 | + onRightClick={({ event, node }) => { |
| 110 | + console.log('onRightClick', event, node) |
| 111 | + }} |
| 112 | + // onSelect={onClickItem} |
22 | 113 | >
|
23 |
| - <TreeNode title='parent' key='parent'> |
24 |
| - <TreeNode title='child' key='child'> |
25 |
| - <TreeNode title='child3' key='child3'> |
26 |
| - <TreeNode icon={({ selected }) => codIcon('codicon-symbol-file')} title='child5' key='child5'></TreeNode> |
27 |
| - </TreeNode> |
28 |
| - <TreeNode title='child4' key='child4'></TreeNode> |
29 |
| - </TreeNode> |
30 |
| - <TreeNode title='child1' key='child1'></TreeNode> |
31 |
| - </TreeNode> |
| 114 | + {renderTreeNodes(treeData)} |
32 | 115 | </Tree>
|
33 | 116 | </div>
|
34 | 117 | );
|
35 | 118 | };
|
| 119 | + |
| 120 | +export default memo(TreeView) |
0 commit comments