-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathMenu.js
109 lines (100 loc) · 3.1 KB
/
Menu.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
109
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import inflection from 'inflection';
import compose from 'recompose/compose';
import { withStyles, createStyles } from '@material-ui/core/styles';
import classnames from 'classnames';
import { getResources, translate } from 'ra-core';
import DefaultIcon from '@material-ui/icons/ViewList';
import DashboardMenuItem from './DashboardMenuItem';
import MenuItemLink from './MenuItemLink';
import Responsive from '../layout/Responsive';
const styles = createStyles({
main: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'flex-start',
},
});
const translatedResourceName = (resource, translate) =>
translate(`resources.${resource.name}.name`, {
smart_count: 2,
_:
resource.options && resource.options.label
? translate(resource.options.label, {
smart_count: 2,
_: resource.options.label,
})
: inflection.humanize(inflection.pluralize(resource.name)),
});
const Menu = ({
classes,
className,
dense,
hasDashboard,
onMenuClick,
open,
pathname,
resources,
translate,
logout,
...rest
}) => (
<div className={classnames(classes.main, className)} {...rest}>
{hasDashboard && <DashboardMenuItem onClick={onMenuClick} />}
{resources
.filter(r => r.hasList)
.map(resource => (
<MenuItemLink
key={resource.name}
to={`/${resource.name}`}
primaryText={translatedResourceName(resource, translate)}
leftIcon={
resource.icon ? <resource.icon /> : <DefaultIcon />
}
onClick={onMenuClick}
dense={dense}
/>
))}
<Responsive xsmall={logout} medium={null} />
</div>
);
Menu.propTypes = {
classes: PropTypes.object,
className: PropTypes.string,
dense: PropTypes.bool,
hasDashboard: PropTypes.bool,
logout: PropTypes.element,
onMenuClick: PropTypes.func,
open: PropTypes.bool,
pathname: PropTypes.string,
resources: PropTypes.array.isRequired,
translate: PropTypes.func.isRequired,
};
Menu.defaultProps = {
onMenuClick: () => null,
};
const mapStateToProps = state => ({
open: state.admin.ui.sidebarOpen,
resources: getResources(state),
pathname: state.router.location.pathname, // used to force redraw on navigation
});
const enhance = compose(
translate,
connect(
mapStateToProps,
{}, // Avoid connect passing dispatch in props,
null,
{
areStatePropsEqual: (prev, next) =>
prev.resources.every(
(value, index) => value === next.resources[index] // shallow compare resources
) &&
prev.pathname === next.pathname &&
prev.open === next.open,
}
),
withStyles(styles)
);
export default enhance(Menu);