React/MUI Popover 使用anchorPosition 定位不正确

2024-01-24

我在 a 中使用 React/MUI Popover反应窗口 https://github.com/bvaughn/react-window列表元素并且无法使 Popover 正确定位 - 它总是出现在窗口的左上角(该组件无法在锚元素上执行 getBoundingClientRctd() [anchorEl在文档中])。

因此,为了暂时解决这个问题,我决定使用anchorPosition参数允许设置绝对位置——在我的例子中,只是窗口的中间。那也不行。

我已经检查了 Chrome DevTools 中的值,一切似乎都正常(即,当我使用它时,我确实得到了一个anchorEl;我得到了有效的positionLeft/Top值;等等......

可能是非常简单的事情,希望有人能指出我做错了什么。

编辑:解决方案的关键要素

  1. Row组件必须在包含组件之外定义。
  2. the <List>组件有一个itemData用于将自定义数据传递给的属性Row.

编辑以添加反应窗口列表渲染器。

这是基本设置:

弹出框渲染器

  renderPopover(template, itemId) {
    const { anchorEl, anchorId } = this.state;
    const { classes } = this.props;
    const open = Boolean(anchorEl) && anchorId === itemId;
    const bgColor = '#babaef';
    const { innerHeight, innerWidth } = window;
    const positionLeft = innerWidth / 2;
    const positionTop = innerHeight / 2;
    console.log(`renderPopover: ${positionLeft} / ${positionTop}`);
      <Popover
        id="simple-popper"
        open={open}
        style={{ color: 'Black' }}
        anchorEl={anchorEl}
        onClose={event => this.handlePopoverClose(event)}
        anchorPosition={{ left: {positionLeft}, top: {positionTop} }}
        anchorReference="anchorPosition"
      >
        <Typography style={{ backgroundColor: bgColor }} className={classes.typography}>
          {this.renderScheduleElements(template, itemId)}
        </Typography>
      </Popover>
    );
  }

按钮元素渲染器

 renderScheduleComponent(template, itemId) {
    const { anchorEl, anchorId } = this.state;
    const open = Boolean(anchorEl) && anchorId === itemId;
    const { classes } = this.props;
    const id = open ? 'simple-popper' : undefined;
    return (
      <Grid key={itemId} item>
        <Paper className={classes.paper}>
          <div style={{ padding: '4px' }}>
            <Button
              NO_ref={itemId}
              NODE_ref={(node) => this.buttonRef = node}
              id={itemId}
              name={itemId}
              aria-owns={id}
              aria-haspopup="true"
              variant="outlined"
              color="primary"
              style={{
                fontWeight: 'bold',
                padding: '8px',
                margin: 'auto',
                display: 'block',
                width: '100%',
              }}
              onClick={event => this.handlePopoverClick(event, itemId)}
            >
              {template.templateName}
            </Button>
            {(this.renderPopover).call(this, template, itemId)}
          </div>
        </Paper>
      </Grid>
    );
  }

单击事件处理程序

  handlePopoverClick(event, id) {
  event.preventDefault();
    console.log(`handlePopoverClick : ${event.currentTarget.name}`);
    this.setState({
      anchorEl: event.currentTarget,
      anchorId: id,
    });
  }

React-Window 列表渲染器

  renderScheduleColumn(columnData) {
    const { classes } = this.props;
    const { scheduleDate, scheduleTemplates } = columnData;
    this.scheduleTemplates = scheduleTemplates;

    const Row = ({ index, style }) => {
      return (
        <div className={index % 2 ? "ListItemOdd" : "ListItemEven"} style={style}>
          {this.renderScheduleComponent(scheduleTemplates[index], `${scheduleDate}:${index}`)}
        </div>
      );
    }

    const { columnHeight, columnWidth } = this.state;
    return (
      <Grid id={scheduleDate} key={scheduleDate} item>
        <Paper className={classes.paper}>
          <div style={{ width: '100%', textAlign: 'center' }}>
            <Typography variant="h6" style={{ padding: '24px', color: 'white', backgroundColor: '#3f51b5' }}>
              {scheduleDate}
            </Typography>
          </div>
          <List
            className="List"
            height={columnHeight}
            itemCount={scheduleTemplates.length}
            itemSize={50}
            width={columnWidth}
          >
            {Row}
          </List>
        </Paper>
      </Grid>
    );
  }

它看起来与这里类似的问题:React Material-UI 菜单锚被反应窗口列表破坏 https://stackoverflow.com/questions/55192935/react-material-ui-menu-anchor-broken-by-react-window-list/55203218#55203218.

你应该移动你的定义Row函数出renderScheduleColumn使其成为一致的类型。这将需要移动/返工renderScheduleComponent以及。您可以使用列表上的 itemData 属性将信息传递到行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React/MUI Popover 使用anchorPosition 定位不正确 的相关文章

