【问题标题】:How to set Anchor to Popover in Material-UI如何在 Material-UI 中将锚点设置为 Popover
【发布时间】:2020-07-19 11:41:41
【问题描述】:

我在 Material-UI 中有一个弹出框,我想将锚点永久设置为按钮。不仅仅是点击event.currentTarget。 我怎样才能用打字稿做到这一点?

不幸的是,Material-UI 中的当前示例使用 event.currentTarget 并且引用它不起作用。

import React,{useRef} from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    typography: {
      padding: theme.spacing(2),
    },
  }),
);

export default function SimplePopover() {
  const classes = useStyles();

  const ref = useRef(null)


  return (
    <div>
      <Button ref={ref}  variant="contained" color="primary">
        Open Popover
      </Button>
      <Popover
        open
        anchorEl={ref}
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'center',
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'center',
        }}
      >
        <Typography className={classes.typography}>The content of the Popover.</Typography>
      </Popover>
    </div>
  );
}

Here 一个代码框。

【问题讨论】:

    标签: reactjs material-ui popover


    【解决方案1】:

    你一定漏掉了一些细节。我关注了Simple Popover example in official docs,它仍然有效。

    如果要使用useRef,请务必在设置anchorRef时参考buttonRef.current

    下面是分叉的代码框:

    【讨论】:

    • 它适用于 event.currentTarget。我的问题是如何在没有 event.currentTarget 的情况下将 anchorEl 永久设置为按钮。
    • 谢谢!你知道吗,为什么当我将open={open} 更改为open 时,它不再引用锚点了? codesandbox.io/s/popover-anchor-5xdon?file=/demo.tsx:935-946
    • @Jöcker 是的,首先,当组件被挂载时,buttonRef.currentnull 以便您还直接将 prop anchorRef 设置为 null ,在 buttonRef 被引用之前 @ 987654335@,导致了您不受欢迎的行为。为避免这种情况,请使用 useEffect 收听buttonRef 的更改。这是分叉的codesandbox
    • 我建议让 useEffect 注释更加突出 - 让我有些头疼,但在 cmets 中我几乎错过了它
    【解决方案2】:

    两件事

    • 您需要存储打开的弹出窗口的状态
    • 从外面打开
    • 从里面关闭它

    相关JS

    import React, { useRef, useState } from "react";
    import { makeStyles, createStyles, Theme } from "@material-ui/core/styles";
    import Popover from "@material-ui/core/Popover";
    import Typography from "@material-ui/core/Typography";
    import Button from "@material-ui/core/Button";
    
    const useStyles = makeStyles((theme: Theme) =>
      createStyles({
        typography: {
          padding: theme.spacing(2)
        }
      })
    );
    
    export default function SimplePopover() {
      const classes = useStyles();
    
      const ref = useRef(null);
    
      const [popOverVisible, setPopOverVisible] = useState(false);
      const togglePopOver = () => {
        popOverVisible === false
          ? setPopOverVisible(true)
          : setPopOverVisible(false);
      };
    
      return (
        <div>
          <Button
            ref={ref}
            variant="contained"
            color="primary"
            onClick={togglePopOver}
          >
            Open Popover
          </Button>
          Status: {popOverVisible.toString()}
          <Popover
            open={popOverVisible}
            anchorEl={ref}
            anchorOrigin={{
              vertical: "bottom",
              horizontal: "center"
            }}
            transformOrigin={{
              vertical: "top",
              horizontal: "center"
            }}
          >
            <Button
              ref={ref}
              variant="contained"
              color="primary"
              onClick={togglePopOver}
            >
              CLOSE Popover
            </Button>
            <Typography className={classes.typography}>
              The content of the Popover.
            </Typography>
          </Popover>
        </div>
      );
    }
    

    编辑:根据用户的以下评论:

          <Popover
        open={popOverVisible}
        anchorEl={ref}
        anchorReference="anchorPosition"
        anchorPosition={{ top: 50, left: 140 }}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "left"
        }}
        transformOrigin={{
          vertical: "top",
          horizontal: "left"
        }}
      >
    

    分叉sandbox here

    【讨论】:

    • 弹出框应该在按钮的上下角。有了 ref,它就出现在屏幕中间...
    • @Jöcker,添加了 anchorReference="anchorPosition"anchorPosition={{ top: 50, left: 140 }} 以获取您想要的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多