2

react-windowリスト要素内で React/MUI ポップオーバーを使用していますが、ポップオーバーを正しく配置できません。常にウィンドウの左上隅に表示されます (コンポーネントは getBoundingClientRctd( を実行できません) ) アンカー要素 [anchorElドキュメント内])。

その問題を一時的に回避するためにanchorPosition、絶対位置を設定できるパラメーターを使用することにしました。私の場合は、ウィンドウのちょうど真ん中です。それもうまくいきません。

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

1 に答える 1

1

ここと同様の問題のように見えます: React Material-UI メニュー アンカーが react-window list によって壊れています

Row関数の定義を から移動してrenderScheduleColumn、一貫した型にする必要があります。これには、移動/再加工renderScheduleComponentも必要になります。List の itemData プロパティを使用して、Row に情報を渡すことができます。

于 2019-05-20T21:04:59.897 に答える