Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 12x 12x 12x 12x 12x 12x 12x | import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import * as R from 'ramda'; import cx from 'classnames'; import { makeStyles } from '@material-ui/core/styles'; import List from '@material-ui/core/List'; import ListItem from '@material-ui/core/ListItem'; import ListItemIcon from '@material-ui/core/ListItemIcon'; import ListItemText from '@material-ui/core/ListItemText'; import Collapse from '@material-ui/core/Collapse'; import ExpandLess from '@material-ui/icons/ExpandLess'; import ExpandMore from '@material-ui/icons/ExpandMore'; import Divider from '@material-ui/core/Divider'; const useStyles = makeStyles(theme => ({ listItemIcon: { minWidth: 20, paddingRight: theme.spacing(1.25), paddingLeft: theme.spacing(0), fontSize: theme.typography.fontSize, color: 'unset', '& > svg': { fontSize: theme.typography.fontSize + 4, }, }, listItemText: { marginTop: theme.spacing(0), marginBottom: theme.spacing(0), fontSize: theme.typography.fontSize, overflow: 'hidden', textOverflow: 'ellipsis', }, menuItemSelected: { color: theme.palette.primary.main, }, spacing: { marginLeft: theme.spacing(1), marginRight: theme.spacing(1), }, })); const ListElements = ({ id, icon, label = '', action, hasChild, isSelected, classes, children = [], handleClick, open, className, }) => ( <React.Fragment key={id}> <ListItem button onClick={handleClick(hasChild, id, action)} selected={isSelected} className={className} > {R.isNil(icon) ? null : ( <ListItemIcon className={cx(classes.listItemIcon, { [classes.menuItemSelected]: isSelected })} > {icon} </ListItemIcon> )} {<ListItemText key="buttonMenuText" className={classes.listItemText} primary={label} />} {hasChild ? R.prop(id)(open) ? <ExpandLess /> : <ExpandMore /> : null} </ListItem> {hasChild ? ( <Collapse in={R.prop(id)(open)} timeout="auto" unmountOnExit> {children} </Collapse> ) : null} </React.Fragment> ); ListElements.propTypes = { id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), action: PropTypes.func, hasChild: PropTypes.bool, isSelected: PropTypes.bool, classes: PropTypes.object, children: PropTypes.arrayOf( PropTypes.shape({ icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), children: PropTypes.array, }), ), handleClick: PropTypes.func, open: PropTypes.object, className: PropTypes.string, }; const ListParent = ({ items = [], isChildSelected, handleClick, open, className, classes, level = 0, }) => ( <List component="div" disablePadding dense> {R.map(({ id, label = '', icon, action, children = [], isSelected }) => { const _isSelected = R.or(isSelected, isChildSelected(id)); const hasChild = R.pipe(R.isEmpty, R.not)(children); return ( <ListElements key={id} id={id} icon={icon} label={label} hasChild={hasChild} isSelected={_isSelected} classes={classes} handleClick={handleClick} action={action} open={open} className={className} level={level} > <ListParent items={children} isChildSelected={isChildSelected} handleClick={handleClick} open={open} level={R.inc(level)} classes={classes} /> </ListElements> ); })(items)} </List> ); ListParent.propTypes = { items: PropTypes.array, isChildSelected: PropTypes.func, handleClick: PropTypes.func, open: PropTypes.object, className: PropTypes.string, classes: PropTypes.object, level: PropTypes.number, }; const MainList = props => { const classes = useStyles(); const mainList = R.propOr([], 'items')(props); return R.addIndex(R.map)((items, index) => ( <Fragment key={`list-${R.pipe(R.head, R.prop('id'))(items)}-${index}`}> <ListParent {...props} items={items} classes={classes} /> {R.gt(R.length(mainList) - 1, index) ? <Divider className={classes.spacing} /> : null} </Fragment> ))(mainList); }; MainList.propTypes = { items: PropTypes.arrayOf( PropTypes.arrayOf( PropTypes.shape({ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), children: PropTypes.arrayOf( PropTypes.shape({ icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), children: PropTypes.array, }), ), }), ), ), isChildSelected: PropTypes.func, handleClick: PropTypes.func, open: PropTypes.object, className: PropTypes.string, }; export default MainList; |