File tree Expand file tree Collapse file tree 2 files changed +40
-24
lines changed Expand file tree Collapse file tree 2 files changed +40
-24
lines changed Original file line number Diff line number Diff line change 1
1
import * as React from "react" ;
2
2
import PropTypes from "prop-types" ;
3
+ import withHover from "./withHover" ;
3
4
4
5
const container = {
5
6
position : "relative" ,
6
7
display : "flex" ,
7
8
} ;
8
9
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 {
26
11
render ( ) {
27
- const { hovering } = this . state ;
28
- const { children, element } = this . props ;
12
+ const { children, element, hovering } = this . props ;
29
13
30
14
return (
31
- < div
32
- onMouseOver = { this . mouseOver }
33
- onMouseOut = { this . mouseOut }
34
- style = { container }
35
- >
15
+ < div style = { container } >
36
16
{ hovering === true && element }
37
17
{ children }
38
18
</ div >
@@ -44,3 +24,5 @@ Tooltip.propTypes = {
44
24
children : PropTypes . node . isRequired ,
45
25
element : PropTypes . node . isRequired ,
46
26
} ;
27
+
28
+ export default withHover ( Tooltip ) ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments