2019-07-11 08:35:17 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import { Icons } from '../icons';
|
|
|
|
|
|
|
|
const StyledOuter = styled.div`
|
|
|
|
width: ${props => props.size ? Math.abs(parseInt(props.size)) + "px" : "20px"};
|
2019-08-13 11:16:59 +00:00
|
|
|
cursor: ${props => props.isDisabled || !props.isClickable ? 'default' : 'pointer'};
|
2019-07-11 09:38:30 +00:00
|
|
|
line-height: 0;
|
2019-07-11 08:35:17 +00:00
|
|
|
`;
|
2019-07-18 13:21:22 +00:00
|
|
|
class IconButton extends React.Component{
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2019-07-18 13:53:59 +00:00
|
|
|
currentIconName: this.props.iconName,
|
|
|
|
currentIconColor: this.props.color
|
2019-07-18 13:21:22 +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-08-09 08:24:52 +00:00
|
|
|
|
|
|
|
this.isNeedUpdate = false;
|
2019-07-18 13:21:22 +00:00
|
|
|
}
|
2019-07-11 08:35:17 +00:00
|
|
|
|
2019-07-18 13:21:22 +00:00
|
|
|
|
|
|
|
onMouseEnter(e){
|
2019-08-08 09:45:55 +00:00
|
|
|
if(!this.props.isDisabled){
|
|
|
|
this.setState({
|
|
|
|
currentIconName: this.props.iconHoverName ? this.props.iconHoverName : this.props.iconName,
|
|
|
|
currentIconColor: this.props.hoverColor ? this.props.hoverColor : this.props.color
|
|
|
|
});
|
|
|
|
this.props.onMouseEnter && this.props.onMouseEnter(e);
|
|
|
|
}
|
2019-07-18 13:21:22 +00:00
|
|
|
}
|
|
|
|
onMouseLeave(e){
|
2019-08-08 09:45:55 +00:00
|
|
|
if(!this.props.isDisabled){
|
|
|
|
this.setState({
|
|
|
|
currentIconName: this.props.iconName,
|
|
|
|
currentIconColor: this.props.color
|
|
|
|
});
|
|
|
|
this.props.onMouseDown && this.props.onMouseDown(e);
|
|
|
|
}
|
2019-07-18 13:21:22 +00:00
|
|
|
}
|
|
|
|
onMouseDown(e){
|
2019-08-08 09:45:55 +00:00
|
|
|
if(!this.props.isDisabled){
|
|
|
|
this.setState({
|
|
|
|
currentIconName: this.props.iconClickName ? this.props.iconClickName : this.props.iconName,
|
|
|
|
currentIconColor: this.props.clickColor ? this.props.clickColor : this.props.color
|
|
|
|
});
|
|
|
|
this.props.onMouseDown && this.props.onMouseDown(e);
|
|
|
|
}
|
2019-07-18 13:21:22 +00:00
|
|
|
}
|
|
|
|
onMouseUp(e){
|
2019-08-08 09:45:55 +00:00
|
|
|
if(!this.props.isDisabled){
|
|
|
|
switch (e.nativeEvent.which) {
|
|
|
|
case 1: //Left click
|
|
|
|
this.setState({
|
|
|
|
currentIconName: this.props.iconHoverName ? this.props.iconHoverName : this.props.iconName,
|
|
|
|
currentIconColor: this.props.iconHoverName ? this.props.iconHoverName : this.props.color
|
|
|
|
});
|
|
|
|
this.props.onClick && this.props.onClick(e);
|
|
|
|
this.props.onMouseUp && this.props.onMouseUp(e);
|
|
|
|
break;
|
|
|
|
case 3://Right click
|
|
|
|
this.props.onMouseUp && this.props.onMouseUp(e);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-07-19 09:47:35 +00:00
|
|
|
}
|
2019-07-18 13:21:22 +00:00
|
|
|
}
|
2019-08-09 08:24:52 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState){
|
|
|
|
if(!this.isNeedUpdate){
|
|
|
|
for (let propsKey in this.props) {
|
|
|
|
if(typeof this.props[propsKey] != "function" && typeof this.props[propsKey] != "object" && this.props[propsKey] != nextProps[propsKey]){
|
|
|
|
this.isNeedUpdate = true;
|
2019-08-13 07:29:51 +00:00
|
|
|
if(propsKey == "iconName"){
|
|
|
|
this.setState({
|
2019-08-13 11:16:59 +00:00
|
|
|
currentIconName: nextProps[propsKey]
|
2019-08-13 07:29:51 +00:00
|
|
|
});
|
2019-08-13 08:07:35 +00:00
|
|
|
break;
|
2019-08-13 07:29:51 +00:00
|
|
|
}
|
2019-08-09 08:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let stateKey in this.state) {
|
|
|
|
if(typeof this.state[stateKey] != "function" && typeof this.state[stateKey] != "object" && this.state[stateKey] != nextState[stateKey]){
|
|
|
|
this.isNeedUpdate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!this.isNeedUpdate) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
this.isNeedUpdate = false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-18 13:21:22 +00:00
|
|
|
render(){
|
2019-07-24 17:39:19 +00:00
|
|
|
//console.log("IconButton render");
|
2019-07-18 13:21:22 +00:00
|
|
|
return (
|
|
|
|
<StyledOuter
|
|
|
|
size={this.props.size}
|
|
|
|
isDisabled={this.props.isDisabled}
|
2019-08-12 11:22:37 +00:00
|
|
|
|
2019-07-18 13:21:22 +00:00
|
|
|
onMouseEnter={this.onMouseEnter}
|
|
|
|
onMouseLeave={this.onMouseLeave}
|
|
|
|
onMouseDown={this.onMouseDown}
|
|
|
|
onMouseUp={this.onMouseUp}
|
2019-08-13 11:16:59 +00:00
|
|
|
|
|
|
|
isClickable={typeof this.props.onClick === 'function'}
|
2019-07-18 13:21:22 +00:00
|
|
|
>
|
2019-07-18 13:53:59 +00:00
|
|
|
{React.createElement(Icons[this.state.currentIconName], {size: "scale", color: this.state.currentIconColor, isfill: this.props.isFill})}
|
2019-07-18 13:21:22 +00:00
|
|
|
</StyledOuter>
|
|
|
|
);
|
|
|
|
};
|
2019-07-11 08:35:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IconButton.propTypes = {
|
|
|
|
color: PropTypes.string,
|
2019-07-18 13:53:59 +00:00
|
|
|
hoverColor: PropTypes.string,
|
|
|
|
clickColor: PropTypes.string,
|
2019-07-11 08:35:17 +00:00
|
|
|
size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
|
|
isFill: PropTypes.bool,
|
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
iconName: PropTypes.string.isRequired,
|
2019-07-18 13:21:22 +00:00
|
|
|
iconHoverName: PropTypes.string,
|
|
|
|
iconClickName: PropTypes.string,
|
2019-07-11 08:35:17 +00:00
|
|
|
onClick:PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
IconButton.defaultProps = {
|
|
|
|
color: "#d0d5da",
|
|
|
|
size: 25,
|
|
|
|
isFill: true,
|
|
|
|
iconName: "AZSortingIcon",
|
|
|
|
isDisabled: false
|
|
|
|
};
|
|
|
|
|
|
|
|
export default IconButton;
|