DocSpace-client/web/ASC.Web.Components/src/components/context-menu-button/index.js

171 lines
4.1 KiB
JavaScript
Raw Normal View History

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'
import IconButton from '../icon-button'
2019-06-24 13:50:54 +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;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
2019-06-24 13:50:54 +00:00
`;
class ContextMenuButton extends React.Component {
constructor(props) {
super(props);
2019-06-24 13:50:54 +00:00
this.ref = React.createRef();
2019-06-24 13:50:54 +00:00
this.state = {
isOpen: props.opened,
data: props.data
};
}
handleClick = (e) => this.state.isOpen && !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
componentDidUpdate(prevProps) {
if (this.props.opened !== prevProps.opened) {
this.toggle(this.props.opened);
}
}
2019-06-24 13:50:54 +00:00
onIconButtonClick = () => {
2020-01-09 13:27:49 +00:00
if (this.props.isDisabled) {
this.stopAction;
return;
}
2020-01-09 13:27:49 +00:00
this.setState({
data: this.props.getData(),
isOpen: !this.state.isOpen
});
}
clickOutsideAction = (e) => {
if (this.ref.current.contains(e.target)) return;
2020-01-09 13:27:49 +00:00
this.onIconButtonClick();
}
onDropDownItemClick = (item, e) => {
item.onClick && item.onClick(e);
this.toggle(!this.state.isOpen);
}
shouldComponentUpdate(nextProps, nextState) {
if (this.props.opened === nextProps.opened && this.state.isOpen === nextState.isOpen) {
return false;
}
return true;
}
render() {
2019-07-26 12:35:41 +00:00
//console.log("ContextMenuButton render");
const {
className,
clickColor,
color,
columnCount,
directionX,
directionY,
hoverColor,
iconClickName,
iconHoverName,
iconName,
iconOpenName,
id,
isDisabled,
onMouseEnter,
onMouseLeave,
onMouseOut,
onMouseOver,
size,
style
} = this.props;
const { isOpen } = this.state;
const iconButtonName = isOpen && iconOpenName ? iconOpenName : iconName;
return (
<StyledOuter ref={this.ref} className={className} id={id} style={style}>
<IconButton
color={color}
hoverColor={hoverColor}
clickColor={clickColor}
size={size}
iconName={iconButtonName}
iconHoverName={iconHoverName}
iconClickName={iconClickName}
isFill={false}
isDisabled={isDisabled}
onClick={this.onIconButtonClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
/>
<DropDown
directionX={directionX}
directionY={directionY}
open={isOpen}
2020-01-09 13:27:49 +00:00
clickOutsideAction={this.clickOutsideAction}
columnCount={columnCount}
>
2019-06-24 13:50:54 +00:00
{
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)}
/>
)
2019-06-24 13:50:54 +00:00
}
</DropDown>
</StyledOuter>
);
}
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,
size: PropTypes.number,
color: PropTypes.string,
isDisabled: PropTypes.bool,
hoverColor: PropTypes.string,
clickColor: PropTypes.string,
iconHoverName: PropTypes.string,
iconClickName: PropTypes.string,
iconOpenName: PropTypes.string,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
onMouseOver: PropTypes.func,
onMouseOut: PropTypes.func,
directionX: PropTypes.string,
directionY: PropTypes.string,
className: PropTypes.string,
id: PropTypes.string,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
columnCount: PropTypes.number
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',
size: 16,
isDisabled: false,
directionX: 'left'
2019-06-24 13:50:54 +00:00
};
export default ContextMenuButton