Skip to content

Commit 19b9d85

Browse files
authored
Merge pull request #201 from wwayne/prevent-empty-tip
Hide tooltip if the tip is empty or disabled
2 parents f2a5d04 + 7710d59 commit 19b9d85

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

example/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const Test = React.createClass({
181181
<div className="example-jsx">
182182
<div className="side">
183183
<a data-for='custom-class' data-tip='hover on me will keep the tootlip'>(・ω´・ )</a>
184+
{/* <a data-for='custom-class' data-tip='' data-tip-disable='true'>empty testing</a> */}
184185
<ReactTooltip id='custom-class' class='extraClass' delayHide={1000} effect='solid'/>
185186
</div>
186187
<div className="side">

src/index.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class ReactTooltip extends Component {
6969
eventOff: props.eventOff || null,
7070
currentEvent: null, // Current mouse event
7171
currentTarget: null, // Current target of mouse event
72-
ariaProps: parseAria(props) // aria- and role attributes
72+
ariaProps: parseAria(props), // aria- and role attributes
73+
isEmptyTip: false,
74+
disable: false
7375
}
7476

7577
this.bind([
@@ -207,11 +209,6 @@ class ReactTooltip extends Component {
207209
* When mouse enter, show the tooltip
208210
*/
209211
showTooltip (e, isGlobalCall) {
210-
const disabled = e.currentTarget.getAttribute('data-tip-disable')
211-
? e.currentTarget.getAttribute('data-tip-disable') === 'true'
212-
: (this.props.disable || false)
213-
if (disabled) return
214-
215212
if (isGlobalCall) {
216213
// Don't trigger other elements belongs to other ReactTooltip
217214
const targetArray = this.getTargetArray(this.props.id)
@@ -234,6 +231,7 @@ class ReactTooltip extends Component {
234231
}
235232
}
236233
const placeholder = getTipContent(originTooltip, content, isMultiline)
234+
const isEmptyTip = typeof placeholder === 'string' && placeholder === ''
237235

238236
// If it is focus event or called by ReactTooltip.show, switch to `solid` effect
239237
const switchToSolid = e instanceof window.FocusEvent || isGlobalCall
@@ -248,6 +246,7 @@ class ReactTooltip extends Component {
248246

249247
this.setState({
250248
placeholder,
249+
isEmptyTip,
251250
place: e.currentTarget.getAttribute('data-place') || this.props.place || 'top',
252251
type: e.currentTarget.getAttribute('data-type') || this.props.type || 'dark',
253252
effect: switchToSolid && 'solid' || e.currentTarget.getAttribute('data-effect') || this.props.effect || 'float',
@@ -260,7 +259,10 @@ class ReactTooltip extends Component {
260259
border: e.currentTarget.getAttribute('data-border')
261260
? e.currentTarget.getAttribute('data-border') === 'true'
262261
: (this.props.border || false),
263-
extraClass: e.currentTarget.getAttribute('data-class') || this.props.class || ''
262+
extraClass: e.currentTarget.getAttribute('data-class') || this.props.class || '',
263+
disable: e.currentTarget.getAttribute('data-tip-disable')
264+
? e.currentTarget.getAttribute('data-tip-disable') === 'true'
265+
: (this.props.disable || false)
264266
}, () => {
265267
if (scrollHide) this.addScrollListener(e)
266268
this.updateTooltip(e)
@@ -270,8 +272,10 @@ class ReactTooltip extends Component {
270272
if (this.mount) {
271273
const {getContent} = this.props
272274
const placeholder = getTipContent(originTooltip, getContent[0](), isMultiline)
275+
const isEmptyTip = typeof placeholder === 'string' && placeholder === ''
273276
this.setState({
274-
placeholder
277+
placeholder,
278+
isEmptyTip
275279
})
276280
}
277281
}, getContent[1])
@@ -283,14 +287,14 @@ class ReactTooltip extends Component {
283287
* When mouse hover, updatetooltip
284288
*/
285289
updateTooltip (e) {
286-
const {delayShow, show} = this.state
290+
const {delayShow, show, isEmptyTip, disable} = this.state
287291
const {afterShow} = this.props
288292
let {placeholder} = this.state
289293
const delayTime = show ? 0 : parseInt(delayShow, 10)
290294
const eventTarget = e.currentTarget
291295

296+
if (isEmptyTip || disable) return // if the tooltip is empty, disable the tooltip
292297
const updateState = () => {
293-
if (typeof placeholder === 'string') placeholder = placeholder.trim()
294298
if (Array.isArray(placeholder) && placeholder.length > 0 || placeholder) {
295299
const isInvisible = !this.state.show
296300
this.setState({
@@ -316,15 +320,16 @@ class ReactTooltip extends Component {
316320
* When mouse leave, hide tooltip
317321
*/
318322
hideTooltip (e, hasTarget) {
323+
const {delayHide, isEmptyTip, disable} = this.state
324+
const {afterHide} = this.props
319325
if (!this.mount) return
326+
if (isEmptyTip || disable) return // if the tooltip is empty, disable the tooltip
320327
if (hasTarget) {
321328
// Don't trigger other elements belongs to other ReactTooltip
322329
const targetArray = this.getTargetArray(this.props.id)
323330
const isMyElement = targetArray.some(ele => ele === e.currentTarget)
324331
if (!isMyElement || !this.state.show) return
325332
}
326-
const {delayHide} = this.state
327-
const {afterHide} = this.props
328333
const resetState = () => {
329334
const isVisible = this.state.show
330335
this.setState({
@@ -360,7 +365,6 @@ class ReactTooltip extends Component {
360365
updatePosition () {
361366
const {currentEvent, currentTarget, place, effect, offset} = this.state
362367
const node = ReactDOM.findDOMNode(this)
363-
364368
const result = getPosition(currentEvent, currentTarget, node, place, effect, offset)
365369

366370
if (result.isNewState) {
@@ -397,10 +401,10 @@ class ReactTooltip extends Component {
397401
}
398402

399403
render () {
400-
const {placeholder, extraClass, html, ariaProps} = this.state
404+
const {placeholder, extraClass, html, ariaProps, disable, isEmptyTip} = this.state
401405
let tooltipClass = classname(
402406
'__react_component_tooltip',
403-
{'show': this.state.show},
407+
{'show': this.state.show && !disable && !isEmptyTip},
404408
{'border': this.state.border},
405409
{'place-top': this.state.place === 'top'},
406410
{'place-bottom': this.state.place === 'bottom'},
@@ -413,7 +417,6 @@ class ReactTooltip extends Component {
413417
{'type-info': this.state.type === 'info'},
414418
{'type-light': this.state.type === 'light'}
415419
)
416-
417420
if (html) {
418421
return (
419422
<div className={`${tooltipClass} ${extraClass}`}

src/utils/getTipContent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function (tip, children, multiline) {
1818

1919
const regexp = /<br\s*\/?>/
2020
if (!multiline || multiline === 'false' || !regexp.test(tip)) {
21+
// No trim(), so that user can keep their input
2122
return tip
2223
}
2324

0 commit comments

Comments
 (0)