Skip to content

Commit 8448c93

Browse files
authored
Merge pull request #202 from wwayne/handle-getcontent-null
Hide tooltip when getContent return null or undefined, same for empty…
2 parents 19b9d85 + c3ffa00 commit 8448c93

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import ReactTooltip from 'react-tooltip'
108108
```
109109

110110
## Trouble Shooting
111-
### Using tooltip within the modal (e.g. [react-modal](https://github.com/reactjs/react-modal))
111+
### 1. Using tooltip within the modal (e.g. [react-modal](https://github.com/reactjs/react-modal))
112112
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:
113113

114114
1. Put `<ReactTooltip />` out of the `<Modal>`
@@ -118,6 +118,21 @@ The component was designed to set a `<Reactooltip />` one place then use tooltip
118118
>I suggest always put `<ReactTooltip />` in the Highest level or smart component of Redux, so you might need these static
119119
method to control tooltip's behaviour in some situations
120120

121+
### 2. Hide tooltip when getContent returns null or undefined
122+
When you set `getContent={() => {return null}}` or `getContent={() => {return}}` you will find the tooltip will dispaly `true`, that's because React will set the value of data-* to be 'true' automatically if there is no value to be set. So you have to set `data-tip=''` in this situaction.
123+
124+
```
125+
<p data-tip='' data-for='test'></p>
126+
<ReactTooltip id='test' getContent={() => { return null }}/>
127+
```
128+
129+
Same for empty children, if you don't want show the tooltip when the children is empty
130+
131+
```
132+
<p data-tip='' data-for='test'></p>
133+
<ReactTooltip id='test'>{}</ReactTooltip>
134+
```
135+
121136
## Article
122137
[How I insert sass into react component](https://medium.com/@wwayne_me/how-i-insert-sass-into-my-npm-react-component-b46b9811c226#.gi4hxu44a)
123138

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,16 @@ class ReactTooltip extends Component {
222222
const isMultiline = e.currentTarget.getAttribute('data-multiline') || multiline || false
223223

224224
// Generate tootlip content
225-
let content = children
225+
let content
226226
if (getContent) {
227227
if (Array.isArray(getContent)) {
228228
content = getContent[0] && getContent[0]()
229229
} else {
230230
content = getContent()
231231
}
232232
}
233-
const placeholder = getTipContent(originTooltip, content, isMultiline)
234-
const isEmptyTip = typeof placeholder === 'string' && placeholder === ''
233+
const placeholder = getTipContent(originTooltip, children, content, isMultiline)
234+
const isEmptyTip = typeof placeholder === 'string' && placeholder === '' || placeholder === null
235235

236236
// If it is focus event or called by ReactTooltip.show, switch to `solid` effect
237237
const switchToSolid = e instanceof window.FocusEvent || isGlobalCall

src/utils/getTipContent.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
*/
1414
import React from 'react'
1515

16-
export default function (tip, children, multiline) {
16+
export default function (tip, children, getContent, multiline) {
1717
if (children) return children
18+
if (getContent !== undefined && getContent !== null) return getContent // getContent can be 0, '', etc.
19+
if (getContent === null) return null // Tip not exist and childern is null or undefined
1820

1921
const regexp = /<br\s*\/?>/
2022
if (!multiline || multiline === 'false' || !regexp.test(tip)) {

0 commit comments

Comments
 (0)