javascriptreactjspopperjs

Set different React Popper tooltip for each element


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.sstatic.net/oHuBA.png

Solution

  • 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" />