From f02e0a806470c76704e9dc500ebff82ad2fd8396 Mon Sep 17 00:00:00 2001 From: wwayne Date: Wed, 3 Aug 2016 21:47:07 +0800 Subject: [PATCH] Add new attribute afterShow and afterHide --- README.md | 4 +++- src/index.js | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fd81811f4..2f114a4c9 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ npm install react-tooltip 1 . Require react-tooltip after installation ```js -var ReactTooltip = require("react-tooltip") +import ReactTooltip from 'react-tooltip' ``` 2 . Add data-tip = "your placeholder" to your element @@ -63,6 +63,8 @@ class | data-class | String | | extra custom class, can use !important to border | data-border | Bool | true, false | Add one pixel white border getContent | null | Func or Array | () => {}, [() => {}, Interval] | Generate the tip content dynamically 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 + afterShow | null | Func | () => {} | Function that will be called after tooltip show + afterHide | null | Func | () => {} | Function that will be called after tooltip hide ## Using react component as tooltip Check the example [React-tooltip Test](http://wwayne.com/react-tooltip) diff --git a/src/index.js b/src/index.js index a2808a200..bb87addae 100644 --- a/src/index.js +++ b/src/index.js @@ -40,7 +40,9 @@ class ReactTooltip extends Component { isCapture: PropTypes.bool, globalEventOff: PropTypes.string, getContent: PropTypes.any, - countTransform: PropTypes.bool + countTransform: PropTypes.bool, + afterShow: PropTypes.func, + afterHide: PropTypes.func } constructor (props) { @@ -255,6 +257,7 @@ class ReactTooltip extends Component { */ updateTooltip (e) { const {delayShow, show} = this.state + const {afterShow} = this.props let {placeholder} = this.state const delayTime = show ? 0 : parseInt(delayShow, 10) const eventTarget = e.currentTarget @@ -262,17 +265,19 @@ class ReactTooltip extends Component { const updateState = () => { if (typeof placeholder === 'string') placeholder = placeholder.trim() if (Array.isArray(placeholder) && placeholder.length > 0 || placeholder) { + const isInvisible = !this.state.show this.setState({ currentEvent: e, currentTarget: eventTarget, show: true }, () => { this.updatePosition() + if (isInvisible && afterShow) afterShow() }) } } - this.clearTimer() + clearTimeout(this.delayShowLoop) if (delayShow) { this.delayShowLoop = setTimeout(updateState, delayTime) } else { @@ -285,14 +290,18 @@ class ReactTooltip extends Component { */ hideTooltip () { const {delayHide} = this.state + const {afterHide} = this.props if (!this.mount) return const resetState = () => { + const isVisible = this.state.show this.setState({ show: false + }, () => { + this.removeScrollListener() + if (isVisible && afterHide) afterHide() }) - this.removeScrollListener() } this.clearTimer()