mgh
mgh

Reputation: 407

onClick is not working on Popper component in Material-UI Tooltip

The intended behavior is to go to another page when tooltip is clicked. to achieve this functionality I provided the onClick prop for the PopperProps object.

Below is my code:

interface Props extends TooltipProps{}

const CustomTooltip : React.FC<Props> = (props) => {
  const { arrow, ...classes } = useStylesBootstrap();
  const [arrowRef, setArrowRef] = React.useState<HTMLSpanElement | null>(null);

  const handleClick = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
    event.preventDefault();
    event.stopPropagation();
    console.log("clicked");
    window.location.replace('/info/contact-us');
  };

  return (
    <Tooltip
      classes={classes}
      PopperProps={{
        style: { border: "1px red solid" },
        onClick: handleClick,
        popperOptions: {
          modifiers: {
            arrow: {
              enabled: Boolean(arrowRef),
              element: arrowRef,
            },
          },
        },
      }}
      {...props}
      title={(
        <>
          {props.title}
          <span className={arrow} ref={setArrowRef} />
        </>
        )}
    />
  );
};

Upvotes: 3

Views: 4668

Answers (1)

Ryan Cogswell
Ryan Cogswell

Reputation: 81006

You need to provide the interactive prop for the Tooltip. When interactive is not specified, Tooltip disables pointer events (so click doesn't do anything), but pointer-events is set to auto when interactive is true. The interactive prop also prevents the Tooltip from immediately closing as you move the mouse from the triggering element to the Tooltip.

Here is a working example:

import React from "react";
import DeleteIcon from "@material-ui/icons/Delete";
import IconButton from "@material-ui/core/IconButton";
import Tooltip from "@material-ui/core/Tooltip";

export default function SimpleTooltips() {
  return (
    <div>
      <Tooltip
        interactive
        title={
          <div onClick={() => console.log("clicked tooltip text")}>Delete</div>
        }
      >
        <IconButton aria-label="delete">
          <DeleteIcon />
        </IconButton>
      </Tooltip>
    </div>
  );
}

Edit Interactive Tooltip

Upvotes: 5

Related Questions