Skip to content

Commit 45cc694

Browse files
authored
Merge pull request #193 from wwayne/hide-specific
hide specific tooltip
2 parents b4d72ec + 8be638e commit 45cc694

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,26 @@ Check the example [React-tooltip Test](http://wwayne.com/react-tooltip)
7777
3. When using react component as tooltip, you can have many `<ReactTooltip />` in a page but they should have different **id**
7878

7979
## Static Methods
80-
**ReactTooltip.hide()**: Hide the tooltip manually
80+
###ReactTooltip.hide(target)
8181

82-
**ReactTooltip.rebuild()**: Rebinding tooltip to the corresponding elements
82+
> Hide the tooltip manually, the target is optional, if no target passed in, all exitent tooltip will be hiden
8383
84-
**ReactTooltip.show(target)**: Show specific tooltip manually, for example:
84+
```js
85+
import {findDOMNode} from 'react-dom'
86+
import ReactTooltip from 'react-tooltip'
87+
88+
<p ref='foo' data-tip='tooltip'></p>
89+
<button onClick={() => { ReactTooltip.hide(findDOMNode(this.refs.foo)) }}></button>
90+
<ReactTooltip />
91+
```
92+
93+
###ReactTooltip.rebuild()
94+
95+
> Rebinding all tooltips
96+
97+
###ReactTooltip.show(target)
98+
99+
> Show specific tooltip manually, for example:
85100
86101
```js
87102
import {findDOMNode} from 'react-dom'

example/src/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
import React from 'react'
4-
import {render} from 'react-dom'
4+
import {render, findDOMNode} from 'react-dom'
55
import ReactTooltip from '../../src'
66

77
const Test = React.createClass({
@@ -153,8 +153,14 @@ const Test = React.createClass({
153153
<ReactTooltip id='custom-event' globalEventOff='click' />
154154
</div>
155155
<div className="side">
156-
<a data-for='custom-off-event' data-tip='custom show and hide' data-event='click' data-event-off='dblclick'>( •̀д•́)</a>
156+
<a data-for='custom-off-event' ref='target' data-tip='custom show and hide' data-event='click' data-event-off='dblclick'>( •̀д•́)</a>
157157
<ReactTooltip id='custom-off-event'/>
158+
{/*
159+
<div>
160+
<button onClick={() => { ReactTooltip.show(findDOMNode(this.refs.target)) }}>Show toolip</button>
161+
<button onClick={() => { ReactTooltip.hide(findDOMNode(this.refs.target)) }}>Hide toolip</button>
162+
</div>
163+
*/}
158164
</div>
159165
</div>
160166
<br />

src/decorators/staticMethods.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export default function (target) {
2323
* Hide all tooltip
2424
* @trigger ReactTooltip.hide()
2525
*/
26-
target.hide = () => {
27-
dispatchGlobalEvent(CONSTANT.GLOBAL.HIDE)
26+
target.hide = (target) => {
27+
dispatchGlobalEvent(CONSTANT.GLOBAL.HIDE, {target})
2828
}
2929

3030
/**
@@ -58,4 +58,11 @@ export default function (target) {
5858
this.showTooltip(e, true)
5959
}
6060
}
61+
62+
target.prototype.globalHide = function (event) {
63+
if (this.mount) {
64+
const hasTarget = event && event.detail && event.detail.target && true || false
65+
this.hideTooltip({ currentTarget: hasTarget && event.detail.target }, hasTarget)
66+
}
67+
}
6168
}

src/decorators/windowListener.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import CONSTANT from '../constant'
66
export default function (target) {
77
target.prototype.bindWindowEvents = function () {
88
// ReactTooltip.hide
9-
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.hideTooltip)
10-
window.addEventListener(CONSTANT.GLOBAL.HIDE, this.hideTooltip, false)
9+
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.globalHide)
10+
window.addEventListener(CONSTANT.GLOBAL.HIDE, this.globalHide, false)
1111

1212
// ReactTooltip.rebuild
1313
window.removeEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild)
@@ -23,7 +23,7 @@ export default function (target) {
2323
}
2424

2525
target.prototype.unbindWindowEvents = function () {
26-
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.hideTooltip)
26+
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.globalHide)
2727
window.removeEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild)
2828
window.removeEventListener(CONSTANT.GLOBAL.SHOW, this.globalShow)
2929
window.removeEventListener('resize', this.onWindowResize)

src/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ReactTooltip extends Component {
7474
'hideTooltip',
7575
'globalRebuild',
7676
'globalShow',
77+
'globalHide',
7778
'onWindowResize'
7879
])
7980

@@ -313,12 +314,16 @@ class ReactTooltip extends Component {
313314
/**
314315
* When mouse leave, hide tooltip
315316
*/
316-
hideTooltip () {
317+
hideTooltip (e, hasTarget) {
318+
if (!this.mount) return
319+
if (hasTarget) {
320+
// Don't trigger other elements belongs to other ReactTooltip
321+
const targetArray = this.getTargetArray(this.props.id)
322+
const isMyElement = targetArray.some(ele => ele === e.currentTarget)
323+
if (!isMyElement || !this.state.show) return
324+
}
317325
const {delayHide} = this.state
318326
const {afterHide} = this.props
319-
320-
if (!this.mount) return
321-
322327
const resetState = () => {
323328
const isVisible = this.state.show
324329
this.setState({

0 commit comments

Comments
 (0)