I'm using Mui V5 and want to change TreeView so it doesnt include collapse/extand icons since I need them to be a part of the TreeItem label component and appear on the left, not right. Also, I want to cancel the indentation of the TreeItems. how can I do that?
CodePudding user response:
I think that what you are trying to do with indentation cannot be directly done through TreeView or TreeItem props. You can remove icons just avoiding defaultCollapseIcon and defaultExpandIcon props.
You can always add some custom styling to achieve what you want. Here's an example to just get a TreeView without icons nor indentation.
const StyledTreeView = styled(TreeView)`
.MuiTreeItem-group {
margin-left: 0;
}
`;
const StyledTreeItem = styled(TreeItem)`
.MuiTreeItem-iconContainer {
display: none;
}
`;
export default function App() {
return (
<StyledTreeView aria-label="tree">
<StyledTreeItem nodeId="1" label="Item 1">
<StyledTreeItem nodeId="2" label="Subitem 1-1" />
</StyledTreeItem>
<StyledTreeItem nodeId="5" label="Item 2">
<StyledTreeItem nodeId="10" label="Subitem 2-1" />
<StyledTreeItem nodeId="6" label="Subitem 2-2">
<StyledTreeItem nodeId="8" label="Subitem 2-2-1" />
</StyledTreeItem>
</StyledTreeItem>
</StyledTreeView>
);
}
The custom style applied to TreeView removes the indentation, and the one applied to StyledTreeItem removes the space assigned to an icon.
