Skip to content

Commit 1257d1f

Browse files
committed
Make getContent to support dynamical generate content while hover
1 parent 7f6b50f commit 1257d1f

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ example/
99
test/
1010
bin/
1111
CHANGELOG.md
12+
src/
1213

1314
// setting files
1415
.babelrc

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class | data-class | String | | extra custom class, can use !important to
6161
delayHide | data-delay-hide | Number | | `<p data-tip="tooltip" data-delay-hide='1000'></p>` or `<ReactTooltip delayHide={1000} />`
6262
delayShow | data-delay-show | Number | | `<p data-tip="tooltip" data-delay-show='1000'></p>` or `<ReactTooltip delayShow={1000} />`
6363
border | data-border | Bool | true, false | Add one pixel white border
64+
getContent | null | Func or Array | () => {}, [() => {}, Interval] | Generate the tip content dynamically
6465

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

example/src/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,36 @@ const Test = React.createClass({
221221
</div>
222222
</pre>
223223
</div>
224+
225+
<div className="section">
226+
<h4 className='title'>Update tip content over time</h4>
227+
<p className="sub-title"></p>
228+
229+
<div className="example-jsx">
230+
<div className="side">
231+
<a data-for='getContent' data-tip>=͟͟͞͞( •̀д•́)</a>
232+
<ReactTooltip id='getContent' getContent={() => Math.floor(Math.random() * 100)}/>
233+
</div>
234+
235+
<div className="side">
236+
<a data-for='overTime' data-tip>=͟͟͞͞( •̀д•́)</a>
237+
<ReactTooltip id='overTime'
238+
getContent={[() => {return new Date().toISOString()}, 1000]}/>
239+
</div>
240+
</div>
241+
<br />
242+
<pre className='example-pre'>
243+
<div>
244+
<p>{"<a data-for='getContent' data-tip>=͟͟͞͞( •̀д•́)</a>\n" +
245+
"<ReactTooltip id='getContent' getContent={() => Math.floor(Math.random() * 100)} />"}</p>
246+
</div>
247+
248+
<div>
249+
<p>{"<a data-for='overTime' data-tip>=͟͟͞͞( •̀д•́)</a>\n" +
250+
"<ReactTooltip id='overTime' getContent={[() => {return new Date().toISOString()}, 1000]}/>"}</p>
251+
</div>
252+
</pre>
253+
</div>
224254
</section>
225255
</div>
226256
)

src/index.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ReactTooltip extends Component {
3838
watchWindow: PropTypes.bool,
3939
isCapture: PropTypes.bool,
4040
globalEventOff: PropTypes.string,
41-
getContent: PropTypes.func
41+
getContent: PropTypes.any
4242
}
4343

4444
constructor (props) {
@@ -64,6 +64,7 @@ class ReactTooltip extends Component {
6464
this.mount = true
6565
this.delayShowLoop = null
6666
this.delayHideLoop = null
67+
this.intervalUpdateContent = null
6768
}
6869

6970
componentDidMount () {
@@ -75,8 +76,7 @@ class ReactTooltip extends Component {
7576
componentWillUnmount () {
7677
this.mount = false
7778

78-
clearTimeout(this.delayShowLoop)
79-
clearTimeout(this.delayHideLoop)
79+
this.clearTimer()
8080

8181
this.unbindListener()
8282
this.removeScrollListener()
@@ -170,11 +170,13 @@ class ReactTooltip extends Component {
170170
const originTooltip = e.currentTarget.getAttribute('data-tip')
171171
const isMultiline = e.currentTarget.getAttribute('data-multiline') || multiline || false
172172

173-
let content
174-
if (children) {
175-
content = children
176-
} else if (getContent) {
177-
content = getContent()
173+
let content = children
174+
if (getContent) {
175+
if (Array.isArray(getContent)) {
176+
content = getContent[0] && getContent[0]()
177+
} else {
178+
content = getContent()
179+
}
178180
}
179181

180182
const placeholder = getTipContent(originTooltip, content, isMultiline)
@@ -193,6 +195,16 @@ class ReactTooltip extends Component {
193195
}, () => {
194196
this.addScrollListener(e)
195197
this.updateTooltip(e)
198+
199+
if (getContent && Array.isArray(getContent)) {
200+
this.intervalUpdateContent = setInterval(() => {
201+
const {getContent} = this.props
202+
const placeholder = getTipContent(originTooltip, getContent[0](), isMultiline)
203+
this.setState({
204+
placeholder
205+
})
206+
}, getContent[1])
207+
}
196208
})
197209
}
198210

@@ -228,8 +240,7 @@ class ReactTooltip extends Component {
228240

229241
if (!this.mount) return
230242

231-
clearTimeout(this.delayShowLoop)
232-
clearTimeout(this.delayHideLoop)
243+
this.clearTimer()
233244
this.delayHideLoop = setTimeout(() => {
234245
this.setState({
235246
show: false
@@ -282,6 +293,15 @@ class ReactTooltip extends Component {
282293
}
283294
}
284295

296+
/**
297+
* CLear all kinds of timeout of interval
298+
*/
299+
clearTimer () {
300+
clearTimeout(this.delayShowLoop)
301+
clearTimeout(this.delayHideLoop)
302+
clearInterval(this.intervalUpdateContent)
303+
}
304+
285305
render () {
286306
const {placeholder, extraClass, html} = this.state
287307
let tooltipClass = classname(

0 commit comments

Comments
 (0)