ant-design / ant-design

An enterprise-class UI design language and React UI library
https://ant.design
MIT License
90.42k stars 46.41k forks source link

antd table 的 ExpandedRowRender是一个表单,当默认展开的时候,对表单进行赋值的时候,总是会对最后一个表单赋值。 #48670

Closed liushuhui closed 2 weeks ago

liushuhui commented 2 weeks ago

Reproduction link

https://react-21qt77.stackblitz.io/

Steps to reproduce

import { Table, Badge, Menu, Dropdown, Input, Form, Select } from "antd"; import { useState } from "react";

const { Option } = Select; export default () => { const [value, setValue] = useState(""); const expandedRowRender = () => { const handleChange = (e) => { setValue(e); }; return ( <> <Select style={{ width: 120 }} onChange={handleChange}>

      <Option value="lucy">Lucy</Option>
      <Option value="disabled" disabled>
        Disabled
      </Option>
      <Option value="Yiminghe">yiminghe</Option>
    </Select>
    <Input value={value} placeholder="Password" />
  </>
);

};

const columns = [ { title: "Name", dataIndex: "name", key: "name" }, { title: "Platform", dataIndex: "platform", key: "platform" }, { title: "Version", dataIndex: "version", key: "version" }, { title: "Upgraded", dataIndex: "upgradeNum", key: "upgradeNum" }, { title: "Creator", dataIndex: "creator", key: "creator" }, { title: "Date", dataIndex: "createdAt", key: "createdAt" }, { title: "Action", key: "operation", render: () => Publish }, ];

const data = []; for (let i = 0; i < 3; i) { data.push({ key: i, name: "Screem", platform: "iOS", version: "10.3.4.5654", upgradeNum: 500, creator: "Jack", createdAt: "2014-12-24 23:12:00", }); }

return ( <Table rowKey='key' className="components-table-demo-nested" columns={columns} expandable={{ expandedRowRender: expandedRowRender, }} dataSource={data} /> ); };

What is expected?

demo是一个简化版的需求,expandedRowRender返回的是一个表单,默认全部展开的情况下,当我对相应表单进行form.setFieldValue操作的时候,只是修改当前行的数据。而不影响其他的行的表单数据

What is actually happening?

demo是一个简化版的需求,expandedRowRender返回的是一个表单,默认全部展开的情况下,当我对相应表单进行form.setFieldValue操作的时候,假如有两行,我修改第一行或者第二行的表单值的时候,都会映射到第三行的表单去,也就是说对表单进行赋值的时候,总是会对最后一个表单赋值。

Environment Info
antd undefined
React 16.x
System widnow
Browser 谷歌
liushuhui commented 2 weeks ago

实际场景如下:父组件: image 子组件: image

afc163 commented 2 weeks ago

这样写相当于三个 Select 共用了一份 value 状态,肯定是一改都改了。你需要把状态隔离到各个子组件中。