随机推荐

  • 网站目录之外的参考文件

    如何引用网站根目录之外的文件 例如我的网站位于 C dev TestSite 我正在使用 ASP NET 和 XSP Web 应用程序将使用 mod mono 部署在 Apache 上 我在 C images 中有图像 我想这样做 img
  • Unicode HTML 标题在 IE、Chrome 中显示为框

    这是我的 HTML 另请参阅http www caudillweb com test test htm http www caudillweb com test test htm h1 日历 h1 以下是我分别在 IE Chrome 和 F
  • 如何在 TensorFlow 中水平连接两个张量?

    我有 2 个形状张量 100 4 and 100 2 我想在 TensorFlow 中执行串联操作 类似于np hstack 在 NumPy 中 这样输出的形状 100 6 有 TensorFlow 函数可以做到这一点吗 您可以使用tf c
  • 如何使用vimeo高级API显示视频

    我使用 Vimeo simple API 来显示我网站上某个频道的视频 但您可能知道 它有一个限制 我想知道您能否给我一个如何使用高级 API 的示例 我已阅读文档 但我只是不知道如何使用这些方法 显然我不是 php 专家 因此 如果您能给
  • WPF 列表框按钮所选项目

    我有一个带有一些文本块和一个按钮的列表框 在按钮的代码隐藏中 它调用一个传递当前选定的列表框项目的方法 这非常有用 问题是 当我选择一个项目 然后单击另一个项目上的按钮时 它不会更新 SelectedItem 属性 有没有办法 Xaml 或
  • 如何在C++中调用所有基类的复制构造函数来复制钻石继承中的大多数派生类对象?

    考虑下面的代码 include
  • 需要图像编辑库的建议

    我正在尝试创建一个基于 WPF 的应用程序 该应用程序将允许用户编辑照片并提供以下功能 增加 减少亮度 Crop 复古外观 还有更多 我还期待提供更多奇特的功能 例如 构图 打印到 T 恤 但这些功能的优先级较低 有相关的开源库吗 P S
  • 在 C 中使用 #define 定义路径

    我想定义这样的路径 define PATH abc xyz lmn 该 PATH 是一个包含文件 foo1 foo2 foo3 foo115 的目录 如何在 open 调用中使用这个 define 来打开 foo1 foo2 foo115
  • 查询Windows显示缩放比例

    I want to query the Windows display scaling setting programmatically In this case I want it to return 125 since I config
  • 如何使用斯坦福 CoreNLP 对预标记文本进行 NER 和 POS 标记?

    我在我的应用程序中使用斯坦福大学的 CoreNLP 命名实体识别器 NER 和词性 POS 标记器 问题是我的代码预先标记了文本 然后我需要对每个标记进行 NER 和 POS 标记 但是 我只能找到如何使用命令行选项来执行此操作 而不能以编
  • 没有冲突的合并是否等于没有冲突的rebase?

    这是正确的吗git merge当且仅当相应的git rebase会在没有冲突的情况下成功吗 不 事实上有一个简单的情况 合并工作正常 但变基却不行 o A lt mainline B C C lt branch where C是一个与以下冲
  • 数据绑定错误 - 找不到访问器

    我用谷歌搜索但仍然没有找到适合我的解决方案 这是我的 xml
  • 带标志的数组

    假设我有一个布尔标志数组 它会根据条件设置为 true 或 false 假设索引 1 2 6 已设置 所有其他标志均未设置 我需要调用functionA 如果索引 2 3 5 已设置并且所有其他标志未设置 我需要调用functionB 除了
  • 如何使角度组件与窗口的实际尺寸全屏显示

    我正在使用 Angular 在我的 app component html 中我有一些像这样的组件
  • 在c语句中计算token

    以下 C 语句中的标记数 printf i d i x i i 我认为这里有 12 个令牌 但我的答案是错误的 谁能告诉我如何找到上述 C 语句中的标记 PS 我知道标记是编译器不会分解为组件元素的源程序文本 据我了解C代码解析 标记是 总
  • 在 UITableView 中取消选择行时如何从数组中删除项目

    我正在尝试从 a 中捕获选定的项目UITableView并将它们保存到一个新数组中 以下代码通过在点击行时添加项目来创建新数组 但它不会在取消选择行时删除项目 如何从列表中删除项目newFruitList当a中的一行UITableView被
  • 在 Ant build.xml 中使用 Compass 编译 SASS

    有谁知道如何使用 JRuby 和 Compass 模块在 build xml 中编译 SASS scss 文件 我可以在 sass 独立安装中使用 Sass Exec 模块 通过以下方式从 scss 编译为 css
  • NHibernate 错误消息:此 SqlParameterCollection 的索引 3 无效,计数 = 3

    I have a test database design like this 以下是伪代码 BhillHeader public class BillHeader public BillHeader BillDetails new Lis
  • std::regex 和忽略标志

    学习基础后c questions tagged c 2b 2b规则 我专注于std regex 创建两个控制台应用程序 1 renrem and 2 bfind 我决定创建一些方便的函数来处理regex in c questions tag
  • React/MUI Popover 使用anchorPosition 定位不正确

    我在 a 中使用 React MUI Popover反应窗口 https github com bvaughn react window列表元素并且无法使 Popover 正确定位 它总是出现在窗口的左上角 该组件无法在锚元素上执行 get