-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
164 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,120 @@ | ||
import * as React from 'react'; | ||
// import { useState } from 'react'; | ||
import { memo, useEffect, useState } from 'react'; | ||
import Tree, { TreeNode } from 'mo/components/tree'; | ||
import { prefixClaName } from 'mo/common/className'; | ||
import { codIcon } from 'mo/common/className'; | ||
import { IMenuItem } from 'mo/components/menu'; | ||
import { prefixClaName, codIcon } from 'mo/common/className'; | ||
import './style.scss'; | ||
|
||
export interface ITreeNodeItem { | ||
title?: string; | ||
key?: string; | ||
type?: string; | ||
contextMenu?: IMenuItem[] | ||
children?: ITreeNodeItem[] | ||
readonly id?: string; | ||
icon?: string | React.ReactNode; | ||
} | ||
interface ITreeProps { | ||
isActive?: boolean; | ||
data: ITreeNodeItem[] | ||
} | ||
|
||
// const initState = { | ||
// } | ||
export const TreeView: React.FunctionComponent<ITreeProps> = ( | ||
ITreeProps | ||
const TreeView: React.FunctionComponent<ITreeProps> = ( | ||
props: ITreeProps | ||
) => { | ||
const { | ||
data, | ||
} = props; | ||
const [treeData, setTreeData] = useState<ITreeNodeItem[]>(data) | ||
|
||
/** | ||
* Refer to antd for details | ||
*/ | ||
const onDrop = info => { | ||
console.log(info); | ||
const dropKey = info.node.props.eventKey; | ||
const dragKey = info.dragNode.props.eventKey; | ||
const dropPos = info.node.props.pos.split('-'); | ||
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]); | ||
|
||
const loopTree = (data, key, callback) => { | ||
data.forEach((item, index, arr) => { | ||
if (item.key === key) { | ||
return callback(item, index, arr); | ||
} | ||
if (item.children) { | ||
return loopTree(item.children, key, callback); | ||
} | ||
}); | ||
}; | ||
const data = [...treeData]; | ||
|
||
// Find dragObject | ||
let dragObj; | ||
loopTree(data, dragKey, (item, index, arr) => { | ||
arr.splice(index, 1); | ||
dragObj = item; | ||
}); | ||
|
||
if (!info.dropToGap) { | ||
// Drop on the content | ||
loopTree(data, dropKey, item => { | ||
item.children = item.children || []; | ||
item.children.push(dragObj); | ||
}); | ||
} else if ( | ||
(info.node.props.children || []).length > 0 && | ||
info.node.props.expanded && | ||
dropPosition === 1 | ||
) { | ||
loopTree(data, dropKey, item => { | ||
item.children = item.children || []; | ||
item.children.unshift(dragObj); | ||
}); | ||
} else { | ||
let ar, i; | ||
loopTree(data, dropKey, (item, index, arr) => { | ||
ar = arr; i = index; | ||
}); | ||
if (dropPosition === -1) { | ||
ar.splice(i, 0, dragObj); | ||
} else { | ||
ar.splice(i + 1, 0, dragObj); | ||
} | ||
} | ||
console.log('data', data) | ||
setTreeData(data); | ||
}; | ||
|
||
useEffect(() => { | ||
return () => { | ||
console.log('clean effect') | ||
} | ||
}, data) | ||
|
||
const renderTreeNodes = data => | ||
data?.map(item => { | ||
return <TreeNode data={item} title={item.title} key={item.key} icon={codIcon(item.icon)}> | ||
{item.children && renderTreeNodes(item.children)} | ||
</TreeNode> | ||
}); | ||
return ( | ||
/** | ||
* TODO: contextMenu、line | ||
*/ | ||
<div className={prefixClaName('tree', 'sidebar')}> | ||
<Tree | ||
prefixCls='rc-tree' | ||
draggable | ||
onDrop={onDrop} | ||
switcherIcon={codIcon('codicon-chevron-right')} | ||
onRightClick={({ event, node }) => { | ||
console.log('onRightClick', event, node) | ||
}} | ||
// onSelect={onClickItem} | ||
> | ||
<TreeNode title='parent' key='parent'> | ||
<TreeNode title='child' key='child'> | ||
<TreeNode title='child3' key='child3'> | ||
<TreeNode icon={({ selected }) => codIcon('codicon-symbol-file')} title='child5' key='child5'></TreeNode> | ||
</TreeNode> | ||
<TreeNode title='child4' key='child4'></TreeNode> | ||
</TreeNode> | ||
<TreeNode title='child1' key='child1'></TreeNode> | ||
</TreeNode> | ||
{renderTreeNodes(treeData)} | ||
</Tree> | ||
</div> | ||
); | ||
}; | ||
|
||
export default memo(TreeView) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export const data = [ | ||
{ | ||
title: 'folder', | ||
key: 'folder', | ||
type: 'folder', | ||
id: 'abc', | ||
children: [ | ||
{ | ||
// id: 'abc', | ||
title: 'abccccccccc', | ||
key: 'abccccccccc', | ||
contextMenu: [ | ||
{ | ||
id: 'CommandPalette', | ||
name: 'Command Palette...', | ||
title: 'Command Palette', | ||
}, | ||
{ | ||
id: 'Settings', | ||
name: 'Settings', | ||
title: 'Settings', | ||
}, | ||
{ | ||
id: 'ColorTheme', | ||
name: 'Color Theme', | ||
title: 'Color Theme', | ||
} | ||
], | ||
children: [ | ||
{ title: 'test.txt', key: 'test.txt', type: 'file', icon: 'codicon-symbol-file' }, | ||
{ title: 'test.js', key: 'test.js', type: 'file', icon: 'codicon-file-binary' }, | ||
{ title: 'test.html', key: 'test.html', type: 'file', icon: 'codicon-file-code' }, | ||
], | ||
}, | ||
{ | ||
title: 'xyz', | ||
key: 'xyz', | ||
type: 'folder', | ||
children: [ | ||
{ title: 'test.pdf', key: 'test.pdf', type: 'file', icon: 'codicon-file-pdf' }, | ||
{ title: 'test.media', key: 'test.media', type: 'file', icon: 'codicon-file-media' }, | ||
{ title: 'test.zip', key: 'test.zip', type: 'file', icon: 'codicon-file-zip' }, | ||
], | ||
}, | ||
{ | ||
title: 'file.yaml', | ||
key: 'file.yaml', | ||
type: 'file', | ||
}, | ||
], | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters