2019-07-11 08:35:17 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
2019-09-29 14:28:56 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
import { Icons } from "../icons";
|
|
|
|
import isEmpty from "lodash/isEmpty";
|
2019-07-11 08:35:17 +00:00
|
|
|
|
|
|
|
const StyledOuter = styled.div`
|
2020-10-16 13:16:01 +00:00
|
|
|
width: ${(props) =>
|
2019-09-29 14:28:56 +00:00
|
|
|
props.size ? Math.abs(parseInt(props.size)) + "px" : "20px"};
|
2020-10-16 13:16:01 +00:00
|
|
|
cursor: ${(props) =>
|
2019-09-29 14:28:56 +00:00
|
|
|
props.isDisabled || !props.isClickable ? "default" : "pointer"};
|
2020-10-16 13:16:01 +00:00
|
|
|
line-height: 0;
|
|
|
|
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
|
2019-07-11 08:35:17 +00:00
|
|
|
`;
|
2019-09-12 11:10:15 +00:00
|
|
|
class IconButton extends React.PureComponent {
|
2019-09-29 14:28:56 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-07-11 08:35:17 +00:00
|
|
|
|
2019-09-29 14:28:56 +00:00
|
|
|
this.state = {
|
|
|
|
currentIconName: this.props.iconName,
|
2020-10-16 13:16:01 +00:00
|
|
|
currentIconColor: this.props.color,
|
2019-09-29 14:28:56 +00:00
|
|
|
};
|
|
|
|
this.onMouseEnter = this.onMouseEnter.bind(this);
|
|
|
|
this.onMouseLeave = this.onMouseLeave.bind(this);
|
|
|
|
this.onMouseDown = this.onMouseDown.bind(this);
|
|
|
|
this.onMouseUp = this.onMouseUp.bind(this);
|
2019-09-12 06:51:15 +00:00
|
|
|
|
2019-09-29 14:28:56 +00:00
|
|
|
this.isNeedUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseEnter(e) {
|
|
|
|
const {
|
|
|
|
isDisabled,
|
|
|
|
iconHoverName,
|
|
|
|
iconName,
|
|
|
|
hoverColor,
|
|
|
|
color,
|
2020-10-16 13:16:01 +00:00
|
|
|
onMouseEnter,
|
2019-09-29 14:28:56 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (isDisabled) return;
|
|
|
|
|
|
|
|
this.setState({
|
2020-10-16 13:16:01 +00:00
|
|
|
currentIconName: !("ontouchstart" in document.documentElement)
|
|
|
|
? iconHoverName || iconName
|
|
|
|
: iconName,
|
|
|
|
currentIconColor: hoverColor || color,
|
2019-09-29 14:28:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
onMouseEnter && onMouseEnter(e);
|
|
|
|
}
|
|
|
|
onMouseLeave(e) {
|
2020-01-10 11:34:41 +00:00
|
|
|
const { isDisabled, iconName, color, onMouseLeave } = this.props;
|
2019-09-29 14:28:56 +00:00
|
|
|
|
|
|
|
if (isDisabled) return;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
currentIconName: iconName,
|
2020-10-16 13:16:01 +00:00
|
|
|
currentIconColor: color,
|
2019-09-29 14:28:56 +00:00
|
|
|
});
|
|
|
|
|
2020-01-10 11:34:41 +00:00
|
|
|
onMouseLeave && onMouseLeave(e);
|
2019-09-29 14:28:56 +00:00
|
|
|
}
|
|
|
|
onMouseDown(e) {
|
|
|
|
const {
|
|
|
|
isDisabled,
|
|
|
|
iconClickName,
|
|
|
|
iconName,
|
|
|
|
clickColor,
|
|
|
|
color,
|
2020-10-16 13:16:01 +00:00
|
|
|
onMouseDown,
|
2019-09-29 14:28:56 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (isDisabled) return;
|
|
|
|
|
|
|
|
this.setState({
|
2020-10-16 13:16:01 +00:00
|
|
|
currentIconName: !("ontouchstart" in document.documentElement)
|
|
|
|
? iconClickName || iconName
|
|
|
|
: iconName,
|
|
|
|
currentIconColor: clickColor || color,
|
2019-09-29 14:28:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
onMouseDown && onMouseDown(e);
|
|
|
|
}
|
|
|
|
onMouseUp(e) {
|
|
|
|
const {
|
|
|
|
isDisabled,
|
|
|
|
iconHoverName,
|
|
|
|
iconName,
|
|
|
|
color,
|
|
|
|
onClick,
|
2020-10-16 13:16:01 +00:00
|
|
|
onMouseUp,
|
2019-09-29 14:28:56 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (isDisabled) return;
|
|
|
|
|
|
|
|
switch (e.nativeEvent.which) {
|
|
|
|
case 1: //Left click
|
|
|
|
this.setState({
|
2020-10-16 13:16:01 +00:00
|
|
|
currentIconName: !("ontouchstart" in document.documentElement)
|
|
|
|
? iconHoverName || iconName
|
|
|
|
: iconName,
|
|
|
|
currentIconColor: iconHoverName || color,
|
2019-09-29 14:28:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
onClick && onClick(e);
|
|
|
|
onMouseUp && onMouseUp(e);
|
|
|
|
break;
|
|
|
|
case 3: //Right click
|
|
|
|
onMouseUp && onMouseUp(e);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-07-18 13:21:22 +00:00
|
|
|
}
|
2019-09-29 14:28:56 +00:00
|
|
|
}
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { iconName, color } = this.props;
|
|
|
|
|
|
|
|
let newState = {};
|
|
|
|
|
|
|
|
if (iconName !== prevProps.iconName) {
|
|
|
|
newState.currentIconName = iconName;
|
2019-08-09 08:24:52 +00:00
|
|
|
}
|
2019-09-29 14:28:56 +00:00
|
|
|
if (color !== prevProps.color) {
|
|
|
|
newState.currentIconColor = color;
|
2019-09-10 07:03:36 +00:00
|
|
|
}
|
2019-09-29 14:28:56 +00:00
|
|
|
|
|
|
|
if (!isEmpty(newState)) this.setState(newState);
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
//console.log("IconButton render");
|
|
|
|
const {
|
|
|
|
className,
|
|
|
|
size,
|
|
|
|
isDisabled,
|
|
|
|
isFill,
|
|
|
|
isClickable,
|
2019-10-18 06:51:03 +00:00
|
|
|
onClick,
|
2019-11-06 14:41:35 +00:00
|
|
|
id,
|
2019-12-05 11:17:45 +00:00
|
|
|
style,
|
2020-10-16 13:16:01 +00:00
|
|
|
dataTip,
|
2019-09-29 14:28:56 +00:00
|
|
|
} = this.props;
|
2019-10-17 13:15:43 +00:00
|
|
|
|
2019-09-29 14:28:56 +00:00
|
|
|
return (
|
|
|
|
<StyledOuter
|
|
|
|
className={className}
|
|
|
|
size={size}
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
onMouseEnter={this.onMouseEnter}
|
|
|
|
onMouseLeave={this.onMouseLeave}
|
|
|
|
onMouseDown={this.onMouseDown}
|
|
|
|
onMouseUp={this.onMouseUp}
|
|
|
|
isClickable={typeof onClick === "function" || isClickable}
|
2019-11-06 14:41:35 +00:00
|
|
|
data-tip={dataTip}
|
2019-10-18 06:51:03 +00:00
|
|
|
data-event="click focus"
|
|
|
|
data-for={id}
|
2019-11-28 11:00:59 +00:00
|
|
|
style={style}
|
2019-10-17 13:15:43 +00:00
|
|
|
//{...this.props}
|
2019-09-29 14:28:56 +00:00
|
|
|
>
|
|
|
|
{React.createElement(Icons[this.state.currentIconName], {
|
|
|
|
size: "scale",
|
|
|
|
color: this.state.currentIconColor,
|
2020-10-16 13:16:01 +00:00
|
|
|
isfill: isFill,
|
2019-09-29 14:28:56 +00:00
|
|
|
})}
|
|
|
|
</StyledOuter>
|
|
|
|
);
|
|
|
|
}
|
2019-09-10 07:03:36 +00:00
|
|
|
}
|
2019-07-11 08:35:17 +00:00
|
|
|
|
|
|
|
IconButton.propTypes = {
|
2019-09-29 14:28:56 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
color: PropTypes.string,
|
|
|
|
hoverColor: PropTypes.string,
|
|
|
|
clickColor: PropTypes.string,
|
|
|
|
size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
|
|
isFill: PropTypes.bool,
|
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
isClickable: PropTypes.bool,
|
|
|
|
iconName: PropTypes.string.isRequired,
|
|
|
|
iconHoverName: PropTypes.string,
|
|
|
|
iconClickName: PropTypes.string,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
onMouseEnter: PropTypes.func,
|
|
|
|
onMouseDown: PropTypes.func,
|
2019-10-18 06:51:03 +00:00
|
|
|
onMouseUp: PropTypes.func,
|
2020-01-10 11:34:41 +00:00
|
|
|
onMouseLeave: PropTypes.func,
|
2019-11-06 14:41:35 +00:00
|
|
|
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
2019-12-05 11:17:45 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2020-10-16 13:16:01 +00:00
|
|
|
dataTip: PropTypes.string,
|
2019-07-11 08:35:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IconButton.defaultProps = {
|
2019-09-29 14:28:56 +00:00
|
|
|
color: "#d0d5da",
|
|
|
|
size: 25,
|
|
|
|
isFill: true,
|
|
|
|
iconName: "AZSortingIcon",
|
|
|
|
isDisabled: false,
|
2019-11-06 14:41:35 +00:00
|
|
|
isClickable: false,
|
2020-10-16 13:16:01 +00:00
|
|
|
dataTip: "",
|
2019-07-11 08:35:17 +00:00
|
|
|
};
|
|
|
|
|
2019-09-29 14:28:56 +00:00
|
|
|
export default IconButton;
|