forked from ReactTooltip/react-tooltip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
286 lines (259 loc) · 7.99 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
import React, { PropTypes } from 'react';
import classname from 'classnames';
class ReactTooltip extends React.Component {
_bind(...handlers) {
handlers.forEach(handler => this[handler] = this[handler].bind(this));
}
constructor() {
super();
this._bind("showTooltip", "updateTooltip", "hideTooltip");
this.state = {
show: false,
multilineCount: 0,
placeholder: "",
x: "NONE",
y: "NONE",
place: "",
type: "",
effect: "",
multiline: false,
position: {}
};
}
componentDidMount() {
this._updatePosition();
this.bindListener();
}
componentWillUnmount() {
this.unbindListener();
}
componentWillUpdate() {
this.unbindListener();
}
componentDidUpdate(){
this._updatePosition();
this.bindListener();
}
bindListener() {
let targetArray = document.querySelectorAll("[data-tip]");
for(let i = 0; i < targetArray.length; i++) {
targetArray[i].addEventListener("mouseenter", this.showTooltip, false);
targetArray[i].addEventListener("mousemove", this.updateTooltip, false);
targetArray[i].addEventListener("mouseleave", this.hideTooltip, false);
}
}
unbindListener() {
let targetArray = document.querySelectorAll("[data-tip]");
for(let i = 0; i < targetArray.length; i++) {
targetArray[i].removeEventListener("mouseenter", this.showTooltip);
targetArray[i].removeEventListener("mousemove", this.updateTooltip);
targetArray[i].removeEventListener("mouseleave", this.hideTooltip);
}
}
_updatePosition(){
let node = React.findDOMNode(this);
let tipWidth = node.clientWidth;
let tipHeight = node.clientHeight;
let offset = {x:0, y:0};
let { effect } = this.state;
if(effect === "float") {
if(this.state.place === "top") {
offset.x = -(tipWidth/2);
offset.y = -50;
}
else if(this.state.place === "bottom") {
offset.x = -(tipWidth/2);
offset.y = 30;
}
else if(this.state.place === "left") {
offset.x = -(tipWidth + 15);
offset.y = -(tipHeight/2);
}
else if(this.state.place === "right") {
offset.x = 10;
offset.y = -(tipHeight/2);
}
}
let xPosition = 0, yPosition = 0, {position} = this.state;
if(Object.prototype.toString.apply(position) === "[object String]") {
position = JSON.parse(position.toString().replace(/\'/g,"\""));
}
for(let key in position) {
if(key === "top") {
yPosition -= parseInt(position[key]);
}
else if(key === "bottom") {
yPosition += parseInt(position[key]);
}
else if(key === "left") {
xPosition -= parseInt(position[key]);
}
else if(key === "right") {
xPosition += parseInt(position[key]);
}
}
node.style.left = this.state.x + offset.x + xPosition + 'px';
node.style.top = this.state.y + offset.y + yPosition + 'px';
}
showTooltip(e) {
const originTooltip = e.target.getAttribute("data-tip"),
regexp = /<br\s*\W*>|\W+/ ,
multiline = e.target.getAttribute("data-multiline") ?
e.target.getAttribute("data-multiline") :
this.props.multiline ?
this.props.multiline :
false
;
let tooltipText,
multilineCount = 0 ;
if(!multiline || multiline === "false" || !regexp.test(originTooltip)) {
tooltipText = originTooltip
}
else {
tooltipText = originTooltip.split(regexp).map((d, i) => {
multilineCount += 1;
return (
<span key={i} className="multi-line">{d}</span>
)
})
}
this.setState({
placeholder: tooltipText,
multilineCount: multilineCount,
place: e.target.getAttribute("data-place") ?
e.target.getAttribute("data-place") :
this.props.place ?
this.props.place :
"top"
,
type: e.target.getAttribute("data-type") ?
e.target.getAttribute("data-type") :
this.props.type ?
this.props.type :
"dark"
,
effect: e.target.getAttribute("data-effect") ?
e.target.getAttribute("data-effect") :
this.props.effect ?
this.props.effect :
"float"
,
position: e.target.getAttribute("data-position") ?
e.target.getAttribute("data-position") :
this.props.position ?
this.props.position :
{}
,
multiline: multiline
,
})
this.updateTooltip(e);
}
updateTooltip(e) {
if(this.trim(this.state.placeholder).length > 0) {
const {multilineCount, place} = this.state;
if(this.state.effect === "float") {
const offsetY = !multilineCount ?
e.clientY :
place !== "top" ?
e.clientY:
e.clientY - multilineCount * 14.5
this.setState({
show: true,
x: e.clientX,
y: offsetY
})
}
else if(this.state.effect === "solid"){
const boundingClientRect = e.target.getBoundingClientRect()
let targetTop = boundingClientRect.top;
let targetLeft = boundingClientRect.left;
let node = React.findDOMNode(this);
let tipWidth = node.clientWidth;
let tipHeight = node.clientHeight;
let targetWidth = e.target.clientWidth;
let targetHeight = e.target.clientHeight;
let x, y ;
if(place === "top") {
x = targetLeft - (tipWidth/2) + (targetWidth/2);
y = targetTop - tipHeight - 8;
}
else if(place === "bottom") {
x = targetLeft - (tipWidth/2) + (targetWidth/2);
y = targetTop + targetHeight + 8;
}
else if(place === "left") {
x = targetLeft - tipWidth - 6;
y = targetTop + (targetHeight/2) - (tipHeight/2);
}
else if(place === "right") {
x = targetLeft + targetWidth + 6;
y = targetTop + (targetHeight/2) - (tipHeight/2);
}
this.setState({
show: true,
x: this.state.x === "NONE" ? x : this.state.x,
y: this.state.y === "NONE" ? y : this.state.y
})
}
}
}
hideTooltip(e) {
this.setState({
show: false,
x: "NONE",
y: "NONE",
});
}
render() {
let tooltipClass = classname(
'reactTooltip',
{"show": this.state.show},
{"place-top": this.state.place === "top"},
{"place-bottom": this.state.place === "bottom"},
{"place-left": this.state.place === "left"},
{"place-right": this.state.place === "right"},
{"type-dark": this.state.type === "dark"},
{"type-success": this.state.type === "success"},
{"type-warning": this.state.type === "warning"},
{"type-error": this.state.type === "error"},
{"type-info": this.state.type === "info"},
{"type-light": this.state.type === "light"}
);
return (
<span className={tooltipClass} data-id="tooltip">{this.state.placeholder}</span>
)
}
trim(string) {
if(Object.prototype.toString.call(string) !== "[object String]") {
return string
}
let newString = string.split("");
let firstCount = 0, lastCount = 0;
for(let i = 0; i < string.length; i++) {
if(string[i] !== " ") {
break;
}
firstCount++;
}
for(let i = string.length-1; i >= 0; i--) {
if(string[i] !== " ") {
break;
}
lastCount++;
}
newString.splice(0, firstCount);
newString.splice(-lastCount, lastCount);
return newString.join("");
}
}
ReactTooltip.displayName = 'ReactTooltip';
ReactTooltip.propTypes = {
place: PropTypes.string,
type: PropTypes.string,
effect: PropTypes.string,
position: PropTypes.object,
multiline: PropTypes.bool
};
export default ReactTooltip;