-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIcon.jsx
executable file
·107 lines (93 loc) · 2.36 KB
/
Icon.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
class AbstractIcon extends Component {
state = {
doubleReady: false,
};
disableAction = () => {
this.setState({ doubleReady: false });
};
checkDoubleClick = () => {
this.handleClick();
if (!this.props.onDoubleClick) {
return;
}
if (this.state.doubleReady) {
this.props.onDoubleClick();
this.disableAction();
} else {
this.setState({ doubleReady: true });
setTimeout(this.disableAction, 700);
}
};
handleClick = e => {
this.icon.focus();
if (this.props.onClick) {
this.props.onClick(e);
}
};
handleContextMenu = e => {
e.preventDefault();
this.icon.focus();
if (this.props.onContextMenu) {
this.props.onContextMenu(e);
}
//return false;
};
render() {
const { props } = this;
const Comp = props.href ? 'a' : 'button';
const iconProps = {
onClick: this.checkDoubleClick,
onContextMenu: this.props.onContextMenu && this.handleContextMenu,
onTouchEnd: this.props.onDoubleClick || this.props.onClick,
className: cx('icon', props.className),
title: props.alt,
value: props.value,
ref: icon => {
this.icon = icon;
},
href: props.href,
};
const contents = (
<>
<div
className="icon__icon"
style={{ backgroundImage: `url('${props.icon}')` }}
/>
<div className="icon__text">{props.title}</div>
</>
);
if (this.props.onClick || this.props.onDoubleClick) {
return (
<Comp
ref={icon => {
this.icon = icon;
}}
target={props.external && Comp === 'a' && '_blank'}
rel={props.external && Comp === 'a' && 'noopener noreferrer'}
{...iconProps}
>
{contents}
</Comp>
);
}
return <div {...iconProps}>{contents}</div>;
}
}
export const iconProps = {
title: PropTypes.string,
icon: PropTypes.string,
children: PropTypes.node,
className: PropTypes.string,
alt: PropTypes.string,
value: PropTypes.any,
onClick: PropTypes.func,
onDoubleClick: PropTypes.func,
onContextMenu: PropTypes.func,
href: PropTypes.string,
external: PropTypes.bool,
};
AbstractIcon.propTypes = iconProps;
export default AbstractIcon;