forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomContentTreeView.js
102 lines (93 loc) · 2.74 KB
/
CustomContentTreeView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as React from 'react';
import clsx from 'clsx';
import { styled } from '@mui/material/styles';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Avatar from '@mui/material/Avatar';
import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView';
import { TreeItem, useTreeItemState } from '@mui/x-tree-view/TreeItem';
const CustomContentRoot = styled('div')(({ theme }) => ({
'&': { padding: theme.spacing(0.5, 1) },
}));
const CustomContent = React.forwardRef(function CustomContent(props, ref) {
const {
className,
classes,
label,
nodeId,
icon: iconProp,
expansionIcon,
displayIcon,
} = props;
const {
disabled,
expanded,
selected,
focused,
handleExpansion,
handleSelection,
preventSelection,
} = useTreeItemState(nodeId);
const icon = iconProp || expansionIcon || displayIcon;
const handleMouseDown = (event) => {
preventSelection(event);
};
const handleClick = (event) => {
handleExpansion(event);
handleSelection(event);
};
return (
<CustomContentRoot
className={clsx(className, classes.root, {
'Mui-expanded': expanded,
'Mui-selected': selected,
'Mui-focused': focused,
'Mui-disabled': disabled,
})}
onClick={handleClick}
onMouseDown={handleMouseDown}
ref={ref}
>
<div className={classes.iconContainer}>{icon}</div>
<Box sx={{ flexGrow: 1, display: 'flex', gap: 1 }}>
<Avatar
sx={(theme) => ({
background: theme.palette.primary.main,
width: 24,
height: 24,
fontSize: '0.8rem',
})}
>
{label[0]}
</Avatar>
<Typography component="div" className={classes.label}>
{label}
</Typography>
</Box>
</CustomContentRoot>
);
});
const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
return <TreeItem ContentComponent={CustomContent} {...props} ref={ref} />;
});
export default function CustomContentTreeView() {
return (
<Box sx={{ minHeight: 180, flexGrow: 1, maxWidth: 300 }}>
<SimpleTreeView
aria-label="icon expansion"
sx={{ position: 'relative' }}
defaultExpandedNodes={['3']}
>
<CustomTreeItem nodeId="1" label="Amelia Hart">
<CustomTreeItem nodeId="2" label="Jane Fisher" />
</CustomTreeItem>
<CustomTreeItem nodeId="3" label="Bailey Monroe">
<CustomTreeItem nodeId="4" label="Freddie Reed" />
<CustomTreeItem nodeId="5" label="Georgia Johnson">
<CustomTreeItem nodeId="6" label="Samantha Malone" />
</CustomTreeItem>
</CustomTreeItem>
</SimpleTreeView>
</Box>
);
}