Material-UI Popover组件确实触发了onClose事件

时间:2019-10-16 11:32:24

标签: javascript reactjs material-ui

我正在使用Material-ui Popover组件,Popover组件的打开由其状态处理,我定义了一个名为handlePopoverClose的函数来处理当我在“带有Popover的悬停”外部单击时,应该设置状态将anchorEl的值设置为null,然后将open的状态设置为false。但是这个功能动作没有触发,有人知道会发生什么吗?

import React from 'react';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(theme => ({
  popover: {
    pointerEvents: 'none',
  },
  paper: {
    padding: theme.spacing(1),
  },
}));

export default function MouseOverPopover() {
  const classes = useStyles();
  const [anchorEl, setAnchorEl] = React.useState(null);

  const handlePopoverOpen = event => {
    setAnchorEl(event.currentTarget);
  };

  const handlePopoverClose = () => {
    setAnchorEl(null);
  };

  const open = Boolean(anchorEl);

  return (
    <div>
      <Typography
        aria-owns={open ? 'mouse-over-popover' : undefined}
        aria-haspopup="true"
        onMouseEnter={handlePopoverOpen}
      >
        Hover with a Popover.
      </Typography>
      <Popover
        id="mouse-over-popover"
        className={classes.popover}
        classes={{
          paper: classes.paper,
        }}
        open={open}
        anchorEl={anchorEl}
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'left',
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'left',
        }}
        onClose={handlePopoverClose}
        disableRestoreFocus
      >
        <Typography>I use Popover.</Typography>
      </Popover>
    </div>
  );
}

1 个答案:

答案 0 :(得分:1)

您只需要从样式中删除以下代码:

popover: {
    pointerEvents: 'none',
  }

它将开始正常工作。现场演示:https://codesandbox.io/s/boring-snow-k6x1d