2019-07-14 18:23:55 +00:00
|
|
|
import React from 'react'
|
2020-08-19 07:36:27 +00:00
|
|
|
import styled, { css } from 'styled-components'
|
2019-06-24 13:50:54 +00:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import DropDownItem from '../drop-down-item'
|
|
|
|
import DropDown from '../drop-down'
|
2019-07-14 18:23:55 +00:00
|
|
|
import IconButton from '../icon-button'
|
2020-08-19 07:36:27 +00:00
|
|
|
import Backdrop from '../backdrop'
|
|
|
|
import Aside from '../aside'
|
|
|
|
import Heading from '../heading'
|
|
|
|
import Text from '../text'
|
|
|
|
import Link from '../link'
|
|
|
|
import { desktop } from '../../utils/device'
|
|
|
|
import throttle from "lodash/throttle";
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-10-31 11:42:30 +00:00
|
|
|
const StyledOuter = styled.div`
|
2019-06-24 13:50:54 +00:00
|
|
|
display: inline-block;
|
|
|
|
position: relative;
|
2019-06-25 09:02:44 +00:00
|
|
|
cursor: pointer;
|
2020-01-31 14:15:21 +00:00
|
|
|
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
|
2019-06-24 13:50:54 +00:00
|
|
|
`;
|
|
|
|
|
2020-08-19 07:36:27 +00:00
|
|
|
const Content = styled.div`
|
|
|
|
box-sizing: border-box;
|
|
|
|
position: relative;
|
|
|
|
width: 100%;
|
|
|
|
background-color: #fff;
|
|
|
|
padding: 0 16px 16px;
|
|
|
|
|
|
|
|
.header {
|
|
|
|
max-width: 500px;
|
|
|
|
margin: 0;
|
|
|
|
line-height: 56px;
|
|
|
|
font-weight: 700 !important;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Header = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
border-bottom: 1px solid #dee2e6;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Body = styled.div`
|
|
|
|
position: relative;
|
|
|
|
padding: 16px 0;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
|
|
.context-menu-button_link {
|
|
|
|
margin-top: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.context-menu-button_link-header:not(:first-child) {
|
|
|
|
margin-top: 32px;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-09-13 12:35:37 +00:00
|
|
|
class ContextMenuButton extends React.Component {
|
2019-07-14 18:23:55 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
this.ref = React.createRef();
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
this.state = {
|
|
|
|
isOpen: props.opened,
|
2020-08-19 07:36:27 +00:00
|
|
|
data: props.data,
|
2020-08-20 14:40:35 +00:00
|
|
|
displayType: props.displayType
|
2019-07-14 18:23:55 +00:00
|
|
|
};
|
2020-08-19 07:36:27 +00:00
|
|
|
this.throttledResize = throttle(this.resize, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTypeByWidth = () => {
|
|
|
|
if (this.props.displayType !== "auto") return this.props.displayType;
|
|
|
|
return window.innerWidth < desktop.match(/\d+/)[0] ? "aside" : "dropdown";
|
|
|
|
};
|
|
|
|
|
|
|
|
resize = () => {
|
|
|
|
if (this.props.displayType !== "auto") return;
|
|
|
|
const type = this.getTypeByWidth();
|
|
|
|
if (type === this.state.displayType) return;
|
|
|
|
this.setState({ displayType: type });
|
|
|
|
};
|
|
|
|
|
|
|
|
popstate = () => {
|
|
|
|
window.removeEventListener("popstate", this.popstate, false);
|
|
|
|
this.onClose();
|
|
|
|
window.history.go(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener("resize", this.throttledResize);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener("resize", this.throttledResize);
|
2019-07-14 18:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stopAction = (e) => e.preventDefault();
|
|
|
|
toggle = (isOpen) => this.setState({ isOpen: isOpen });
|
2020-08-19 07:36:27 +00:00
|
|
|
onClose = () => this.setState({ isOpen: !this.state.isOpen });
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2020-01-20 13:25:05 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2019-07-14 18:23:55 +00:00
|
|
|
if (this.props.opened !== prevProps.opened) {
|
|
|
|
this.toggle(this.props.opened);
|
|
|
|
}
|
2020-08-19 07:36:27 +00:00
|
|
|
|
|
|
|
if (this.props.opened && this.state.displayType === "aside") {
|
|
|
|
window.addEventListener("popstate", this.popstate, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.displayType !== prevProps.displayType) {
|
|
|
|
this.setState({displayType: this.getTypeByWidth()});
|
|
|
|
}
|
2019-07-14 18:23:55 +00:00
|
|
|
}
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-07-31 18:57:16 +00:00
|
|
|
onIconButtonClick = () => {
|
2020-01-09 13:27:49 +00:00
|
|
|
if (this.props.isDisabled) {
|
|
|
|
this.stopAction;
|
|
|
|
return;
|
2019-07-31 18:57:16 +00:00
|
|
|
}
|
2020-01-09 13:27:49 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
data: this.props.getData(),
|
|
|
|
isOpen: !this.state.isOpen
|
2020-07-31 08:08:33 +00:00
|
|
|
}, () => !this.props.isDisabled && this.state.isOpen && this.props.onClick && this.props.onClick());
|
2020-01-09 13:27:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 13:25:05 +00:00
|
|
|
clickOutsideAction = (e) => {
|
|
|
|
if (this.ref.current.contains(e.target)) return;
|
|
|
|
|
2020-01-09 13:27:49 +00:00
|
|
|
this.onIconButtonClick();
|
2019-07-31 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 11:39:49 +00:00
|
|
|
onDropDownItemClick = (item, e) => {
|
|
|
|
item.onClick && item.onClick(e);
|
2019-07-31 18:57:16 +00:00
|
|
|
this.toggle(!this.state.isOpen);
|
|
|
|
}
|
|
|
|
|
2019-09-13 12:35:37 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
if (this.props.opened === nextProps.opened && this.state.isOpen === nextState.isOpen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
render() {
|
2019-07-26 12:35:41 +00:00
|
|
|
//console.log("ContextMenuButton render");
|
2019-10-31 11:42:30 +00:00
|
|
|
const {
|
2020-03-26 14:10:14 +00:00
|
|
|
className,
|
|
|
|
clickColor,
|
2019-10-31 11:42:30 +00:00
|
|
|
color,
|
2020-03-26 14:10:14 +00:00
|
|
|
columnCount,
|
|
|
|
directionX,
|
|
|
|
directionY,
|
2019-10-31 11:42:30 +00:00
|
|
|
hoverColor,
|
2020-03-26 14:10:14 +00:00
|
|
|
iconClickName,
|
|
|
|
iconHoverName,
|
2019-10-31 11:42:30 +00:00
|
|
|
iconName,
|
2020-01-09 07:28:45 +00:00
|
|
|
iconOpenName,
|
2020-03-26 14:10:14 +00:00
|
|
|
id,
|
2019-10-31 11:42:30 +00:00
|
|
|
isDisabled,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onMouseOut,
|
2020-03-26 14:10:14 +00:00
|
|
|
onMouseOver,
|
|
|
|
size,
|
2020-08-14 14:05:47 +00:00
|
|
|
style,
|
2020-08-19 07:36:27 +00:00
|
|
|
isFill,
|
|
|
|
asideHeader
|
2019-10-31 11:42:30 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2020-08-19 07:36:27 +00:00
|
|
|
const { isOpen, displayType } = this.state;
|
2020-01-09 07:28:45 +00:00
|
|
|
const iconButtonName = isOpen && iconOpenName ? iconOpenName : iconName;
|
2019-07-14 18:23:55 +00:00
|
|
|
return (
|
2019-11-27 13:33:53 +00:00
|
|
|
<StyledOuter ref={this.ref} className={className} id={id} style={style}>
|
2019-07-14 18:23:55 +00:00
|
|
|
<IconButton
|
2019-10-31 11:42:30 +00:00
|
|
|
color={color}
|
|
|
|
hoverColor={hoverColor}
|
|
|
|
clickColor={clickColor}
|
|
|
|
size={size}
|
2020-01-09 07:28:45 +00:00
|
|
|
iconName={iconButtonName}
|
2019-10-31 11:42:30 +00:00
|
|
|
iconHoverName={iconHoverName}
|
|
|
|
iconClickName={iconClickName}
|
2020-08-14 14:05:47 +00:00
|
|
|
isFill={isFill}
|
2019-10-31 11:42:30 +00:00
|
|
|
isDisabled={isDisabled}
|
2019-07-31 18:57:16 +00:00
|
|
|
onClick={this.onIconButtonClick}
|
2019-10-31 11:42:30 +00:00
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
onMouseOver={onMouseOver}
|
|
|
|
onMouseOut={onMouseOut}
|
2019-07-14 18:23:55 +00:00
|
|
|
/>
|
2020-08-19 07:36:27 +00:00
|
|
|
{displayType === 'dropdown' ?
|
|
|
|
<DropDown
|
|
|
|
directionX={directionX}
|
|
|
|
directionY={directionY}
|
|
|
|
open={isOpen}
|
|
|
|
clickOutsideAction={this.clickOutsideAction}
|
|
|
|
columnCount={columnCount}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
this.state.data.map((item, index) =>
|
|
|
|
(item && (item.label || item.icon || item.key)) && (
|
|
|
|
<DropDownItem {...item} key={item.key || index} onClick={this.onDropDownItemClick.bind(this, item)} />
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</DropDown>
|
|
|
|
: <>
|
|
|
|
<Backdrop
|
|
|
|
onClick={this.onClose}
|
|
|
|
visible={isOpen}
|
|
|
|
zIndex={310}
|
2019-07-14 18:23:55 +00:00
|
|
|
/>
|
2020-08-19 07:36:27 +00:00
|
|
|
<Aside visible={isOpen} scale={false} zIndex={310}>
|
|
|
|
<Content>
|
|
|
|
<Header>
|
|
|
|
<Heading className="header" size="medium" truncate={true}>
|
|
|
|
{asideHeader}
|
|
|
|
</Heading>
|
|
|
|
</Header>
|
|
|
|
<Body>
|
|
|
|
{this.state.data.map((item, index) =>
|
|
|
|
(item && (item.label || item.icon || item.key)) && (
|
|
|
|
<Link
|
|
|
|
className={`context-menu-button_link${item.isHeader ? '-header' : ''}`}
|
|
|
|
key={item.key || index}
|
|
|
|
fontSize={item.isHeader ? '15px' : '13px'}
|
|
|
|
noHover={item.isHeader}
|
|
|
|
fontWeight={600}
|
|
|
|
onClick={this.onDropDownItemClick.bind(this, item)}
|
|
|
|
>
|
|
|
|
{item.label}
|
|
|
|
</Link>
|
|
|
|
))}
|
|
|
|
</Body>
|
|
|
|
</Content>
|
|
|
|
</Aside>
|
|
|
|
</>
|
|
|
|
}
|
2019-10-31 11:42:30 +00:00
|
|
|
</StyledOuter>
|
2019-07-14 18:23:55 +00:00
|
|
|
);
|
2019-09-10 07:03:36 +00:00
|
|
|
}
|
2019-06-24 13:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ContextMenuButton.propTypes = {
|
|
|
|
opened: PropTypes.bool,
|
|
|
|
data: PropTypes.array,
|
|
|
|
getData: PropTypes.func.isRequired,
|
2019-06-25 09:02:44 +00:00
|
|
|
title: PropTypes.string,
|
2019-06-26 14:29:18 +00:00
|
|
|
iconName: PropTypes.string,
|
2019-07-15 09:44:55 +00:00
|
|
|
size: PropTypes.number,
|
2019-07-14 18:23:55 +00:00
|
|
|
color: PropTypes.string,
|
2019-10-31 11:42:30 +00:00
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
|
|
|
|
hoverColor: PropTypes.string,
|
|
|
|
clickColor: PropTypes.string,
|
|
|
|
|
|
|
|
iconHoverName: PropTypes.string,
|
|
|
|
iconClickName: PropTypes.string,
|
2020-01-09 07:28:45 +00:00
|
|
|
iconOpenName: PropTypes.string,
|
2019-10-31 11:42:30 +00:00
|
|
|
|
|
|
|
onMouseEnter: PropTypes.func,
|
|
|
|
onMouseLeave: PropTypes.func,
|
|
|
|
onMouseOver: PropTypes.func,
|
|
|
|
onMouseOut: PropTypes.func,
|
|
|
|
|
2019-11-27 13:33:53 +00:00
|
|
|
directionX: PropTypes.string,
|
2020-02-17 12:30:15 +00:00
|
|
|
directionY: PropTypes.string,
|
2019-11-27 13:33:53 +00:00
|
|
|
|
|
|
|
className: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
2020-03-26 14:10:14 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2020-08-19 07:36:27 +00:00
|
|
|
columnCount: PropTypes.number,
|
|
|
|
displayType: PropTypes.string
|
2019-06-24 13:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ContextMenuButton.defaultProps = {
|
|
|
|
opened: false,
|
|
|
|
data: [],
|
2019-06-25 09:02:44 +00:00
|
|
|
title: '',
|
2019-06-26 14:29:18 +00:00
|
|
|
iconName: 'VerticalDotsIcon',
|
2019-07-15 09:44:55 +00:00
|
|
|
size: 16,
|
2019-10-31 11:42:30 +00:00
|
|
|
isDisabled: false,
|
2020-08-14 14:05:47 +00:00
|
|
|
directionX: 'left',
|
2020-08-19 07:36:27 +00:00
|
|
|
isFill: false,
|
|
|
|
displayType: 'dropdown'
|
2019-06-24 13:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ContextMenuButton
|