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-06-24 13:50:54 +00:00
|
|
|
|
|
|
|
const StyledOuther = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
position: relative;
|
2019-06-25 09:02:44 +00:00
|
|
|
cursor: pointer;
|
2019-06-24 13:50:54 +00:00
|
|
|
`;
|
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
class ContextMenuButton extends React.Component {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = (e) => !this.ref.current.contains(e.target) && this.toggle(false);
|
|
|
|
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
|
|
|
componentDidMount() {
|
|
|
|
if (this.ref.current) {
|
|
|
|
document.addEventListener("click", this.handleClick);
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener("click", this.handleClick)
|
|
|
|
}
|
2019-06-26 14:29:18 +00:00
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
// Store prevId in state so we can compare when props change.
|
|
|
|
// Clear out previously-loaded data (so we don't render stale stuff).
|
|
|
|
if (this.props.opened !== prevProps.opened) {
|
|
|
|
this.toggle(this.props.opened);
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:50:54 +00:00
|
|
|
|
2019-07-14 18:23:55 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<StyledOuther ref={this.ref}>
|
|
|
|
<IconButton
|
|
|
|
color={this.props.color}
|
2019-07-18 13:54:23 +00:00
|
|
|
hoverColor={this.props.hoverColor}
|
|
|
|
clickColor={this.props.clickColor}
|
2019-07-14 18:23:55 +00:00
|
|
|
size={this.props.size}
|
|
|
|
iconName={this.props.iconName}
|
2019-07-18 13:22:29 +00:00
|
|
|
iconHoverName={this.props.iconHoverName}
|
|
|
|
iconClickName={this.props.iconClickName}
|
2019-07-14 18:23:55 +00:00
|
|
|
isFill={false}
|
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
onClick={
|
|
|
|
!this.props.isDisabled
|
|
|
|
? () => {
|
|
|
|
this.setState({ data: this.props.getData()});
|
|
|
|
this.toggle(!this.state.isOpen);
|
|
|
|
}
|
|
|
|
: this.stopAction
|
|
|
|
}
|
2019-07-18 09:09:05 +00:00
|
|
|
onMouseEnter={this.props.onMouseEnter}
|
|
|
|
onMouseLeave={this.props.onMouseLeave}
|
|
|
|
onMouseOver={this.props.onMouseOver}
|
|
|
|
onMouseOut={this.props.onMouseOut}
|
2019-07-14 18:23:55 +00:00
|
|
|
/>
|
2019-07-18 14:21:43 +00:00
|
|
|
<DropDown directionX={this.props.directionX || 'left'} isOpen={this.state.isOpen}>
|
2019-06-24 13:50:54 +00:00
|
|
|
{
|
2019-07-14 18:23:55 +00:00
|
|
|
this.state.data.map(item =>
|
|
|
|
<DropDownItem
|
|
|
|
{...item}
|
|
|
|
onClick={() => {
|
|
|
|
item.onClick && item.onClick();
|
|
|
|
this.toggle(!this.state.isOpen);
|
2019-07-15 08:50:23 +00:00
|
|
|
}}
|
2019-07-14 18:23:55 +00:00
|
|
|
/>
|
|
|
|
)
|
2019-06-24 13:50:54 +00:00
|
|
|
}
|
|
|
|
</DropDown>
|
|
|
|
</StyledOuther>
|
2019-07-14 18:23:55 +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,
|
|
|
|
isDisabled: PropTypes.bool
|
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-07-14 18:23:55 +00:00
|
|
|
isDisabled: false
|
2019-06-24 13:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ContextMenuButton
|