在特定条件下点击 table 展开一行 Ant Design table react js

Expand a row by click of table on a certain condition Ant Design table react js

我需要一点帮助。

我需要编写代码,使 table 行仅在它的切换打开时展开,当切换关闭时它不应展开。我已经使用 属性 expandRowByClick 在单击时展开一行。但这里的问题是,当开关关闭时,它不应该是可点击的,但现在一个空行会展开。我怎样才能避免它?

任何人都可以帮助我。谢谢。

沙盒link:https://codesandbox.io/s/purple-sun-1rtz1?file=/index.js

只需粘贴此代码。这是工作 。希望这会对你有所帮助。

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Switch } from "antd";

const { Column } = Table;

class EditableTable extends React.Component {
  state = {
    firstRow: false,
    secondrow: false
  };
  constructor(props) {
    super(props);
    this.state = {
      dataSource: [
        {
          key: "0",
          name: "Edward King 0",
          expandable: false
        },
        {
          key: "1",
          name: "Edward King 1",
          expandable: false
        }
      ]
    };
  }
  handleChnage = key => {
    let k = parseInt(key);
    let data = this.state.dataSource;
    let value = data[k].expandable;
    data[k].expandable = !value;
    this.setState({
      dataSource: data
    });
  };

  render() {
    const { dataSource } = this.state;
    return (
      <Table
        bordered
        dataSource={dataSource}
        pagination={false}
        expandRowByClick={true}
        expandable={{
          expandedRowRender: record => (
            <p style={{ margin: 0 }}>{"Here is expandable row"}</p>
          ),
          rowExpandable: record => record.expandable
        }}
      >
        <Column title="name" dataIndex="name" width="30%" editable={true} />
        <Column
          align="right"
          render={(text, record, index) => {
            return <Switch onChange={() => this.handleChnage(record.key)} />;
          }}
        />
      </Table>
    );
  }
}

ReactDOM.render(<EditableTable />, document.getElementById("container"));