React / MUI Popover与anchorPosition

时间:2019-05-20 19:21:17

标签: javascript reactjs material-ui react-window

我正在react-window列表元素内使用React / MUI Popover,并且无法使Popover正确定位-它总是出现在窗口的左上角(该组件无法打开)在文档的锚元素[anchorEl上执行getBoundingClientRctd()。

因此,为了暂时解决该问题,我决定使用anchorPosition参数,该参数可以设置绝对位置-在我的情况下,仅位于窗口的中间。那也不行。

我已经检查了Chrome DevTools中的值,并且一切似乎都还可以(即,使用时确实得到了anchorEl;我得到了有效的positionLeft / Top值;等等...

可能很简单,希望有人能指出我做错了。

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

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

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

这是基本设置:

Popover渲染器

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

反应窗口列表渲染器

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

1 个答案:

答案 0 :(得分:0)

似乎与这里的问题类似:React Material-UI menu anchor broken by react-window list

您应该将Row函数的定义移出renderScheduleColumn,以便它是一致的类型。这也将需要移动/重做renderScheduleComponent。您可以使用列表上的itemData属性将信息传递给行。