gpt4 book ai didi

javascript - Material-UI:如何在 TreeView 中添加边框

转载 作者:行者123 更新时间:2023-12-04 11:35:32 32 4
gpt4 key购买 nike

在这里,我在 React 中有一个代码。我想在树的左侧显示虚线。
我怎样才能做到这一点?
我需要这样的东西:
enter image description here
而且,我想合并 TreeView 的样式使用此代码:

const StyledTreeItem = withStyles((theme) => ({
iconContainer: {
'& .close': {
opacity: 0.3,
},
},
group: {
marginLeft: 2,
paddingLeft: 3,
borderLeft: `1px dashed ${fade(theme.palette.text.primary, 0.4)}`,
},
}))((props) => <TreeItem {...props} />);
Edit https://codesandbox.io/s/green-surf-dcoqr?fontsize=14&hidenavigation=1&theme=dark&file=/src/tree.js:0-2494
"organizationalUnitsList": [
{
"organizationalUnitId": "",
"organizationalUnitName": "A",
"organizationalUnitsList": [
{
"organizationalUnitId": "",
"organizationalUnitName": "b",
}
]
},
{
"organizationalUnitId": "",
"organizationalUnitName": "C",
"organizationalUnitsList": [
{
"organizationalUnitId": "",
"organizationalUnitName": "D",
"organizationalUnitsList": [
{
"organizationalUnitId": "",
"organizationalUnitName": "e",
}
]
}
]
},
{
"organizationalUnitId": "",
"organizationalUnitName": "f"
}
]
现在,我希望 TreeView 不会在 OrganizationalUnitName 处将borderBottom 显示为“A”、“C”和“D”。因为他们将扮演他们 child 的 parent 。我希望 child 显示borderBottom 而不是 parent 。
有多个organizationUnitId。但是每当出现对象数组时,我希望对象显示为子对象而不是父对象。
我怎样才能做到这一点?

最佳答案

docs 中的示例演示如何为 TreeItem 添加垂直边框.要添加水平边框,您可以为每个 TreeItem 创建一个伪元素并使用 absolute位置以正确放置它们。这是一个例子:

import TreeItem, { treeItemClasses } from "@mui/lab/TreeItem";

type StyledTreeItemProps = {
rootNode?: boolean;
};

const StyledTreeItem = styled(TreeItem)<StyledTreeItemProps>(({ rootNode }) => {
const borderColor = "gray";

return {
position: "relative",
"&:before": {
pointerEvents: "none",
content: '""',
position: "absolute",
width: 32,
left: -16,
top: 12,
borderBottom:
// only display if the TreeItem is not root node
!rootNode ? `1px dashed ${borderColor}` : "none"
},

[`& .${treeItemClasses.group}`]: {
marginLeft: 16,
paddingLeft: 18,
borderLeft: `1px dashed ${borderColor}`
}
};
});
用法
<TreeView>
<StyledTreeItem rootNode nodeId="1" label="Applications">
<StyledTreeItem nodeId="2" label="Calendar" />
<StyledTreeItem nodeId="3" label="Drive" />
</StyledTreeItem>
<StyledTreeItem rootNode nodeId="8" label="Documents">
<StyledTreeItem nodeId="9" label="OSS" />
<StyledTreeItem nodeId="10" label="MUI">
<StyledTreeItem nodeId="11" label="index.js" />
</StyledTreeItem>
</StyledTreeItem>
</TreeView>
现场演示
V5
Codesandbox Demo
V4
Edit 66946584/how-to-customize-the-treeview-in-react-with-material-ui

关于javascript - Material-UI:如何在 TreeView 中添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66946584/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com