Skip to content

Commit b281953

Browse files
authored
Merge pull request #176 from wwayne/show
Support ReactTooltip.show() #47
2 parents 0d5a791 + da4e965 commit b281953

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,32 @@ Check the example [React-tooltip Test](http://wwayne.com/react-tooltip)
7575
3. When using react component as tooltip, you can have many `<ReactTooltip />` in a page but they should have different **id**
7676

7777
## Static Methods
78-
`ReactTooltip.hide()`: Hide the tooltip manually
78+
**ReactTooltip.hide()**: Hide the tooltip manually
7979

80-
`ReactTooltip.rebuild()`: Rebinding tooltip to the corresponding elements
80+
**ReactTooltip.rebuild()**: Rebinding tooltip to the corresponding elements
8181

82-
I suggest always put `<ReactTooltip />` in the Highest level or smart component of Redux, so you might need these static
83-
method to control tooltip's behaviour in some situations
82+
**ReactTooltip.show(target)**: Show specific tooltip manually, for example:
83+
84+
```
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.show(findDOMNode(this.refs.foo)) }}></button>
90+
<ReactTooltip />
91+
```
8492

8593
## Trouble Shooting
86-
#### Using tooltip within the modal (e.g. [react-modal](https://github.com/reactjs/react-modal))
94+
### Using tooltip within the modal (e.g. [react-modal](https://github.com/reactjs/react-modal))
8795
The component was designed to set a `<Reactooltip />` one place then use tooltip everywhere, but a lot of people stuck in using this component with modal, you can check the discussion [here](https://github.com/wwayne/react-tooltip/issues/130), the summarization of solving the problem is as following:
8896

8997
1. Put `<ReactTooltip />` out of the `<Modal>`
9098
2. Use `React.rebuild()` when opening the modal
9199
3. If your modal's z-index happens to higher than the tooltip, use the attribute `class` to custom your tooltip's z-index
92100

101+
>I suggest always put `<ReactTooltip />` in the Highest level or smart component of Redux, so you might need these static
102+
method to control tooltip's behaviour in some situations
103+
93104
## Article
94105
[How I insert sass into react component](https://medium.com/@wwayne_me/how-i-insert-sass-into-my-npm-react-component-b46b9811c226#.gi4hxu44a)
95106

src/decorators/staticMethods.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
*/
44
import CONSTANT from '../constant'
55

6-
const dispatchGlobalEvent = (eventName) => {
6+
const dispatchGlobalEvent = (eventName, opts) => {
77
// Compatibale with IE
88
// @see http://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work
99
let event
1010

11-
if (typeof window.Event === 'function') {
12-
event = new window.Event(eventName)
11+
if (typeof window.CustomEvent === 'function') {
12+
event = new window.CustomEvent(eventName, { detail: opts })
1313
} else {
1414
event = document.createEvent('Event')
15-
event.initEvent(eventName, false, true)
15+
event.initEvent(eventName, false, true, opts)
1616
}
1717

1818
window.dispatchEvent(event)
@@ -35,10 +35,27 @@ export default function (target) {
3535
dispatchGlobalEvent(CONSTANT.GLOBAL.REBUILD)
3636
}
3737

38+
/**
39+
* Show specific tooltip
40+
* @trigger ReactTooltip.show()
41+
*/
42+
target.show = (target) => {
43+
dispatchGlobalEvent(CONSTANT.GLOBAL.SHOW, {target})
44+
}
45+
3846
target.prototype.globalRebuild = function () {
3947
if (this.mount) {
4048
this.unbindListener()
4149
this.bindListener()
4250
}
4351
}
52+
53+
target.prototype.globalShow = function (event) {
54+
if (this.mount) {
55+
// Create a fake event, specific show will limit the type to `solid`
56+
// only `float` type cares e.clientX e.clientY
57+
const e = { currentTarget: event.detail.target }
58+
this.showTooltip(e, true)
59+
}
60+
}
4461
}

src/decorators/windowListener.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default function (target) {
1313
window.removeEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild)
1414
window.addEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild, false)
1515

16+
// ReactTooltip.show
17+
window.removeEventListener(CONSTANT.GLOBAL.SHOW, this.globalShow)
18+
window.addEventListener(CONSTANT.GLOBAL.SHOW, this.globalShow, false)
19+
1620
// Resize
1721
window.removeEventListener('resize', this.onWindowResize)
1822
window.addEventListener('resize', this.onWindowResize, false)
@@ -21,6 +25,7 @@ export default function (target) {
2125
target.prototype.unbindWindowEvents = function () {
2226
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.hideTooltip)
2327
window.removeEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild)
28+
window.removeEventListener(CONSTANT.GLOBAL.SHOW, this.globalShow)
2429
window.removeEventListener('resize', this.onWindowResize)
2530
}
2631

src/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class ReactTooltip extends Component {
7171
'updateTooltip',
7272
'hideTooltip',
7373
'globalRebuild',
74+
'globalShow',
7475
'onWindowResize'
7576
])
7677

@@ -198,7 +199,13 @@ class ReactTooltip extends Component {
198199
/**
199200
* When mouse enter, show the tooltip
200201
*/
201-
showTooltip (e) {
202+
showTooltip (e, isGlobalCall) {
203+
if (isGlobalCall) {
204+
// Don't trigger other elements belongs to other ReactTooltip
205+
const targetArray = this.getTargetArray(this.props.id)
206+
const isMyElement = targetArray.some(ele => ele === e.currentTarget)
207+
if (!isMyElement || this.state.show) return
208+
}
202209
// Get the tooltip content
203210
// calculate in this phrase so that tip width height can be detected
204211
const {children, multiline, getContent} = this.props
@@ -216,13 +223,13 @@ class ReactTooltip extends Component {
216223
}
217224
const placeholder = getTipContent(originTooltip, content, isMultiline)
218225

219-
// If it is focus event, switch to `solid` effect
220-
const isFocus = e instanceof window.FocusEvent
226+
// If it is focus event or called by ReactTooltip.show, switch to `solid` effect
227+
const switchToSolid = e instanceof window.FocusEvent || isGlobalCall
221228
this.setState({
222229
placeholder,
223230
place: e.currentTarget.getAttribute('data-place') || this.props.place || 'top',
224231
type: e.currentTarget.getAttribute('data-type') || this.props.type || 'dark',
225-
effect: isFocus && 'solid' || e.currentTarget.getAttribute('data-effect') || this.props.effect || 'float',
232+
effect: switchToSolid && 'solid' || e.currentTarget.getAttribute('data-effect') || this.props.effect || 'float',
226233
offset: e.currentTarget.getAttribute('data-offset') || this.props.offset || {},
227234
html: e.currentTarget.getAttribute('data-html')
228235
? e.currentTarget.getAttribute('data-html') === 'true'

0 commit comments

Comments
 (0)