Skip to content

Commit

Permalink
feat: compoent ui opti
Browse files Browse the repository at this point in the history
compoent ui opti
  • Loading branch information
zhangtengjin committed Nov 19, 2020
1 parent b7d37a9 commit e120d46
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 46 deletions.
4 changes: 1 addition & 3 deletions src/components/collapse/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@
cursor: not-allowed;
}
.rc-collapse-content {
overflow: hidden;
// padding: 0 16px;
overflow: auto;
}
.rc-collapse-content > .rc-collapse-content-box {
font-size: 12px;
margin-top: 8px;
margin-bottom: 8px;
}
.rc-collapse-content-inactive {
Expand Down
1 change: 0 additions & 1 deletion src/components/tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { prefixClaName, classNames } from 'mo/common/className';
export interface ITree {}

interface ITreeProps extends TreeProps {
prefixCls: any;
}

export const Tree: React.FunctionComponent<ITreeProps> = (
Expand Down
22 changes: 3 additions & 19 deletions src/components/tree/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
margin: 0;
padding: 0 0 0 8px;
line-height: 22px;
// border: 1px solid red;
height: 22px;
white-space: nowrap;
list-style: none;
Expand Down Expand Up @@ -57,7 +56,6 @@
display: inline-block;
height: 22px;
margin: 0;
// padding: 1px 3px 0 0;
text-decoration: none;
vertical-align: top;
cursor: pointer;
Expand All @@ -82,7 +80,8 @@
.rc-tree .rc-tree-treenode span.rc-tree-iconEle.rc-tree-icon__customize {
background-image: none;
.codicon {
margin-top: 3px;
margin-top: 4px;
transform: scale(0.8);
}
}
.rc-tree .rc-tree-treenode span.rc-tree-switcher.rc-tree-switcher-noop {
Expand All @@ -91,21 +90,13 @@
}
.rc-tree .rc-tree-treenode span.rc-tree-switcher.rc-tree-switcher_open {
transform: rotate(90deg);
margin-top: -2px;
}
.rc-tree .rc-tree-treenode span.rc-tree-switcher.rc-tree-switcher_close {
}
.rc-tree:not(.rc-tree-show-line) .rc-tree-treenode .rc-tree-switcher-noop {
background: none;
}
.rc-tree.rc-tree-show-line .rc-tree-treenode:not(:last-child) > ul {
background: url('data:image/gif;base64,R0lGODlhCQACAIAAAMzMzP///yH5BAEAAAEALAAAAAAJAAIAAAIEjI9pUAA7') 0 0 repeat-y;
}
.rc-tree.rc-tree-show-line .rc-tree-treenode:not(:last-child) > .rc-tree-switcher-noop {
background-position: -56px -18px;
}
.rc-tree.rc-tree-show-line .rc-tree-treenode:last-child > .rc-tree-switcher-noop {
background-position: -56px -36px;
}
.rc-tree-child-tree {
display: none;
}
Expand All @@ -121,11 +112,6 @@
.rc-tree-treenode-active {
background: rgba(0, 0, 0, 0.1);
}
.rc-tree-node-selected {
background-color: #ffe6b0;
border: 1px #ffb951 solid;
opacity: 0.8;
}
.rc-tree-icon__open {
margin-right: 2px;
vertical-align: top;
Expand All @@ -134,12 +120,10 @@
.rc-tree-icon__close {
margin-right: 2px;
vertical-align: top;
background-position: -110px 0;
}
.rc-tree-icon__docu {
margin-right: 2px;
vertical-align: top;
background-position: -110px -32px;
}
.rc-tree-icon__customize {
margin-right: 2px;
Expand Down
8 changes: 4 additions & 4 deletions src/extensions/explore/explore.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from 'react';
import { useState } from 'react';
import Collapse, { Panel } from 'mo/components/collapse';
import { TreeView } from './tree';
import TreeView from './tree';
import Toolbar from 'mo/components/toolbar';
import { IActionBarItem } from 'mo/components/actionbar';
import { prefixClaName } from 'mo/common/className';
import { prefixClaName, codIcon } from 'mo/common/className';
import { Header, Content } from 'mo/workbench/sidebar';
import { codIcon } from 'mo/common/className';
import { data } from './treeMock'
interface IExplorerProps {
isActive?: boolean;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ const initState = {
},
],
renderPanel: () => {
return <TreeView />
return <TreeView data={data}/>
}
},
{
Expand Down
121 changes: 103 additions & 18 deletions src/extensions/explore/tree.tsx
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)
52 changes: 52 additions & 0 deletions src/extensions/explore/treeMock.ts
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',
},
],
}
];
2 changes: 1 addition & 1 deletion stories/components/4-Tree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Basic = () => (
prefixCls='rc-tree'
draggable
/**
* TODO: 精灵图...
* TODO: antd 用的精灵图...
*/
// showLine
showIcon
Expand Down

0 comments on commit e120d46

Please sign in to comment.