Match-id-73ae28dbb8f2748c5398680c325a7ec24cee005a

This commit is contained in:
* 2022-03-30 14:14:42 +08:00 committed by *
parent 1cef144042
commit 13e08a8d1b
3 changed files with 64 additions and 25 deletions

View File

@ -5,11 +5,13 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
overflow-y: auto; overflow-y: auto;
}
.treeItem { .treeItem {
width: 100%; width: 100%;
position: absolute; position: absolute;
line-height: 18px;
&:hover {
background-color: @select-color;
}
.treeIcon { .treeIcon {
color: @arrow-color; color: @arrow-color;
@ -17,14 +19,22 @@
width: 12px; width: 12px;
padding-left: 0.5rem; padding-left: 0.5rem;
} }
.componentName { .componentName {
color: @component-name-color; color: @component-name-color;
} }
.componentKeyName { .componentKeyName {
color: @component-key-color; color: @component-key-color;
} }
.componentKeyValue { .componentKeyValue {
color: @componentKeyValue-color; color: @componentKeyValue-color;
} }
} }
.select {
background-color: rgb(141 199 248 / 60%);
}
}

View File

@ -13,23 +13,45 @@ type IItem = {
style: any, style: any,
hasChild: boolean, hasChild: boolean,
onCollapse: (id: string) => void, onCollapse: (id: string) => void,
onClick: (id: string) => void,
isCollapsed: boolean, isCollapsed: boolean,
isSelect: boolean,
} & IData } & IData
// TODO: 计算可以展示的最多数量,并且监听显示器高度变化修改数值 // TODO: 计算可以展示的最多数量,并且监听显示器高度变化修改数值
const showNum = 50; const showNum = 70;
const divHeight = 21; const lineHeight = 18;
const indentationLength = 20; const indentationLength = 20;
function Item({ name, style, userKey, hasChild, onCollapse, isCollapsed, id, indentation }: IItem) { function Item(props: IItem) {
const {
name,
style,
userKey,
hasChild,
onCollapse,
isCollapsed,
id,
indentation,
onClick,
isSelect,
} = props;
const isShowKey = userKey !== ''; const isShowKey = userKey !== '';
const showIcon = hasChild ? <Arrow director={isCollapsed ? 'right' : 'down'} /> : ''; const showIcon = hasChild ? <Arrow director={isCollapsed ? 'right' : 'down'} /> : '';
const onClickCollapse = () => { const handleClickCollapse = () => {
onCollapse(id); onCollapse(id);
} }
const handleClick = () => {
onClick(id);
}
const itemAttr: any = {style, className: styles.treeItem, onClick: handleClick};
if (isSelect) {
itemAttr.tabIndex = 0;
itemAttr.className = styles.treeItem + ' ' + styles.select
}
return ( return (
<div style={style} className={styles.treeItem}> <div {...itemAttr}>
<div style={{ marginLeft: indentation * indentationLength }} className={styles.treeIcon} onClick={onClickCollapse} > <div style={{ marginLeft: indentation * indentationLength }} className={styles.treeIcon} onClick={handleClickCollapse} >
{showIcon} {showIcon}
</div> </div>
<span className={styles.componentName} > <span className={styles.componentName} >
@ -54,6 +76,7 @@ function Item({ name, style, userKey, hasChild, onCollapse, isCollapsed, id, ind
function VTree({ data }: { data: IData[] }) { function VTree({ data }: { data: IData[] }) {
const [scrollTop, setScrollTop] = useState(0); const [scrollTop, setScrollTop] = useState(0);
const [collapseNode, setCollapseNode] = useState(new Set<string>()); const [collapseNode, setCollapseNode] = useState(new Set<string>());
const [selectItem, setSelectItem] = useState();
const changeCollapseNode = (id: string) => { const changeCollapseNode = (id: string) => {
const nodes = new Set<string>(); const nodes = new Set<string>();
collapseNode.forEach(value => { collapseNode.forEach(value => {
@ -66,6 +89,9 @@ function VTree({ data }: { data: IData[] }) {
} }
setCollapseNode(nodes); setCollapseNode(nodes);
}; };
const handleClickItem = (id: string) => {
setSelectItem(id);
};
const showList: any[] = []; const showList: any[] = [];
let totalHeight = 0; let totalHeight = 0;
@ -89,17 +115,19 @@ function VTree({ data }: { data: IData[] }) {
const hasChild = nextItem ? nextItem.indentation > item.indentation : false; const hasChild = nextItem ? nextItem.indentation > item.indentation : false;
showList.push( showList.push(
<Item <Item
key={item.id} key={id}
hasChild={hasChild} hasChild={hasChild}
style={{ style={{
transform: `translateY(${totalHeight}px)`, transform: `translateY(${totalHeight}px)`,
}} }}
onCollapse={changeCollapseNode} onCollapse={changeCollapseNode}
onClick={handleClickItem}
isCollapsed={isCollapsed} isCollapsed={isCollapsed}
isSelect={id === selectItem}
{...item} /> {...item} />
) )
} }
totalHeight = totalHeight + divHeight; totalHeight = totalHeight + lineHeight;
if (isCollapsed) { if (isCollapsed) {
// 该节点需要收起子节点 // 该节点需要收起子节点
currentCollapseIndentation = item.indentation; currentCollapseIndentation = item.indentation;

View File

@ -5,6 +5,7 @@
@component-key-color: rgb(153, 69, 0); @component-key-color: rgb(153, 69, 0);
@componentKeyValue-color: rgb(26, 26, 166); @componentKeyValue-color: rgb(26, 26, 166);
@component-attr-color: rgb(200, 0, 0); @component-attr-color: rgb(200, 0, 0);
@select-color: rgb(141 199 248 / 60%);
@top-height: 2.625rem; @top-height: 2.625rem;
@divider-width: 0.2px; @divider-width: 0.2px;