2021-07-08 19:41:03 +08:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import { defineMessages, injectIntl } from "react-intl";
|
|
|
|
|
|
|
|
import Menu from "@material-ui/core/Menu";
|
|
|
|
import MenuItem from "@material-ui/core/MenuItem";
|
|
|
|
import { Divider } from "@material-ui/core";
|
|
|
|
|
|
|
|
import Icon from "/imports/ui/components/icon/component";
|
|
|
|
import Button from "/imports/ui/components/button/component";
|
|
|
|
|
2021-09-14 09:50:24 +08:00
|
|
|
import { ENTER, SPACE } from "/imports/utils/keyCodes";
|
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
import { styles } from "./styles";
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
close: {
|
|
|
|
id: 'app.dropdown.close',
|
|
|
|
description: 'Close button label',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-19 01:30:27 +08:00
|
|
|
//Used to switch to mobile view
|
2021-07-08 19:41:03 +08:00
|
|
|
const MAX_WIDTH = 640;
|
|
|
|
|
|
|
|
class BBBMenu extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
anchorEl: null,
|
|
|
|
};
|
|
|
|
|
2021-09-02 06:17:18 +08:00
|
|
|
this.opts = props.opts;
|
|
|
|
this.autoFocus = false;
|
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.handleClose = this.handleClose.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(event) {
|
2021-08-03 01:48:52 +08:00
|
|
|
this.setState({ anchorEl: event.currentTarget });
|
2021-07-08 19:41:03 +08:00
|
|
|
};
|
2021-08-03 01:48:52 +08:00
|
|
|
|
2021-09-01 04:08:26 +08:00
|
|
|
handleClose(event) {
|
2021-07-08 19:41:03 +08:00
|
|
|
const { onCloseCallback } = this.props;
|
2021-08-03 01:48:52 +08:00
|
|
|
this.setState({ anchorEl: null }, onCloseCallback());
|
2021-09-01 04:08:26 +08:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
event.persist();
|
|
|
|
|
|
|
|
if (event.type === 'click') {
|
|
|
|
setTimeout(() => {
|
|
|
|
document.activeElement.blur();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 19:41:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
makeMenuItems() {
|
2021-07-19 01:30:27 +08:00
|
|
|
const { actions, selectedEmoji } = this.props;
|
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
return actions?.map(a => {
|
2021-08-14 09:20:49 +08:00
|
|
|
const { dataTest, label, onClick, key, disabled } = a;
|
2021-08-09 01:13:47 +08:00
|
|
|
const itemClasses = [styles.menuitem, a?.className];
|
2021-07-19 01:30:27 +08:00
|
|
|
|
|
|
|
if (key?.toLowerCase()?.includes(selectedEmoji?.toLowerCase())) itemClasses.push(styles.emojiSelected);
|
|
|
|
|
2021-11-06 02:38:01 +08:00
|
|
|
let customStyles = {
|
|
|
|
paddingLeft: '4px',
|
|
|
|
paddingRight: '4px',
|
|
|
|
paddingTop: '8px',
|
|
|
|
paddingBottom: '8px',
|
|
|
|
marginLeft: '4px',
|
|
|
|
marginRight: '4px'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (a.customStyles) {
|
|
|
|
customStyles = { ...customStyles, ...a.customStyles };
|
|
|
|
}
|
|
|
|
|
2021-08-09 01:13:47 +08:00
|
|
|
return [
|
2021-09-02 06:17:18 +08:00
|
|
|
a.dividerTop && <Divider disabled />,
|
2021-08-03 01:48:52 +08:00
|
|
|
<MenuItem
|
2021-08-09 01:13:47 +08:00
|
|
|
key={label}
|
2021-08-17 11:24:16 +08:00
|
|
|
data-test={dataTest || key}
|
2021-08-09 01:13:47 +08:00
|
|
|
className={itemClasses.join(' ')}
|
|
|
|
disableRipple={true}
|
|
|
|
disableGutters={true}
|
2021-08-11 10:44:34 +08:00
|
|
|
disabled={disabled}
|
2021-11-06 02:38:01 +08:00
|
|
|
style={customStyles}
|
2021-09-01 04:08:26 +08:00
|
|
|
onClick={(event) => {
|
2021-08-09 01:13:47 +08:00
|
|
|
onClick();
|
|
|
|
const close = !key.includes('setstatus') && !key.includes('back');
|
|
|
|
// prevent menu close for sub menu actions
|
2021-09-01 04:08:26 +08:00
|
|
|
if (close) this.handleClose(event);
|
2021-08-09 01:13:47 +08:00
|
|
|
}}>
|
2021-09-02 06:17:18 +08:00
|
|
|
<div style={{ display: 'flex', flexFlow: 'row', width: '100%' }}>
|
2021-07-08 19:41:03 +08:00
|
|
|
{a.icon ? <Icon iconName={a.icon} key="icon" /> : null}
|
2021-07-21 01:48:08 +08:00
|
|
|
<div className={styles.option}>{label}</div>
|
2021-07-08 19:41:03 +08:00
|
|
|
{a.iconRight ? <Icon iconName={a.iconRight} key="iconRight" className={styles.iRight} /> : null}
|
|
|
|
</div>
|
|
|
|
</MenuItem>,
|
2021-09-02 06:17:18 +08:00
|
|
|
a.divider && <Divider disabled />
|
2021-08-09 01:13:47 +08:00
|
|
|
];
|
2021-07-08 19:41:03 +08:00
|
|
|
});
|
|
|
|
}
|
2021-08-03 01:48:52 +08:00
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
render() {
|
|
|
|
const { anchorEl } = this.state;
|
2021-11-06 02:38:01 +08:00
|
|
|
const { trigger, intl, wide, classes, customStyles } = this.props;
|
2021-07-08 19:41:03 +08:00
|
|
|
const actionsItems = this.makeMenuItems();
|
2021-08-11 10:44:34 +08:00
|
|
|
const menuClasses = classes || [];
|
|
|
|
menuClasses.push(styles.menu);
|
2021-08-09 01:22:01 +08:00
|
|
|
if (wide) menuClasses.push(styles.wide);
|
2021-07-19 01:30:27 +08:00
|
|
|
|
2021-11-06 02:38:01 +08:00
|
|
|
let menuStyles = { zIndex: 9999 };
|
|
|
|
|
|
|
|
if (customStyles) {
|
|
|
|
menuStyles = { ...menuStyles, ...customStyles };
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
return (
|
2021-08-17 11:18:49 +08:00
|
|
|
<>
|
2021-09-02 06:17:18 +08:00
|
|
|
<div
|
|
|
|
onClick={(e) => {
|
|
|
|
e.persist();
|
2021-09-02 20:37:04 +08:00
|
|
|
this.opts.autoFocus = !(['mouse', 'touch'].includes(e.nativeEvent.pointerType));
|
2021-09-02 06:17:18 +08:00
|
|
|
this.handleClick(e);
|
|
|
|
}}
|
2021-09-14 09:50:24 +08:00
|
|
|
onKeyPress={(e) => {
|
|
|
|
e.persist();
|
|
|
|
if (e.which !== ENTER) return null;
|
|
|
|
this.handleClick(e);
|
|
|
|
}}
|
2021-09-02 06:17:18 +08:00
|
|
|
accessKey={this.props?.accessKey}
|
|
|
|
>
|
|
|
|
{trigger}
|
|
|
|
</div>
|
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
<Menu
|
2021-09-02 06:17:18 +08:00
|
|
|
{...this.opts}
|
2021-07-08 19:41:03 +08:00
|
|
|
anchorEl={anchorEl}
|
|
|
|
open={Boolean(anchorEl)}
|
|
|
|
onClose={this.handleClose}
|
2021-07-19 01:30:27 +08:00
|
|
|
className={menuClasses.join(' ')}
|
2021-11-06 02:38:01 +08:00
|
|
|
style={menuStyles}
|
2021-07-08 19:41:03 +08:00
|
|
|
>
|
|
|
|
{actionsItems}
|
2021-08-03 01:48:52 +08:00
|
|
|
{anchorEl && window.innerWidth < MAX_WIDTH &&
|
2021-07-08 19:41:03 +08:00
|
|
|
<Button
|
|
|
|
className={styles.closeBtn}
|
|
|
|
label={intl.formatMessage(intlMessages.close)}
|
|
|
|
size="lg"
|
|
|
|
color="default"
|
|
|
|
onClick={this.handleClose}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</Menu>
|
2021-08-17 11:18:49 +08:00
|
|
|
</>
|
2021-07-08 19:41:03 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(BBBMenu);
|
|
|
|
|
|
|
|
BBBMenu.defaultProps = {
|
|
|
|
opts: {
|
|
|
|
id: "default-dropdown-menu",
|
2021-09-02 06:17:18 +08:00
|
|
|
autoFocus: false,
|
2021-07-08 19:41:03 +08:00
|
|
|
keepMounted: true,
|
|
|
|
transitionDuration: 0,
|
|
|
|
elevation: 3,
|
|
|
|
getContentAnchorEl: null,
|
|
|
|
fullwidth: "true",
|
|
|
|
anchorOrigin: { vertical: 'top', horizontal: 'right' },
|
2021-08-09 01:13:47 +08:00
|
|
|
transformorigin: { vertical: 'top', horizontal: 'right' },
|
2021-07-08 19:41:03 +08:00
|
|
|
},
|
2021-09-02 06:17:18 +08:00
|
|
|
onCloseCallback: () => { },
|
2021-07-19 01:30:27 +08:00
|
|
|
wide: false,
|
2021-07-08 19:41:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
BBBMenu.propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
|
|
|
|
trigger: PropTypes.element.isRequired,
|
|
|
|
|
|
|
|
actions: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
2021-08-31 02:19:52 +08:00
|
|
|
onClick: PropTypes.func,
|
2021-07-08 19:41:03 +08:00
|
|
|
icon: PropTypes.string,
|
|
|
|
iconRight: PropTypes.string,
|
2021-09-02 06:17:18 +08:00
|
|
|
disabled: PropTypes.bool,
|
2021-07-08 19:41:03 +08:00
|
|
|
divider: PropTypes.bool,
|
2021-08-09 01:13:47 +08:00
|
|
|
dividerTop: PropTypes.bool,
|
2021-08-09 01:22:01 +08:00
|
|
|
accessKey: PropTypes.string,
|
2021-07-08 19:41:03 +08:00
|
|
|
})).isRequired,
|
|
|
|
|
|
|
|
onCloseCallback: PropTypes.func,
|
2021-07-19 01:30:27 +08:00
|
|
|
|
|
|
|
wide: PropTypes.bool,
|
2021-07-08 19:41:03 +08:00
|
|
|
};
|