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 | 12x 12x 12x 12x | import React from 'react'; import PropTypes from 'prop-types'; import * as R from 'ramda'; import cx from 'classnames'; import { makeStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; import Menu from '@material-ui/core/Menu'; import { List } from '../'; const useStyles = makeStyles(theme => ({ buttonRoot: { marginRight: props => (R.equals(props.endIcon, null) ? theme.spacing(1) : theme.spacing(0)), minWidth: props => (props.isSm ? 30 : 64), }, buttonLabel: { display: 'flex', flexDirection: props => props.flexdirection, }, buttonStartIcon: { margin: theme.spacing(0), }, buttonEndIcon: { marginLeft: props => (props.isRtl ? theme.spacing(-1) : theme.spacing(1)), marginRight: props => (props.isRtl ? theme.spacing(0.5) : theme.spacing(-0.5)), }, buttonSelectedMenu: { borderBottom: `3px solid ${theme.palette.primary.main}`, borderRadius: 'unset', paddingBottom: 'unset', }, buttonSelectedAction: { backgroundColor: theme.palette.action.selected, '&:hover': { backgroundColor: theme.palette.action.selected, }, }, rtlList: { flexDirection: props => (props.isRtl ? 'row-reverse' : 'inherit'), }, iconSize: { fontSize: theme.typography.fontSize + 6, }, })); const getIsSelected = equals => R.find(R.pipe(R.prop('id'), equals)); const ButtonMenu = ({ id, items = [], buttonProps, onChange, isChildSelected = () => {}, label = '', icon, isRtl, isSm, }) => { const [anchorEl, setAnchorEl] = React.useState(null); const [open, setOpen] = React.useState({}); const classes = useStyles({ ...buttonProps, isRtl, isSm }); const noItems = R.isEmpty(items); const handleClick = (hasChild, id, action) => () => { if (hasChild) return setOpen({ ...open, [id]: !R.prop(id)(open) }); R.isNil(action) ? onChange(id) : action(id); setAnchorEl(null); }; const handleMenu = id => event => { noItems ? onChange(id) : setAnchorEl(event.currentTarget); }; const handleClose = () => setAnchorEl(null); const variant = R.propOr('Action', 'selectedvariant')(buttonProps); return ( <React.Fragment> <Button key={id} aria-haspopup="true" onClick={handleMenu(id)} classes={{ root: classes.buttonRoot, startIcon: classes.buttonStartIcon, endIcon: classes.buttonEndIcon, label: classes.buttonLabel, }} size="small" color="primary" className={cx({ [R.prop(`buttonSelected${variant}`)(classes)]: R.or( getIsSelected(isChildSelected)(R.flatten(items)), isChildSelected(id), ), })} startIcon={R.isNil(icon) ? null : icon} {...buttonProps} > {label} </Button> {noItems ? null : ( <Menu id={`${id}-menu`} anchorEl={anchorEl} getContentAnchorEl={null} elevation={0} open={Boolean(anchorEl)} anchorOrigin={{ vertical: 'bottom', horizontal: 'right', }} transformOrigin={{ vertical: 'top', horizontal: 'right', }} onClose={handleClose} > <List items={items} isChildSelected={isChildSelected} handleClick={handleClick} open={open} className={classes.rtlList} /> </Menu> )} </React.Fragment> ); }; ButtonMenu.propTypes = { id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), items: PropTypes.arrayOf( 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.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, }), ), }), ), ), buttonProps: PropTypes.shape({ flexdirection: PropTypes.string, selectedvariant: PropTypes.string, }), onChange: PropTypes.func, isChildSelected: PropTypes.func, label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]), icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), isRtl: PropTypes.bool, isSm: PropTypes.bool, }; export default ButtonMenu; |