-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathMenuBranch.js
108 lines (93 loc) · 2.86 KB
/
MenuBranch.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
103
104
105
106
107
108
import React from 'react'
import { styled } from '@mui/material/styles'
import PropTypes from 'prop-types'
import MenuItemContent from './MenuItemContent'
import ExpandableSection from '../ExpandableSection'
import MenuLeaf from './MenuLeaf'
const PREFIX = 'RSFMenuBranch'
const classes = {
expander: `${PREFIX}-expander`,
expanderMargins: `${PREFIX}-expanderMargins`,
expanderSummary: `${PREFIX}-expanderSummary`,
expanderSummaryExpanded: `${PREFIX}-expanderSummaryExpanded`,
expanderIconExpanded: `${PREFIX}-expanderIconExpanded`,
expanderDetails: `${PREFIX}-expanderDetails`,
expanderContent: `${PREFIX}-expanderContent`,
}
const StyledMenuItemContent = styled(MenuItemContent)(({ theme }) => ({
[`& .${classes.expander}`]: {
borderBottom: 'none',
},
[`& .${classes.expanderMargins}`]: {
padding: 0,
},
[`& .${classes.expanderSummary}`]: {
textTransform: 'uppercase',
'&:first-child': {
padding: `10px ${theme.spacing(2)}`,
borderBottom: `1px solid ${theme.palette.divider}`,
},
},
[`& .${classes.expanderSummaryExpanded}`]: {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
},
[`& .${classes.expanderIconExpanded}`]: {
color: theme.palette.secondary.contrastText,
},
[`& .${classes.expanderDetails}`]: {
'$expander &': {
paddingBottom: 0,
},
},
[`& .${classes.expanderContent}`]: {
textTransform: 'none',
},
}))
export {}
const MenuBranch = React.memo(props => {
const { depth, item, ExpanderComponent, itemProps } = props
const isLeaf = !item.items.some(child => child.items && child.items.length > 0)
if (item.expanded != null && item.items && item.items.length && isLeaf) {
return (
<ExpanderComponent
title={item.text}
defaultExpanded={item.expanded}
classes={{
root: classes.expander,
margins: classes.expanderMargins,
summary: classes.expanderSummary,
summaryExpanded: classes.expanderSummaryExpanded,
expandIconExpanded: classes.expanderIconExpanded,
details: classes.expanderDetails,
}}
>
{item.items.map((child, i) => (
<MenuLeaf
key={i}
item={child}
depth={depth}
classes={{ listItem: classes.expanderContent }}
/>
))}
</ExpanderComponent>
)
}
return <StyledMenuItemContent item={item} depth={depth} leaf={false} listItemProps={itemProps} />
})
export default MenuBranch
MenuBranch.propTypes = {
/**
* Overrides the default component for expandable items
*/
ExpanderComponent: PropTypes.elementType.isRequired,
/**
* Additional props for the underlying ListItem
*/
itemProps: PropTypes.object,
depth: PropTypes.number,
item: PropTypes.object,
}
MenuBranch.defaultProps = {
ExpanderComponent: ExpandableSection,
}