Skip to content

Commit 6d06849

Browse files
authored
Merge pull request #281 from alfonsomunozpomer/dynamic_tooltip_content
Get tooltip content dynamically in the render method
2 parents 3fac729 + b93cfda commit 6d06849

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

src/index.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class ReactTooltip extends React.Component {
7575
effect: 'float', // float or fixed
7676
show: false,
7777
border: false,
78-
placeholder: '',
7978
offset: {},
8079
extraClass: '',
8180
html: false,
@@ -87,13 +86,16 @@ class ReactTooltip extends React.Component {
8786
currentTarget: null, // Current target of mouse event
8887
ariaProps: parseAria(props), // aria- and role attributes
8988
isEmptyTip: false,
90-
disable: false
89+
disable: false,
90+
originTooltip: null,
91+
isMultiline: false
9192
}
9293

9394
this.bind([
9495
'showTooltip',
9596
'updateTooltip',
9697
'hideTooltip',
98+
'getTooltipContent',
9799
'globalRebuild',
98100
'globalShow',
99101
'globalHide',
@@ -226,6 +228,26 @@ class ReactTooltip extends React.Component {
226228
target.removeEventListener('mouseleave', this.hideTooltip, isCaptureMode)
227229
}
228230

231+
getTooltipContent () {
232+
const {getContent, children} = this.props
233+
234+
// Generate tooltip content
235+
let content
236+
if (getContent) {
237+
if (Array.isArray(getContent)) {
238+
content = getContent[0] && getContent[0]()
239+
} else {
240+
content = getContent()
241+
}
242+
}
243+
244+
return getTipContent(this.state.originTooltip, children, content, this.state.isMultiline)
245+
}
246+
247+
isEmptyTip (placeholder) {
248+
return typeof placeholder === 'string' && placeholder === '' || placeholder === null
249+
}
250+
229251
/**
230252
* When mouse enter, show the tooltip
231253
*/
@@ -238,22 +260,10 @@ class ReactTooltip extends React.Component {
238260
}
239261
// Get the tooltip content
240262
// calculate in this phrase so that tip width height can be detected
241-
const {children, multiline, getContent} = this.props
263+
const {multiline, getContent} = this.props
242264
const originTooltip = e.currentTarget.getAttribute('data-tip')
243265
const isMultiline = e.currentTarget.getAttribute('data-multiline') || multiline || false
244266

245-
// Generate tootlip content
246-
let content
247-
if (getContent) {
248-
if (Array.isArray(getContent)) {
249-
content = getContent[0] && getContent[0]()
250-
} else {
251-
content = getContent()
252-
}
253-
}
254-
const placeholder = getTipContent(originTooltip, children, content, isMultiline)
255-
const isEmptyTip = typeof placeholder === 'string' && placeholder === '' || placeholder === null
256-
257267
// If it is focus event or called by ReactTooltip.show, switch to `solid` effect
258268
const switchToSolid = e instanceof window.FocusEvent || isGlobalCall
259269

@@ -269,8 +279,8 @@ class ReactTooltip extends React.Component {
269279
this.clearTimer()
270280

271281
this.setState({
272-
placeholder,
273-
isEmptyTip,
282+
originTooltip: originTooltip,
283+
isMultiline: isMultiline,
274284
desiredPlace: e.currentTarget.getAttribute('data-place') || this.props.place || 'top',
275285
place: e.currentTarget.getAttribute('data-place') || this.props.place || 'top',
276286
type: e.currentTarget.getAttribute('data-type') || this.props.type || 'dark',
@@ -297,9 +307,8 @@ class ReactTooltip extends React.Component {
297307
if (this.mount) {
298308
const {getContent} = this.props
299309
const placeholder = getTipContent(originTooltip, '', getContent[0](), isMultiline)
300-
const isEmptyTip = typeof placeholder === 'string' && placeholder === ''
310+
const isEmptyTip = this.isEmptyTip(placeholder)
301311
this.setState({
302-
placeholder,
303312
isEmptyTip
304313
})
305314
}
@@ -312,13 +321,13 @@ class ReactTooltip extends React.Component {
312321
* When mouse hover, updatetooltip
313322
*/
314323
updateTooltip (e) {
315-
const {delayShow, show, isEmptyTip, disable} = this.state
324+
const {delayShow, show, disable} = this.state
316325
const {afterShow} = this.props
317-
let {placeholder} = this.state
326+
const placeholder = this.getTooltipContent()
318327
const delayTime = show ? 0 : parseInt(delayShow, 10)
319328
const eventTarget = e.currentTarget
320329

321-
if (isEmptyTip || disable) return // if the tooltip is empty, disable the tooltip
330+
if (this.isEmptyTip(placeholder) || disable) return // if the tooltip is empty, disable the tooltip
322331
const updateState = () => {
323332
if (Array.isArray(placeholder) && placeholder.length > 0 || placeholder) {
324333
const isInvisible = !this.state.show
@@ -345,10 +354,11 @@ class ReactTooltip extends React.Component {
345354
* When mouse leave, hide tooltip
346355
*/
347356
hideTooltip (e, hasTarget) {
348-
const {delayHide, isEmptyTip, disable} = this.state
357+
const {delayHide, disable} = this.state
349358
const {afterHide} = this.props
359+
const placeholder = this.getTooltipContent()
350360
if (!this.mount) return
351-
if (isEmptyTip || disable) return // if the tooltip is empty, disable the tooltip
361+
if (this.isEmptyTip(placeholder) || disable) return // if the tooltip is empty, disable the tooltip
352362
if (hasTarget) {
353363
// Don't trigger other elements belongs to other ReactTooltip
354364
const targetArray = this.getTargetArray(this.props.id)
@@ -427,7 +437,9 @@ class ReactTooltip extends React.Component {
427437
}
428438

429439
render () {
430-
const {placeholder, extraClass, html, ariaProps, disable, isEmptyTip} = this.state
440+
const {extraClass, html, ariaProps, disable} = this.state
441+
const placeholder = this.getTooltipContent()
442+
const isEmptyTip = this.isEmptyTip(placeholder)
431443
let tooltipClass = classname(
432444
'__react_component_tooltip',
433445
{'show': this.state.show && !disable && !isEmptyTip},

0 commit comments

Comments
 (0)