Skip to content

Commit eed3d6d

Browse files
Get the tooltip content in the render method using a function instead of setting it in state.placeholder. This allows for the tooltip contents to change dynamically (or be hidden if you return null in the getContent prop).
1 parent 2762495 commit eed3d6d

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

src/index.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class ReactTooltip extends Component {
5959
effect: 'float', // float or fixed
6060
show: false,
6161
border: false,
62-
placeholder: '',
6362
offset: {},
6463
extraClass: '',
6564
html: false,
@@ -71,13 +70,16 @@ class ReactTooltip extends Component {
7170
currentTarget: null, // Current target of mouse event
7271
ariaProps: parseAria(props), // aria- and role attributes
7372
isEmptyTip: false,
74-
disable: false
73+
disable: false,
74+
originTooltip: null,
75+
isMultiline: false
7576
}
7677

7778
this.bind([
7879
'showTooltip',
7980
'updateTooltip',
8081
'hideTooltip',
82+
'getTooltipContent',
8183
'globalRebuild',
8284
'globalShow',
8385
'globalHide',
@@ -205,6 +207,26 @@ class ReactTooltip extends Component {
205207
target.removeEventListener('mouseleave', this.hideTooltip)
206208
}
207209

210+
getTooltipContent () {
211+
const {getContent, children} = this.props
212+
213+
// Generate tooltip content
214+
let content
215+
if (getContent) {
216+
if (Array.isArray(getContent)) {
217+
content = getContent[0] && getContent[0]()
218+
} else {
219+
content = getContent()
220+
}
221+
}
222+
223+
return getTipContent(this.state.originTooltip, children, content, this.state.isMultiline)
224+
}
225+
226+
isEmptyTip (placeholder) {
227+
return typeof placeholder === 'string' && placeholder === '' || placeholder === null
228+
}
229+
208230
/**
209231
* When mouse enter, show the tooltip
210232
*/
@@ -217,22 +239,10 @@ class ReactTooltip extends Component {
217239
}
218240
// Get the tooltip content
219241
// calculate in this phrase so that tip width height can be detected
220-
const {children, multiline, getContent} = this.props
242+
const {multiline} = this.props
221243
const originTooltip = e.currentTarget.getAttribute('data-tip')
222244
const isMultiline = e.currentTarget.getAttribute('data-multiline') || multiline || false
223245

224-
// Generate tootlip content
225-
let content
226-
if (getContent) {
227-
if (Array.isArray(getContent)) {
228-
content = getContent[0] && getContent[0]()
229-
} else {
230-
content = getContent()
231-
}
232-
}
233-
const placeholder = getTipContent(originTooltip, children, content, isMultiline)
234-
const isEmptyTip = typeof placeholder === 'string' && placeholder === '' || placeholder === null
235-
236246
// If it is focus event or called by ReactTooltip.show, switch to `solid` effect
237247
const switchToSolid = e instanceof window.FocusEvent || isGlobalCall
238248

@@ -245,8 +255,8 @@ class ReactTooltip extends Component {
245255
}
246256

247257
this.setState({
248-
placeholder,
249-
isEmptyTip,
258+
originTooltip: originTooltip,
259+
isMultiline: isMultiline,
250260
place: e.currentTarget.getAttribute('data-place') || this.props.place || 'top',
251261
type: e.currentTarget.getAttribute('data-type') || this.props.type || 'dark',
252262
effect: switchToSolid && 'solid' || e.currentTarget.getAttribute('data-effect') || this.props.effect || 'float',
@@ -266,34 +276,20 @@ class ReactTooltip extends Component {
266276
}, () => {
267277
if (scrollHide) this.addScrollListener(e)
268278
this.updateTooltip(e)
269-
270-
if (getContent && Array.isArray(getContent)) {
271-
this.intervalUpdateContent = setInterval(() => {
272-
if (this.mount) {
273-
const {getContent} = this.props
274-
const placeholder = getTipContent(originTooltip, getContent[0](), isMultiline)
275-
const isEmptyTip = typeof placeholder === 'string' && placeholder === ''
276-
this.setState({
277-
placeholder,
278-
isEmptyTip
279-
})
280-
}
281-
}, getContent[1])
282-
}
283279
})
284280
}
285281

286282
/**
287283
* When mouse hover, updatetooltip
288284
*/
289285
updateTooltip (e) {
290-
const {delayShow, show, isEmptyTip, disable} = this.state
286+
const {delayShow, show, disable} = this.state
291287
const {afterShow} = this.props
292-
let {placeholder} = this.state
288+
const placeholder = this.getTooltipContent()
293289
const delayTime = show ? 0 : parseInt(delayShow, 10)
294290
const eventTarget = e.currentTarget
295291

296-
if (isEmptyTip || disable) return // if the tooltip is empty, disable the tooltip
292+
if (this.isEmptyTip(placeholder) || disable) return // if the tooltip is empty, disable the tooltip
297293
const updateState = () => {
298294
if (Array.isArray(placeholder) && placeholder.length > 0 || placeholder) {
299295
const isInvisible = !this.state.show
@@ -320,10 +316,11 @@ class ReactTooltip extends Component {
320316
* When mouse leave, hide tooltip
321317
*/
322318
hideTooltip (e, hasTarget) {
323-
const {delayHide, isEmptyTip, disable} = this.state
319+
const {delayHide, disable} = this.state
324320
const {afterHide} = this.props
321+
const placeholder = this.getTooltipContent()
325322
if (!this.mount) return
326-
if (isEmptyTip || disable) return // if the tooltip is empty, disable the tooltip
323+
if (this.isEmptyTip(placeholder) || disable) return // if the tooltip is empty, disable the tooltip
327324
if (hasTarget) {
328325
// Don't trigger other elements belongs to other ReactTooltip
329326
const targetArray = this.getTargetArray(this.props.id)
@@ -401,7 +398,9 @@ class ReactTooltip extends Component {
401398
}
402399

403400
render () {
404-
const {placeholder, extraClass, html, ariaProps, disable, isEmptyTip} = this.state
401+
const {extraClass, html, ariaProps, disable} = this.state
402+
const placeholder = this.getTooltipContent()
403+
const isEmptyTip = this.isEmptyTip(placeholder)
405404
let tooltipClass = classname(
406405
'__react_component_tooltip',
407406
{'show': this.state.show && !disable && !isEmptyTip},

0 commit comments

Comments
 (0)