2019-07-14 18:23:55 +00:00
|
|
|
import React from 'react'
|
2019-06-24 13:50:54 +00:00
|
|
|
import styled from 'styled-components'
|
|
|
|
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'
|
2019-08-02 07:35:54 +00:00
|
|
|
import { handleAnyClick } from '../../utils/event';
|
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;
|
2019-06-24 13:50:54 +00:00
|
|
|
`;
|
|
|
|
|
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,
|
|
|
|
data: props.data
|
|
|
|
};
|
2019-07-24 17:43:18 +00:00
|
|
|
|
2019-07-24 17:57:22 +00:00
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.stopAction = this.stopAction.bind(this);
|
|
|
|
this.toggle = this.toggle.bind(this);
|
2019-07-31 18:57:16 +00:00
|
|
|
this.onIconButtonClick = this.onIconButtonClick.bind(this);
|
|
|
|
this.onDropDownItemClick = this.onDropDownItemClick.bind(this);
|
2019-08-02 07:35:54 +00:00
|
|
|
|
2019-10-31 11:42:30 +00:00
|
|
|
if (props.opened)
|
2019-08-02 07:35:54 +00:00
|
|
|
handleAnyClick(true, this.handleClick);
|
2019-07-14 18:23:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 07:35:54 +00:00
|
|
|
handleClick = (e) => this.state.isOpen && !this.ref.current.contains(e.target) && this.toggle(false);
|
2019-07-14 18:23:55 +00:00
|
|
|
stopAction = (e) => e.preventDefault();
|
|
|
|
toggle = (isOpen) => this.setState({ isOpen: isOpen });
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
componentWillUnmount() {
|
2019-08-02 07:35:54 +00:00
|
|
|
handleAnyClick(false, this.handleClick);
|
2019-07-14 18:23:55 +00:00
|
|
|
}
|
2019-06-26 14:29:18 +00:00
|
|
|
|
2019-08-02 07:35:54 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2019-07-14 18:23:55 +00:00
|
|
|
if (this.props.opened !== prevProps.opened) {
|
|
|
|
this.toggle(this.props.opened);
|
|
|
|
}
|
2019-08-02 07:35:54 +00:00
|
|
|
|
2019-10-31 11:42:30 +00:00
|
|
|
if (this.state.isOpen !== prevState.isOpen) {
|
2019-08-02 07:35:54 +00:00
|
|
|
handleAnyClick(this.state.isOpen, this.handleClick);
|
|
|
|
}
|
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 = () => {
|
2019-10-31 11:42:30 +00:00
|
|
|
if (!this.props.isDisabled) {
|
|
|
|
this.setState({
|
|
|
|
data: this.props.getData(),
|
|
|
|
isOpen: !this.state.isOpen
|
|
|
|
});
|
2019-07-31 18:57:16 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.stopAction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onDropDownItemClick = (item) => {
|
|
|
|
item.onClick && item.onClick();
|
|
|
|
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 {
|
|
|
|
color,
|
|
|
|
hoverColor,
|
|
|
|
clickColor,
|
|
|
|
size,
|
|
|
|
iconName,
|
|
|
|
iconHoverName,
|
|
|
|
iconClickName,
|
|
|
|
isDisabled,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
|
|
|
onMouseOver,
|
|
|
|
onMouseOut,
|
|
|
|
directionX
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const { isOpen } = this.state;
|
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
return (
|
2019-10-31 11:42:30 +00:00
|
|
|
<StyledOuter ref={this.ref}>
|
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}
|
|
|
|
iconName={iconName}
|
|
|
|
iconHoverName={iconHoverName}
|
|
|
|
iconClickName={iconClickName}
|
2019-07-14 18:23:55 +00:00
|
|
|
isFill={false}
|
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
|
|
|
/>
|
2019-10-31 11:42:30 +00:00
|
|
|
<DropDown directionX={directionX} isOpen={isOpen}>
|
2019-06-24 13:50:54 +00:00
|
|
|
{
|
2019-10-31 11:42:30 +00:00
|
|
|
this.state.data.map((item, index) =>
|
2019-11-06 07:02:04 +00:00
|
|
|
(item && (item.label || item.icon)) && <DropDownItem {...item} key={item.key || index} onClick={this.onDropDownItemClick.bind(this, item)}
|
2019-07-14 18:23:55 +00:00
|
|
|
/>
|
|
|
|
)
|
2019-06-24 13:50:54 +00:00
|
|
|
}
|
|
|
|
</DropDown>
|
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,
|
|
|
|
|
|
|
|
onMouseEnter: PropTypes.func,
|
|
|
|
onMouseLeave: PropTypes.func,
|
|
|
|
onMouseOver: PropTypes.func,
|
|
|
|
onMouseOut: PropTypes.func,
|
|
|
|
|
|
|
|
directionX: 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,
|
|
|
|
directionX: 'left'
|
2019-06-24 13:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ContextMenuButton
|