stopPropagation在组件的层次结构中不起作用

问题描述 投票:0回答:1

我试图避免每当在某一列数据下单击show / less时,都会在每个特定行上发生onClick事件。我有一个基本上具有选择和多重选择功能的网格。也有显示更多/更少显示之一的列。每当我单击Show More/Less时,都会触发行选择。我想避免这种情况。有人可以告诉我这里出了什么问题。说,如果有多个相似网格的实例,我希望该行为其中具有支柱的网格选择功能]

沙盒:https://codesandbox.io/s/react-table-row-table-alternate-single-row-working-5fr81

 import * as React from "react";
import ReactTable from "react-table";
import "react-table/react-table.css";

export default class DataGrid extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedRows: []
    };
  }

  rowClick = (state, rowInfo) => {
    if (rowInfo) {
      let selectedRows = new Set();
      return {
        onClick: e => {
          e.stopPropagation();
          if (e.ctrlKey) {
            selectedRows = this.state.selectedRows;
            rowInfo._index = rowInfo.index;
            if (
              selectedRows.filter(row => row._index === rowInfo._index)
                .length === 0
            )
              selectedRows.push(rowInfo);
            else
              selectedRows = selectedRows.filter(
                row => row._index !== rowInfo._index
              );
            this.setState({
              selectedRows
            });
          } else {
            selectedRows = [];
            rowInfo._index = rowInfo.index;
            selectedRows.push(rowInfo);
          }
          this.setState(
            {
              selectedRows
            },
            () => {
              console.log("final values", this.state.selectedRows);
              this.props.rowClicked(this.state.selectedRows);
            }
          );
        },
        style: {
          background:
            this.state.selectedRows.some(e => e._index === rowInfo.index) &&
            "#9bdfff"
        }
      };
    } else {
      return "";
    }
  };

  render() {
    return (
      <ReactTable
        data={this.props.data}
        columns={this.props.columns}
        getTrProps={this.rowClick}
      />
    );
  }
}

javascript reactjs javascript-events react-table stoppropagation
1个回答
1
投票

在ShowMore组件上,您可以修改onClick事件处理程序以调用event.stopPropagation()。这将停止捕获和冒泡阶段中当前事件的进一步传播,并允许您单击“显示更多/更少”而不触发行选择。

const toggleTruncate = (e) => {
  e.stopPropagation();
  setShowMore(!isShowMore);
}

反应实际上定义了合成事件,您可以在点击事件处理程序中使用它,而无需调用document.addEventListener()。您可以在here上的React中阅读有关事件处理的更多信息。

并回答问题的第二部分,鉴于rowClicked事件是DataGrid组件的可选道具,您将需要在DataGrid组件上进行手动检查以确保仅将其调用如果已定义。这是可以做到的:

if (rowClicked) {
 // do the rest
}

这就是您的rowClick方法的外观。请注意,第二行上有destructured props对象。这将确保仅在定义了rowClicked的情况下才执行行选择逻辑。

rowClick = (state, rowInfo) => {
  const { rowClicked } = this.props;
  if (rowInfo && rowClicked) {
    let selectedRows = new Set();
    return {
      onClick: e => {
        e.stopPropagation();
        if (e.ctrlKey) {
          selectedRows = this.state.selectedRows;
          rowInfo._index = rowInfo.index;
          if (
            selectedRows.filter(row => row._index === rowInfo._index)
              .length === 0
          )
            selectedRows.push(rowInfo);
          else
            selectedRows = selectedRows.filter(
              row => row._index !== rowInfo._index
            );
          this.setState({
            selectedRows
          });
        } else {
          selectedRows = [];
          rowInfo._index = rowInfo.index;
          selectedRows.push(rowInfo);
        }
        this.setState(
          {
            selectedRows
          },
          () => {
            rowClicked(this.state.selectedRows);                
          }
        );
      },
      style: {
        background:
          this.state.selectedRows.some(e => e._index === rowInfo.index) &&
          "#9bdfff"
      }
    };
  } else {
    return "";
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.