react-tooltip icon indicating copy to clipboard operation
react-tooltip copied to clipboard

Best way to display multiple data in tooltip

Open AdrienLemaire opened this issue 5 years ago • 12 comments

I would like to show a title and a description in each of my tooltips. Something like below:

        {badges.map(badge => (
          <FlexCol xs={4} sm={3} className="badge" key={badge.pk}>
            <img
              className="badge-image"
              src={urljoin(MEDIA_URL, badge.imageUrl)}
              alt={badge.name}
              data-multiline
              data-tip={badge}
              data-for="badge-tooltip"
            />
            <ReactTooltip
              id="badge-tooltip"
              getContent={({name, description}) => (
                <React.Fragment>
                  <p>
                    <strong>{name}</strong>
                  </p>
                  <p>{description}</p>
                </React.Fragment>
              )}
            />
          </FlexCol>
        }

The problem is, html5 data attributes only seem to handle strings, so we cannot send arrays or objects to it. I currently cannot see any other solution than concatenating my 2 data items in data-tip, when split it within getContent, which is far from optimal.

Would it be possible to handle any data-tip-* attribute for me to set data-tip-name and data-tip-description attributes, and easily reuse them from getContent ?

AdrienLemaire avatar Aug 29 '18 02:08 AdrienLemaire

I know there was a recent PR to pass the value of data-tip into getContent. If you use the most recent, then 'badge' should get passed in, and you could reference badge.name and badge.description inside getContent().

Please try that, thanks!

aronhelser avatar Aug 29 '18 13:08 aronhelser

yes it is working ! thank you

oladhari avatar Sep 07 '18 03:09 oladhari

@aronhelser sorry, I closed without verifying due to @oladhari's comment, but this doesn't work. I had raised the issue using v3.8.0, and it's still broken with v3.8.1. As explained in the issue description, only a string can be given to getContent. We would like arrays or objects to be supported as well, to have a smaller/cleaner code footprint using the react-tooltip plugin.

AdrienLemaire avatar Sep 07 '18 08:09 AdrienLemaire

it is my mistake, I did not verify passing the object, while trying to pass an object i got this error: Uncaught TypeError: Cannot read property 'name' of null

oladhari avatar Sep 07 '18 08:09 oladhari

If I'll pass an object, it's automatically converted to string "[object Object]"...

ketysek avatar Sep 19 '18 12:09 ketysek

@ketysek I experience the same thing. Did you find a solution?

eccentricexit avatar Mar 07 '19 03:03 eccentricexit

@mtsalenc here the solution that we came with we created a function that handle the content in a customized component for the tooltip:


  handleContent = dataTip => {
    ReactTooltip.rebuild();
    if (!dataTip) {
      return "";
    }
    const [name, description] = dataTip.split("|");
    

    return description ? (
      <p>
        <strong>{name}</strong> <br />
        {description}
      </p>
    ) : null;
  };

in the component using the tooltip you just pass the data-tip like this:

data-tip={`${name}|${description}`}

hope it is helpful

oladhari avatar Mar 11 '19 07:03 oladhari

I did something very similar but using JSON.parse and JSON.stringify so I can manage the whole element and have more flexibility.

So instead of using const [name, description] = dataTip.split("|"); I did something like const obj = JSON.parse(dataTip) Here I can access obj.name and obj.description

And when passing the values, Instead of data-tip={${name}|${description}} I did: data-tip={JSON.stringify(the_object)} Where 'the_object' is the element that contains a name and description attributes

oriuken avatar May 03 '19 18:05 oriuken

@oladhari Where did you call the customized handleContent function to pass an object to dataTip?

capstonednc avatar Jul 29 '19 16:07 capstonednc

@capstonednc in the react-tooltip getContent

            <ReactTooltip id="badge-tooltip" getContent={this.handleContent} />

oladhari avatar Jul 30 '19 05:07 oladhari

@oladhari got it. I was still getting a [object Object] response, until I parsed the dataTip in the function and stringified it in the getContent prop. Thanks for the help!

capstonednc avatar Jul 30 '19 17:07 capstonednc

I kept getting [object Object] and then I removed html={true} from my props and the problem went away.

gxxcastillo avatar Jul 03 '22 13:07 gxxcastillo