Skip to content
Advertisement

Material-UI: How to add border in TreeView

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:

enter image description here

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} />);

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"
    }

]

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

Codesandbox Demo

V4

Edit 66946584/how-to-customize-the-treeview-in-react-with-material-ui

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement