Here, I have a code in React. I want to show dashed lines on the left of the tree.
How can I do that?
I need something like this:
And, I want to merge the style of TreeView
with this code:
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} />);
"organizationalUnitsList": [ { "organizationalUnitId": "", "organizationalUnitName": "A", "organizationalUnitsList": [ { "organizationalUnitId": "", "organizationalUnitName": "b", } ] }, { "organizationalUnitId": "", "organizationalUnitName": "C", "organizationalUnitsList": [ { "organizationalUnitId": "", "organizationalUnitName": "D", "organizationalUnitsList": [ { "organizationalUnitId": "", "organizationalUnitName": "e", } ] } ] }, { "organizationalUnitId": "", "organizationalUnitName": "f" }
]
Now, I want TreeView to not show borderBottom at OrganizationalUnitName as ‘A’,’C’ and ‘D’. Because they will be acting as a parent of their child. I want child to show borderBottom not the parent.
There are multiple organizationalUnitId. But whenever array of objects appears, I want objects to appear as a child not the parent.
How can I do that?
Advertisement
Answer
The example in this docs demonstrates how to add vertical border to the TreeItem
. To add horizontal border, you can create a pseudo element for each TreeItem
and use absolute
position to place them correctly. Here is an example:
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}` } }; });
Usage
<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>
Live Demo
V5
V4