Skip to content

Commit 88d98f0

Browse files
authored
Merge pull request #173 from wwayne/callback
Add new attribute afterShow and afterHide
2 parents 8adb152 + f02e0a8 commit 88d98f0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npm install react-tooltip
1919
1 . Require react-tooltip after installation
2020

2121
```js
22-
var ReactTooltip = require("react-tooltip")
22+
import ReactTooltip from 'react-tooltip'
2323
```
2424

2525
2 . Add data-tip = "your placeholder" to your element
@@ -63,6 +63,8 @@ class | data-class | String | | extra custom class, can use !important to
6363
border | data-border | Bool | true, false | Add one pixel white border
6464
getContent | null | Func or Array | () => {}, [() => {}, Interval] | Generate the tip content dynamically
6565
countTransform | data-count-transform | Bool | True, False | Tell tooltip if it needs to count parents' transform into position calculation, the default is true, but it should be set to false when using with react-list
66+
afterShow | null | Func | () => {} | Function that will be called after tooltip show
67+
afterHide | null | Func | () => {} | Function that will be called after tooltip hide
6668

6769
## Using react component as tooltip
6870
Check the example [React-tooltip Test](http://wwayne.com/react-tooltip)

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class ReactTooltip extends Component {
4040
isCapture: PropTypes.bool,
4141
globalEventOff: PropTypes.string,
4242
getContent: PropTypes.any,
43-
countTransform: PropTypes.bool
43+
countTransform: PropTypes.bool,
44+
afterShow: PropTypes.func,
45+
afterHide: PropTypes.func
4446
}
4547

4648
constructor (props) {
@@ -255,24 +257,27 @@ class ReactTooltip extends Component {
255257
*/
256258
updateTooltip (e) {
257259
const {delayShow, show} = this.state
260+
const {afterShow} = this.props
258261
let {placeholder} = this.state
259262
const delayTime = show ? 0 : parseInt(delayShow, 10)
260263
const eventTarget = e.currentTarget
261264

262265
const updateState = () => {
263266
if (typeof placeholder === 'string') placeholder = placeholder.trim()
264267
if (Array.isArray(placeholder) && placeholder.length > 0 || placeholder) {
268+
const isInvisible = !this.state.show
265269
this.setState({
266270
currentEvent: e,
267271
currentTarget: eventTarget,
268272
show: true
269273
}, () => {
270274
this.updatePosition()
275+
if (isInvisible && afterShow) afterShow()
271276
})
272277
}
273278
}
274279

275-
this.clearTimer()
280+
clearTimeout(this.delayShowLoop)
276281
if (delayShow) {
277282
this.delayShowLoop = setTimeout(updateState, delayTime)
278283
} else {
@@ -285,14 +290,18 @@ class ReactTooltip extends Component {
285290
*/
286291
hideTooltip () {
287292
const {delayHide} = this.state
293+
const {afterHide} = this.props
288294

289295
if (!this.mount) return
290296

291297
const resetState = () => {
298+
const isVisible = this.state.show
292299
this.setState({
293300
show: false
301+
}, () => {
302+
this.removeScrollListener()
303+
if (isVisible && afterHide) afterHide()
294304
})
295-
this.removeScrollListener()
296305
}
297306

298307
this.clearTimer()

0 commit comments

Comments
 (0)