@@ -13,51 +13,28 @@ import cssStyle from './style'
13
13
14
14
@staticMethods @windowListener
15
15
class ReactTooltip extends Component {
16
- /**
17
- * Class method
18
- * @see ReactTooltip.hide() && ReactTooltup.rebuild()
19
- */
20
- // static hide () {
21
- // /**
22
- // * Check for ie
23
- // * @see http://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work
24
- // */
25
- // if (typeof window.Event === 'function') {
26
- // window.dispatchEvent(new window.Event('__react_tooltip_hide_event'))
27
- // } else {
28
- // let event = document.createEvent('Event')
29
- // event.initEvent('__react_tooltip_hide_event', false, true)
30
- // window.dispatchEvent(event)
31
- // }
32
- // }
33
-
34
- // static rebuild () {
35
- // if (typeof window.Event === 'function') {
36
- // window.dispatchEvent(new window.Event('__react_tooltip_rebuild_event'))
37
- // } else {
38
- // let event = document.createEvent('Event')
39
- // event.initEvent('__react_tooltip_rebuild_event', false, true)
40
- // window.dispatchEvent(event)
41
- // }
42
- // }
43
-
44
- globalHide ( ) {
45
- if ( this . mount ) {
46
- this . hideTooltip ( )
47
- }
48
- }
49
16
50
- globalRebuild ( ) {
51
- if ( this . mount ) {
52
- this . unbindListener ( )
53
- this . bindListener ( )
54
- }
17
+ static propTypes = {
18
+ children : PropTypes . any ,
19
+ place : PropTypes . string ,
20
+ type : PropTypes . string ,
21
+ effect : PropTypes . string ,
22
+ offset : PropTypes . object ,
23
+ multiline : PropTypes . bool ,
24
+ border : PropTypes . bool ,
25
+ class : PropTypes . string ,
26
+ id : PropTypes . string ,
27
+ html : PropTypes . bool ,
28
+ delayHide : PropTypes . number ,
29
+ delayShow : PropTypes . number ,
30
+ event : PropTypes . any ,
31
+ eventOff : PropTypes . any ,
32
+ watchWindow : PropTypes . bool ,
33
+ isCapture : PropTypes . bool
55
34
}
56
35
57
36
constructor ( props ) {
58
37
super ( props )
59
- this . _bind ( 'showTooltip' , 'updateTooltip' , 'hideTooltip' , 'checkStatus' , 'onWindowResize' , 'bindClickListener' , 'globalHide' , 'globalRebuild' )
60
- this . mount = true
61
38
this . state = {
62
39
show : false ,
63
40
border : false ,
@@ -78,28 +55,16 @@ class ReactTooltip extends Component {
78
55
eventOff : props . eventOff || null ,
79
56
isCapture : props . isCapture || false
80
57
}
58
+
59
+ this . mount = true
81
60
this . delayShowLoop = null
82
61
this . delayHideLoop = null
83
62
}
84
63
85
- /* Bind this with method */
86
- _bind ( ...handlers ) {
87
- handlers . forEach ( handler => this [ handler ] = this [ handler ] . bind ( this ) )
88
- }
89
-
90
64
componentDidMount ( ) {
91
65
this . bindListener ( )
92
66
this . setStyleHeader ( )
93
67
this . bindWindowEvents ( )
94
- // /* Add window event listener for hide and rebuild */
95
- // window.removeEventListener('__react_tooltip_hide_event', this.globalHide)
96
- // window.addEventListener('__react_tooltip_hide_event', this.globalHide, false)
97
-
98
- // window.removeEventListener('__react_tooltip_rebuild_event', this.globalRebuild)
99
- // window.addEventListener('__react_tooltip_rebuild_event', this.globalRebuild, false)
100
- // /* Add listener on window resize */
101
- // window.removeEventListener('resize', this.onWindowResize)
102
- // window.addEventListener('resize', this.onWindowResize, false)
103
68
}
104
69
105
70
componentWillUpdate ( ) {
@@ -118,61 +83,78 @@ class ReactTooltip extends Component {
118
83
this . removeScrollListener ( )
119
84
this . mount = false
120
85
this . unbindWindowEvents ( )
121
- // window.removeEventListener('__react_tooltip_hide_event', this.globalHide)
122
- // window.removeEventListener('__react_tooltip_rebuild_event', this.globalRebuild)
123
- // window.removeEventListener('resize', this.onWindowResize)
124
86
}
125
87
126
- /* TODO: optimize, bind has been trigger too many times */
88
+ /*
89
+ * Rebind the tooltip
90
+ * Invoked by ReactTooltip.rebuild()
91
+ */
92
+ globalRebuild ( ) {
93
+ if ( this . mount ) {
94
+ this . unbindListener ( )
95
+ this . bindListener ( )
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Bind listener to the target elements
101
+ * These listeners used to trigger showing or hiding the tooltip
102
+ */
127
103
bindListener ( ) {
128
104
let targetArray = this . getTargetArray ( )
129
105
130
106
let dataEvent
131
107
let dataEventOff
132
- for ( let i = 0 ; i < targetArray . length ; i ++ ) {
133
- if ( targetArray [ i ] . getAttribute ( 'currentItem' ) === null ) {
134
- targetArray [ i ] . setAttribute ( 'currentItem' , 'false' )
108
+ targetArray . forEach ( target => {
109
+ if ( target . getAttribute ( 'currentItem' ) === null ) {
110
+ target . setAttribute ( 'currentItem' , 'false' )
135
111
}
136
- dataEvent = this . state . event || targetArray [ i ] . getAttribute ( 'data-event' )
137
- if ( dataEvent ) {
138
- // if off event is specified, we will show tip on data-event and hide it on data-event-off
139
- dataEventOff = this . state . eventOff || targetArray [ i ] . getAttribute ( 'data-event-off' )
140
112
141
- targetArray [ i ] . removeEventListener ( dataEvent , this . checkStatus )
142
- targetArray [ i ] . addEventListener ( dataEvent , this . checkStatus , false )
113
+ dataEvent = this . state . event || target . getAttribute ( 'data-event' )
114
+ dataEventOff = this . state . eventOff || target . getAttribute ( 'data-event-off' )
115
+
116
+ if ( dataEvent ) {
117
+ target . removeEventListener ( dataEvent , this . checkStatus )
118
+ target . addEventListener ( dataEvent , ::this . checkStatus , false )
143
119
if ( dataEventOff ) {
144
- targetArray [ i ] . removeEventListener ( dataEventOff , this . hideTooltip )
145
- targetArray [ i ] . addEventListener ( dataEventOff , this . hideTooltip , false )
120
+ target . removeEventListener ( dataEventOff , this . hideTooltip )
121
+ target . addEventListener ( dataEventOff , :: this . hideTooltip , false )
146
122
}
147
- } else {
148
- targetArray [ i ] . removeEventListener ( 'mouseenter' , this . showTooltip )
149
- targetArray [ i ] . addEventListener ( 'mouseenter' , this . showTooltip , false )
123
+ return
124
+ }
150
125
151
- if ( this . state . effect === 'float' ) {
152
- targetArray [ i ] . removeEventListener ( 'mousemove' , this . updateTooltip )
153
- targetArray [ i ] . addEventListener ( 'mousemove' , this . updateTooltip , false )
154
- }
126
+ target . removeEventListener ( 'mouseenter' , this . showTooltip )
127
+ target . addEventListener ( 'mouseenter' , ::this . showTooltip , false )
155
128
156
- targetArray [ i ] . removeEventListener ( 'mouseleave' , this . hideTooltip )
157
- targetArray [ i ] . addEventListener ( 'mouseleave' , this . hideTooltip , false )
129
+ if ( this . state . effect === 'float' ) {
130
+ target . removeEventListener ( 'mousemove' , this . updateTooltip )
131
+ target . addEventListener ( 'mousemove' , ::this . updateTooltip , false )
158
132
}
159
- }
133
+
134
+ target . removeEventListener ( 'mouseleave' , this . hideTooltip )
135
+ target . addEventListener ( 'mouseleave' , ::this . hideTooltip , false )
136
+ } )
160
137
}
161
138
139
+ /**
140
+ * Unbind listeners on target elements
141
+ */
162
142
unbindListener ( ) {
163
143
let targetArray = document . querySelectorAll ( '[data-tip]' )
164
144
let dataEvent
165
145
166
- for ( let i = 0 ; i < targetArray . length ; i ++ ) {
167
- dataEvent = this . state . event || targetArray [ i ] . getAttribute ( 'data-event' )
146
+ targetArray . forEach ( target => {
147
+ dataEvent = this . state . event || target . getAttribute ( 'data-event' )
168
148
if ( dataEvent ) {
169
- targetArray [ i ] . removeEventListener ( dataEvent , this . checkStatus )
170
- } else {
171
- targetArray [ i ] . removeEventListener ( 'mouseenter' , this . showTooltip )
172
- targetArray [ i ] . removeEventListener ( 'mousemove' , this . updateTooltip )
173
- targetArray [ i ] . removeEventListener ( 'mouseleave' , this . hideTooltip )
149
+ target . removeEventListener ( dataEvent , this . checkStatus )
150
+ if ( dataEventOff ) target . removeEventListener ( dataEventOff , this . hideTooltip )
151
+ return
174
152
}
175
- }
153
+
154
+ target . removeEventListener ( 'mouseenter' , this . showTooltip )
155
+ target . removeEventListener ( 'mousemove' , this . updateTooltip )
156
+ target . removeEventListener ( 'mouseleave' , this . hideTooltip )
157
+ } )
176
158
}
177
159
178
160
/**
@@ -232,7 +214,7 @@ class ReactTooltip extends Component {
232
214
e . currentTarget . setAttribute ( 'currentItem' , 'true' )
233
215
/* when click other place, the tooltip should be removed */
234
216
window . removeEventListener ( 'click' , this . bindClickListener )
235
- window . addEventListener ( 'click' , this . bindClickListener , isCapture )
217
+ window . addEventListener ( 'click' , :: this . bindClickListener , isCapture )
236
218
237
219
this . showTooltip ( e )
238
220
this . setUntargetItems ( e . currentTarget )
@@ -251,7 +233,7 @@ class ReactTooltip extends Component {
251
233
}
252
234
253
235
bindClickListener ( ) {
254
- this . globalHide ( )
236
+ this . hideTooltip ( )
255
237
window . removeEventListener ( 'click' , this . bindClickListener )
256
238
}
257
239
@@ -331,6 +313,9 @@ class ReactTooltip extends Component {
331
313
*/
332
314
hideTooltip ( ) {
333
315
const { delayHide} = this . state
316
+
317
+ if ( ! this . mount ) return
318
+
334
319
clearTimeout ( this . delayShowLoop )
335
320
clearTimeout ( this . delayHideLoop )
336
321
this . delayHideLoop = setTimeout ( ( ) => {
@@ -346,7 +331,7 @@ class ReactTooltip extends Component {
346
331
* or tooltip will always existed
347
332
*/
348
333
addScrollListener ( ) {
349
- window . addEventListener ( 'scroll' , this . hideTooltip )
334
+ window . addEventListener ( 'scroll' , :: this . hideTooltip )
350
335
}
351
336
352
337
removeScrollListener ( ) {
@@ -668,24 +653,5 @@ class ReactTooltip extends Component {
668
653
669
654
}
670
655
671
- ReactTooltip . propTypes = {
672
- children : PropTypes . any ,
673
- place : PropTypes . string ,
674
- type : PropTypes . string ,
675
- effect : PropTypes . string ,
676
- offset : PropTypes . object ,
677
- multiline : PropTypes . bool ,
678
- border : PropTypes . bool ,
679
- class : PropTypes . string ,
680
- id : PropTypes . string ,
681
- html : PropTypes . bool ,
682
- delayHide : PropTypes . number ,
683
- delayShow : PropTypes . number ,
684
- event : PropTypes . any ,
685
- eventOff : PropTypes . any ,
686
- watchWindow : PropTypes . bool ,
687
- isCapture : PropTypes . bool
688
- }
689
-
690
656
/* export default not fit for standalone, it will exports {default:...} */
691
657
module . exports = ReactTooltip
0 commit comments