forked from yuristrelets/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltip.js
More file actions
67 lines (58 loc) · 2.01 KB
/
Tooltip.js
File metadata and controls
67 lines (58 loc) · 2.01 KB
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
import React from 'react';
import ClassNames from 'classnames';
import style from './style';
const Tooltip = (ComposedComponent) => class extends React.Component {
static propTypes = {
children: React.PropTypes.any,
className: React.PropTypes.string,
onClick: React.PropTypes.func,
onMouseEnter: React.PropTypes.func,
onMouseLeave: React.PropTypes.func,
tooltip: React.PropTypes.string,
tooltipDelay: React.PropTypes.number,
tooltipHideOnClick: React.PropTypes.bool
};
static defaultProps = {
className: '',
tooltipDelay: 0,
tooltipHideOnClick: true
};
state = {
active: false
};
handleMouseEnter = (event) => {
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(() =>this.setState({active: true}), this.props.tooltipDelay);
if (this.props.onMouseEnter) this.props.onMouseEnter(event);
};
handleMouseLeave = (event) => {
if (this.timeout) clearTimeout(this.timeout);
if (this.state.active) this.setState({active: false});
if (this.props.onMouseLeave) this.props.onMouseLeave(event);
};
handleClick = (event) => {
if (this.timeout) clearTimeout(this.timeout);
if (this.props.tooltipHideOnClick) this.setState({active: false});
if (this.props.onClick) this.props.onClick(event);
};
render () {
const {children, className, tooltip, tooltipDelay, tooltipHideOnClick, ...other} = this.props; //eslint-disable-line no-unused-vars
const composedClassName = ClassNames(style.root, className);
const tooltipClassName = ClassNames(style.tooltip, {
[style.active]: this.state.active
});
return (
<ComposedComponent
{...other}
className={composedClassName}
onClick={this.handleClick}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
>
{children ? children : null}
<span data-react-toolbox="tooltip" className={tooltipClassName}>{tooltip}</span>
</ComposedComponent>
);
}
};
export default Tooltip;