Set different React Popper tooltip for each element

3k views Asked by At

I am using React popper for displaying additional information for input fields in my form. The problem is that, when i am displaying Tooltip for more than 1 element, it displays the same tooltip. How can i display different tooltips for each field.

Here is the code i am using inside my Component

https://codesandbox.io/s/hungry-gould-modgk?fontsize=14&hidenavigation=1&theme=dark

  // Popper Tooltip Props;
  const {
    getArrowProps,
    getTooltipProps,
    setTooltipRef,
    setTriggerRef,
    visible,
  } = usePopperTooltip({
    trigger: 'hover',
    placement: 'right',
    closeOnOutsideClick: false,
    visible: controlledVisible,
    onVisibleChange: setControlledVisible
  })
return (
                  <TextBox 
                    label="Title"
                    className="title-field"
                    name="title"
                    type="text"
                    isRequired={true}
                  />
                  <div className="field-info" ref={setTriggerRef}>
                    <Icon size="medium">
                      <FontAwesomeIcon icon={faInfoCircle} size="lg" />
                    </Icon>
                  </div>
                  {visible && (
                    <div
                      ref={setTooltipRef}
                      {...getTooltipProps({ className: 'tooltip-container' })}
                    >
                      Tooltip element
                      <div {...getArrowProps({ className: 'tooltip-arrow' })} />
                    </div>
                  )}

                  <TextBox 
                    label="Price"
                    className="price-field"
                    name="price"
                    type="text"
                    isRequired={true}
                  />
                   <div className="field-info" ref={setTriggerRef}>
                    <Icon size="medium">
                      <FontAwesomeIcon icon={faInfoCircle} size="lg" />
                    </Icon>
                  </div>
                  {visible && (
                    <div
                      ref={setTooltipRef}
                      {...getTooltipProps({ className: 'tooltip-container' })}
                    >
                      Tooltip element
                      <div {...getArrowProps({ className: 'tooltip-arrow' })} />
                    </div>
                  )}
)


  [1]: https://i.stack.imgur.com/oHuBA.png
1

There are 1 answers

7
Coot3 On BEST ANSWER

Each tooltip needs its own visible state variable. Can you create your own tooltip component like so:

const MyTooltip = ({tooltipText}) => {

    const [isVisible, setIsVisible] = useState(false)

    const {
        getArrowProps,
        getTooltipProps,
        setTooltipRef,
        setTriggerRef,
        visible,
      } = usePopperTooltip({
        trigger: 'hover',
        placement: 'right',
        closeOnOutsideClick: false,
        visible: isVisible,
        onVisibleChange: setIsVisible
      })
    return (
        <>
            <div className="field-info" ref={setTriggerRef}>
                <Icon size="medium">
                    <FontAwesomeIcon icon={faInfoCircle} size="lg" />
                </Icon>
            </div>
            {visible && (
            <div
                ref={setTooltipRef}
                {...getTooltipProps({ className: 'tooltip-container' })}
            >
                {tooltipText}
                <div {...getArrowProps({ className: 'tooltip-arrow' })} />
            </div>
            )}
        </>
    )
}
  

Then you can use the component like this:

<TextBox 
    label="Title"
    className="title-field"
    name="title"
    type="text"
    isRequired={true}
/>
<MyTooltip tooltipText="Tooltip Element 1" />
<TextBox 
    label="Price"
    className="price-field"
    name="price"
    type="text"
    isRequired={true}
/>
<MyTooltip tooltipText="Tooltip Element 2" />