2021-07-08 19:41:03 +08:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import { defineMessages, injectIntl } from "react-intl";
|
2023-05-03 03:24:06 +08:00
|
|
|
import { Divider } from "@mui/material";
|
2022-02-15 22:51:51 +08:00
|
|
|
import Icon from "/imports/ui/components/common/icon/component";
|
2022-03-04 04:48:56 +08:00
|
|
|
import { SMALL_VIEWPORT_BREAKPOINT } from '/imports/ui/components/layout/enums';
|
2023-04-01 02:06:41 +08:00
|
|
|
import KEY_CODES from '/imports/utils/keyCodes';
|
2024-05-09 20:59:29 +08:00
|
|
|
import MenuSkeleton from './skeleton';
|
2021-11-06 03:59:01 +08:00
|
|
|
import Styled from './styles';
|
2021-07-08 19:41:03 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
close: {
|
|
|
|
id: 'app.dropdown.close',
|
|
|
|
description: 'Close button label',
|
|
|
|
},
|
2023-01-04 23:24:39 +08:00
|
|
|
active: {
|
|
|
|
id: 'app.dropdown.list.item.activeLabel',
|
|
|
|
description: 'active item label',
|
|
|
|
},
|
2021-07-08 19:41:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
class BBBMenu extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
anchorEl: null,
|
|
|
|
};
|
|
|
|
|
2022-05-13 21:42:19 +08:00
|
|
|
this.optsToMerge = {};
|
2021-09-02 06:17:18 +08:00
|
|
|
this.autoFocus = false;
|
|
|
|
|
2023-04-01 02:06:41 +08:00
|
|
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
2021-07-08 19:41:03 +08:00
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.handleClose = this.handleClose.bind(this);
|
|
|
|
}
|
|
|
|
|
2022-04-01 00:58:41 +08:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { anchorEl } = this.state;
|
2023-04-01 02:06:41 +08:00
|
|
|
const { open } = this.props;
|
|
|
|
if (open === false && anchorEl) {
|
2022-04-01 00:58:41 +08:00
|
|
|
this.setState({ anchorEl: null });
|
2023-04-01 02:06:41 +08:00
|
|
|
} else if (open === true && !anchorEl) {
|
2022-04-01 00:58:41 +08:00
|
|
|
this.setState({ anchorEl: this.anchorElRef });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 02:06:41 +08:00
|
|
|
handleKeyDown(event) {
|
|
|
|
const { anchorEl } = this.state;
|
2023-08-04 02:48:47 +08:00
|
|
|
const { isHorizontal } = this.props;
|
2023-04-01 02:06:41 +08:00
|
|
|
const isMenuOpen = Boolean(anchorEl);
|
|
|
|
|
2023-08-04 02:48:47 +08:00
|
|
|
const previousKey = isHorizontal ? KEY_CODES.ARROW_LEFT : KEY_CODES.ARROW_UP;
|
|
|
|
const nextKey = isHorizontal ? KEY_CODES.ARROW_RIGHT : KEY_CODES.ARROW_DOWN;
|
2023-04-01 02:06:41 +08:00
|
|
|
|
|
|
|
if ([KEY_CODES.ESCAPE, KEY_CODES.TAB].includes(event.which)) {
|
|
|
|
this.handleClose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-04 02:48:47 +08:00
|
|
|
if (isMenuOpen && [previousKey, nextKey].includes(event.which)) {
|
2023-04-01 02:06:41 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
const menuItems = Array.from(document.querySelectorAll('[data-key^="menuItem-"]'));
|
|
|
|
if (menuItems.length === 0) return;
|
|
|
|
|
|
|
|
const focusedIndex = menuItems.findIndex(item => item === document.activeElement);
|
2023-08-04 02:48:47 +08:00
|
|
|
const nextIndex = event.which === previousKey ? focusedIndex - 1 : focusedIndex + 1;
|
2023-04-01 02:06:41 +08:00
|
|
|
let indexToFocus = 0;
|
|
|
|
if (nextIndex < 0) {
|
|
|
|
indexToFocus = menuItems.length - 1;
|
|
|
|
} else if (nextIndex >= menuItems.length) {
|
|
|
|
indexToFocus = 0;
|
|
|
|
} else {
|
|
|
|
indexToFocus = nextIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItems[indexToFocus].focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
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() {
|
2023-08-05 03:14:57 +08:00
|
|
|
const { actions, selectedEmoji, intl, isHorizontal, isMobile, roundButtons, keepOpen } = this.props;
|
2021-07-19 01:30:27 +08:00
|
|
|
|
2021-07-08 19:41:03 +08:00
|
|
|
return actions?.map(a => {
|
2023-10-04 22:43:10 +08:00
|
|
|
const { dataTest, label, onClick, key, disabled,
|
2024-05-09 20:59:29 +08:00
|
|
|
description, selected, textColor, isToggle, loading } = a;
|
2021-11-06 03:59:01 +08:00
|
|
|
const emojiSelected = key?.toLowerCase()?.includes(selectedEmoji?.toLowerCase());
|
2021-07-19 01:30:27 +08:00
|
|
|
|
2021-11-06 02:38:01 +08:00
|
|
|
let customStyles = {
|
2022-05-24 00:58:51 +08:00
|
|
|
paddingLeft: '16px',
|
|
|
|
paddingRight: '16px',
|
2022-05-23 22:45:53 +08:00
|
|
|
paddingTop: '12px',
|
|
|
|
paddingBottom: '12px',
|
2022-05-20 04:14:56 +08:00
|
|
|
marginLeft: '0px',
|
|
|
|
marginRight: '0px',
|
2021-11-06 02:38:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (a.customStyles) {
|
|
|
|
customStyles = { ...customStyles, ...a.customStyles };
|
|
|
|
}
|
2024-05-09 20:59:29 +08:00
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return (
|
2024-05-20 22:09:01 +08:00
|
|
|
<MenuSkeleton key={label} />
|
2024-05-09 20:59:29 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-09 01:13:47 +08:00
|
|
|
return [
|
2023-10-04 22:43:10 +08:00
|
|
|
(!a.isSeparator && onClick) && (
|
2023-08-24 22:59:04 +08:00
|
|
|
<Styled.BBBMenuItem
|
|
|
|
emoji={emojiSelected ? 'yes' : 'no'}
|
|
|
|
key={label}
|
|
|
|
data-test={dataTest}
|
|
|
|
data-key={`menuItem-${dataTest}`}
|
|
|
|
disableRipple={true}
|
|
|
|
disableGutters={true}
|
|
|
|
disabled={disabled}
|
|
|
|
style={customStyles}
|
|
|
|
$roundButtons={roundButtons}
|
2024-04-24 01:56:23 +08:00
|
|
|
$isToggle={isToggle}
|
2023-08-24 22:59:04 +08:00
|
|
|
onClick={(event) => {
|
|
|
|
onClick();
|
|
|
|
const close = !keepOpen && !key?.includes('setstatus') && !key?.includes('back');
|
|
|
|
// prevent menu close for sub menu actions
|
|
|
|
if (close) this.handleClose(event);
|
|
|
|
event.stopPropagation();
|
|
|
|
}}>
|
|
|
|
<Styled.MenuItemWrapper>
|
|
|
|
{a.icon ? <Icon iconName={a.icon} key="icon" /> : null}
|
|
|
|
<Styled.Option isHorizontal={isHorizontal} isMobile={isMobile} aria-describedby={`${key}-option-desc`}>{label}</Styled.Option>
|
|
|
|
{description && <div className="sr-only" id={`${key}-option-desc`}>{`${description}${selected ? ` - ${intl.formatMessage(intlMessages.active)}` : ''}`}</div>}
|
|
|
|
{a.iconRight ? <Styled.IconRight iconName={a.iconRight} key="iconRight" /> : null}
|
|
|
|
</Styled.MenuItemWrapper>
|
|
|
|
</Styled.BBBMenuItem>
|
|
|
|
),
|
2023-10-04 22:43:10 +08:00
|
|
|
(!onClick && !a.isSeparator) && (
|
|
|
|
<Styled.BBBMenuInformation
|
|
|
|
key={a.key}
|
|
|
|
>
|
|
|
|
<Styled.MenuItemWrapper>
|
|
|
|
{a.icon ? <Icon color={textColor} iconName={a.icon} key="icon" /> : null}
|
|
|
|
<Styled.Option textColor={textColor} isHorizontal={isHorizontal} isMobile={isMobile} aria-describedby={`${key}-option-desc`}>{label}</Styled.Option>
|
|
|
|
{a.iconRight ? <Styled.IconRight color={textColor} iconName={a.iconRight} key="iconRight" /> : null}
|
|
|
|
</Styled.MenuItemWrapper>
|
|
|
|
</Styled.BBBMenuInformation>
|
|
|
|
),
|
2023-08-24 22:59:04 +08:00
|
|
|
a.isSeparator && <Divider disabled />
|
2021-08-09 01:13:47 +08:00
|
|
|
];
|
2023-07-20 00:22:50 +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;
|
2023-06-27 22:08:49 +08:00
|
|
|
const {
|
|
|
|
trigger,
|
|
|
|
intl,
|
|
|
|
customStyles,
|
|
|
|
dataTest,
|
|
|
|
opts,
|
|
|
|
accessKey,
|
|
|
|
open,
|
|
|
|
renderOtherComponents,
|
|
|
|
customAnchorEl,
|
|
|
|
hasRoundedCorners,
|
|
|
|
overrideMobileStyles,
|
2023-08-04 02:48:47 +08:00
|
|
|
isHorizontal,
|
2023-06-27 22:08:49 +08:00
|
|
|
} = this.props;
|
2021-07-08 19:41:03 +08:00
|
|
|
const actionsItems = this.makeMenuItems();
|
2021-07-19 01:30:27 +08:00
|
|
|
|
2023-06-14 01:51:42 +08:00
|
|
|
const roundedCornersStyles = { borderRadius: '1.8rem' };
|
2023-05-16 03:21:27 +08:00
|
|
|
let menuStyles = { zIndex: 999 };
|
2021-11-06 02:38:01 +08:00
|
|
|
|
|
|
|
if (customStyles) {
|
|
|
|
menuStyles = { ...menuStyles, ...customStyles };
|
|
|
|
}
|
|
|
|
|
2023-08-04 02:48:47 +08:00
|
|
|
if (isHorizontal) {
|
|
|
|
const horizontalStyles = { display: 'flex' };
|
|
|
|
menuStyles = { ...menuStyles, ...horizontalStyles};
|
|
|
|
}
|
|
|
|
|
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();
|
2022-03-28 22:10:54 +08:00
|
|
|
const firefoxInputSource = !([1, 5].includes(e.nativeEvent.mozInputSource)); // 1 = mouse, 5 = touch (firefox only)
|
|
|
|
const chromeInputSource = !(['mouse', 'touch'].includes(e.nativeEvent.pointerType));
|
|
|
|
|
2022-05-13 21:42:19 +08:00
|
|
|
this.optsToMerge.autoFocus = firefoxInputSource && chromeInputSource;
|
2021-09-02 06:17:18 +08:00
|
|
|
this.handleClick(e);
|
|
|
|
}}
|
2021-09-14 09:50:24 +08:00
|
|
|
onKeyPress={(e) => {
|
|
|
|
e.persist();
|
2023-05-03 03:24:06 +08:00
|
|
|
if (e.which !== KEY_CODES.ENTER) return null;
|
2021-09-14 09:50:24 +08:00
|
|
|
this.handleClick(e);
|
|
|
|
}}
|
2023-04-01 02:06:41 +08:00
|
|
|
accessKey={accessKey}
|
2022-04-01 00:58:41 +08:00
|
|
|
ref={(ref) => this.anchorElRef = ref}
|
2023-05-03 03:24:06 +08:00
|
|
|
role="button"
|
2023-05-10 10:15:08 +08:00
|
|
|
tabIndex={-1}
|
2021-09-02 06:17:18 +08:00
|
|
|
>
|
|
|
|
{trigger}
|
|
|
|
</div>
|
|
|
|
|
2023-08-04 02:48:47 +08:00
|
|
|
<Styled.MenuWrapper
|
2022-05-13 21:42:19 +08:00
|
|
|
{...opts}
|
|
|
|
{...this.optsToMerge}
|
2023-06-14 01:51:42 +08:00
|
|
|
anchorEl={customAnchorEl ? customAnchorEl : anchorEl}
|
2021-07-08 19:41:03 +08:00
|
|
|
open={Boolean(anchorEl)}
|
|
|
|
onClose={this.handleClose}
|
2021-11-06 02:38:01 +08:00
|
|
|
style={menuStyles}
|
2022-01-29 03:52:22 +08:00
|
|
|
data-test={dataTest}
|
2023-04-01 02:06:41 +08:00
|
|
|
onKeyDownCapture={this.handleKeyDown}
|
2023-08-14 21:47:23 +08:00
|
|
|
$isHorizontal={isHorizontal}
|
2023-06-27 22:08:49 +08:00
|
|
|
PaperProps={{
|
|
|
|
style: hasRoundedCorners ? roundedCornersStyles : {},
|
|
|
|
className: overrideMobileStyles ? 'override-mobile-styles' : 'MuiPaper-root-mobile',
|
|
|
|
}}
|
2021-07-08 19:41:03 +08:00
|
|
|
>
|
|
|
|
{actionsItems}
|
2023-04-28 22:35:41 +08:00
|
|
|
{renderOtherComponents}
|
2023-06-27 22:08:49 +08:00
|
|
|
{!overrideMobileStyles && anchorEl && window.innerWidth < SMALL_VIEWPORT_BREAKPOINT &&
|
2021-11-06 03:59:01 +08:00
|
|
|
<Styled.CloseButton
|
2021-07-08 19:41:03 +08:00
|
|
|
label={intl.formatMessage(intlMessages.close)}
|
|
|
|
size="lg"
|
|
|
|
color="default"
|
|
|
|
onClick={this.handleClose}
|
|
|
|
/>
|
|
|
|
}
|
2023-08-04 02:48:47 +08:00
|
|
|
</Styled.MenuWrapper>
|
2021-08-17 11:18:49 +08:00
|
|
|
</>
|
2021-07-08 19:41:03 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-05-03 03:24:06 +08:00
|
|
|
getcontentanchorel: null,
|
2021-07-08 19:41:03 +08:00
|
|
|
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: () => { },
|
2023-05-03 03:24:06 +08:00
|
|
|
dataTest: '',
|
2021-07-08 19:41:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
BBBMenu.propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
|
|
|
|
trigger: PropTypes.element.isRequired,
|
|
|
|
|
2023-07-20 00:22:50 +08:00
|
|
|
actions: PropTypes.array.isRequired,
|
2021-07-08 19:41:03 +08:00
|
|
|
|
|
|
|
onCloseCallback: PropTypes.func,
|
2022-01-29 03:52:22 +08:00
|
|
|
dataTest: PropTypes.string,
|
2023-05-03 03:24:06 +08:00
|
|
|
open: PropTypes.bool,
|
|
|
|
customStyles: PropTypes.object,
|
|
|
|
opts: PropTypes.object,
|
|
|
|
accessKey: PropTypes.string,
|
2021-07-08 19:41:03 +08:00
|
|
|
};
|
2023-05-03 03:24:06 +08:00
|
|
|
|
|
|
|
export default injectIntl(BBBMenu);
|