Skip to content

Commit a07f721

Browse files
committed
15-with-hover
1 parent 3f0d76d commit a07f721

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

app/components/Tooltip.jsx

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
import * as React from "react";
22
import PropTypes from "prop-types";
3+
import withHover from "./withHover";
34

45
const container = {
56
position: "relative",
67
display: "flex",
78
};
89

9-
export default class Tooltip extends React.Component {
10-
constructor(props) {
11-
super(props);
12-
13-
this.state = {
14-
hovering: false,
15-
};
16-
17-
this.mouseOver = this.mouseOver.bind(this);
18-
this.mouseOut = this.mouseOut.bind(this);
19-
}
20-
mouseOver() {
21-
this.setState({ hovering: true });
22-
}
23-
mouseOut() {
24-
this.setState({ hovering: false });
25-
}
10+
class Tooltip extends React.Component {
2611
render() {
27-
const { hovering } = this.state;
28-
const { children, element } = this.props;
12+
const { children, element, hovering } = this.props;
2913

3014
return (
31-
<div
32-
onMouseOver={this.mouseOver}
33-
onMouseOut={this.mouseOut}
34-
style={container}
35-
>
15+
<div style={container}>
3616
{hovering === true && element}
3717
{children}
3818
</div>
@@ -44,3 +24,5 @@ Tooltip.propTypes = {
4424
children: PropTypes.node.isRequired,
4525
element: PropTypes.node.isRequired,
4626
};
27+
28+
export default withHover(Tooltip);

app/components/withHover.jsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as React from "react";
2+
3+
export default function withHover(Component, propName = "hovering") {
4+
return class WithHover extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
8+
this.state = {
9+
hovering: false,
10+
};
11+
12+
this.mouseOver = this.mouseOver.bind(this);
13+
this.mouseOut = this.mouseOut.bind(this);
14+
}
15+
mouseOver() {
16+
this.setState({ hovering: true });
17+
}
18+
mouseOut() {
19+
this.setState({ hovering: false });
20+
}
21+
render() {
22+
const props = {
23+
[propName]: this.state.hovering,
24+
...this.props,
25+
};
26+
27+
return (
28+
<div onMouseOver={this.mouseOver} onMouseOut={this.mouseOut}>
29+
<Component {...props} />
30+
</div>
31+
);
32+
}
33+
};
34+
}

0 commit comments

Comments
 (0)