import React from 'react'; import PropTypes from 'prop-types'; import listener from '../context-menu/globalEventListener'; import { Dropdown, DropdownMenu, DropdownToggle, DropdownItem } from 'reactstrap'; import { gettext } from '../../utils/constants'; import { Utils } from '../../utils/utils'; import ModalPortal from '../modal-portal'; import '../../css/item-dropdown-menu.css'; const propTypes = { tagName: PropTypes.string, item: PropTypes.object.isRequired, toggleClass: PropTypes.string, toggleChildren: PropTypes.object, isHandleContextMenuEvent: PropTypes.bool, getMenuList: PropTypes.func.isRequired, onMenuItemClick: PropTypes.func.isRequired, freezeItem: PropTypes.func, unfreezeItem: PropTypes.func, menuStyle: PropTypes.object, isDisplayFiles: PropTypes.bool, }; class ItemDropdownMenu extends React.Component { static defaultProps = { isHandleContextMenuEvent: true, menuStyle: {}, toggleClass: 'sf3-font-more sf3-font' }; constructor(props) { super(props); this.state = { menuList: [], isItemMenuShow: false, isSubMenuShown: false, currentItem: '' }; } componentDidMount() { if (this.props.isHandleContextMenuEvent) { this.listenerId = listener.register(this.onShowMenu, this.onHideMenu); } let { item } = this.props; let menuList = this.props.getMenuList(item); this.setState({ menuList: menuList }); } UNSAFE_componentWillReceiveProps(nextProps) { // for toolbar item operation let { item } = nextProps; if (item.name !== this.props.item.name) { let menuList = this.props.getMenuList(item); this.setState({ menuList: menuList }); } } componentWillUnmount() { if (this.props.isHandleContextMenuEvent && this.listenerId) { listener.unregister(this.listenerId); } } onShowMenu = () => { // nothing todo }; onHideMenu = () => { if (this.state.isItemMenuShow) { this.setState({ isItemMenuShow: false }); if (typeof(this.props.unfreezeItem) === 'function') { this.props.unfreezeItem(); } } }; onDropdownToggleKeyDown = (e) => { if (e.key == 'Enter' || e.key == 'Space') { this.onDropdownToggleClick(e); } }; onDropdownToggleClick = (e) => { e.preventDefault(); e.stopPropagation(); this.toggleOperationMenu(); }; toggleOperationMenu = () => { this.setState( { isItemMenuShow: !this.state.isItemMenuShow }, () => { if (this.state.isItemMenuShow && typeof(this.props.freezeItem) === 'function') { this.props.freezeItem(); } else if (!this.state.isItemMenuShow && typeof(this.props.unfreezeItem) === 'function') { this.props.unfreezeItem(); } } ); }; onMenuItemKeyDown = (e) => { if (e.key == 'Enter' || e.key == 'Space') { this.onMenuItemClick(e); } }; onMenuItemClick = (event) => { let operation = Utils.getEventData(event, 'toggle') ?? event.currentTarget.getAttribute('data-toggle'); let item = this.props.item; this.props.onMenuItemClick(operation, event, item); }; onDropDownMouseMove = (e) => { if (this.state.isSubMenuShown && e.target && e.target.className === 'dropdown-item') { this.setState({ isSubMenuShown: false }); } }; toggleSubMenu = (e) => { e.stopPropagation(); this.setState({ isSubMenuShown: !this.state.isSubMenuShown }); }; toggleSubMenuShown = (item) => { this.setState({ isSubMenuShown: true, currentItem: item.key }); }; render() { let menuList = this.state.menuList; let { toggleClass, toggleChildren, tagName, menuStyle } = this.props; toggleClass = 'sf-dropdown-toggle ' + toggleClass; if (!menuList.length) { return ''; } if (tagName && tagName === 'button') { return ( {toggleChildren} {menuList.map((menuItem, index) => { if (menuItem === 'Divider') { return ; } else { return ( {menuItem.value} ); } })} ); } return ( {menuList.map((menuItem, index) => { if (menuItem === 'Divider') { return ; } else if (menuItem.subOpList) { return ( {e.stopPropagation();}} > {menuItem.value} {menuItem.subOpList.map((item, index) => { if (item == 'Divider') { return ; } else { return ( {item.value} ); } })} ); } else { return ( {menuItem.key === 'Display files' && this.props.isDisplayFiles && ( )} {menuItem.value} ); } })} ); } } ItemDropdownMenu.propTypes = propTypes; export default ItemDropdownMenu;