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

React/MUI Popover positioning incorrectly with anchorPosition

我在 react-window 列表元素中使用 React/MUI 弹出框,但无法正确定位弹出框 - 它总是在 [= 的左上角结束57=](组件无法对锚元素执行 getBoundingClientRctd() [anchorEl in the docs])。

所以为了暂时解决这个问题,我决定使用允许设置绝对位置的 anchorPosition 参数——在我的例子中,就在 window 的中间。那也不行。

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

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

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

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

编辑以添加 react-window 列表渲染器。

这是基本设置:

弹出框渲染器

  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>
    );
  }

它看起来像这里的类似问题:

您应该将 Row 函数的定义从 renderScheduleColumn 中移出,使其成为一致的类型。这也需要 moving/reworking renderScheduleComponent。您可以使用 List 上的 itemData 属性 将信息传递给 Row。