Commit 180e33a9 authored by ι™ˆεΈ…'s avatar ι™ˆεΈ…

fixed #1740 Advanced Form Member Management Cannot Add and Edit

parent 8c74bc6c
import React, { PureComponent, Fragment } from 'react';
import { Table, Button, Input, message, Popconfirm, Divider } from 'antd';
import isEqual from 'lodash.isequal';
import styles from './style.less';
export default class TableForm extends PureComponent {
......@@ -13,18 +14,20 @@ export default class TableForm extends PureComponent {
this.state = {
data: props.value,
loading: false,
editData: [],
/* eslint-disable-next-line react/no-unused-state */
value: props.value,
};
}
static getDerivedStateFromProps(nextProps) {
if ('value' in nextProps) {
static getDerivedStateFromProps(nextProps, preState) {
if (isEqual(nextProps.value, preState.value)) {
return null;
}
return {
data: nextProps.value,
value: nextProps.value,
};
}
return null;
}
getRowByKey(key, newData) {
const { data } = this.state;
......@@ -47,49 +50,34 @@ export default class TableForm extends PureComponent {
};
newMember = () => {
this.index += 1;
this.setState({
editData: [
{
const { data } = this.state;
const newData = data.map(item => ({ ...item }));
newData.push({
key: `NEW_TEMP_ID_${this.index}`,
workId: '',
name: '',
department: '',
editable: true,
isNew: true,
},
],
});
this.index += 1;
this.setState({ data: newData });
};
remove(key) {
const { editData, data } = this.state;
const { data } = this.state;
const { onChange } = this.props;
const editItem = editData.find(item => item.key === key);
if (editItem && editItem.key) {
// ε¦‚ζžœε­˜εœ¨ηΌ“ε­˜
if (this.cacheOriginData[key]) {
data.push(this.cacheOriginData[key]);
this.setState(
{
data,
},
() => {
delete this.cacheOriginData[key];
}
);
}
// 从 editData δΈ­εˆ ι™€
this.setState({
editData: editData.filter(item => item.key !== key),
});
return;
}
const newData = data.filter(item => item.key !== key);
this.setState({ data: newData });
onChange(newData);
}
handleKeyPress(e, key) {
if (e.key === 'Enter') {
this.saveRow(e, key);
}
}
handleFieldChange(e, fieldName, key) {
const { data } = this.state;
const newData = data.map(item => ({ ...item }));
......@@ -101,8 +89,6 @@ export default class TableForm extends PureComponent {
}
saveRow(e, key) {
const { data } = this.state;
const { onChange } = this.props;
e.persist();
this.setState({
loading: true,
......@@ -121,6 +107,8 @@ export default class TableForm extends PureComponent {
});
return;
}
const { data } = this.state;
const { onChange } = this.props;
delete target.isNew;
this.toggleEditable(e, key);
onChange(data);
......@@ -131,9 +119,9 @@ export default class TableForm extends PureComponent {
}
cancel(e, key) {
const { data } = this.state;
this.clickedCancel = true;
e.preventDefault();
const { data } = this.state;
const newData = data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (this.cacheOriginData[key]) {
......@@ -245,14 +233,15 @@ export default class TableForm extends PureComponent {
},
},
];
const { data, editData, loading } = this.state;
const dataSource = data.concat(editData);
const { loading, data } = this.state;
return (
<Fragment>
<Table
loading={loading}
columns={columns}
dataSource={dataSource}
dataSource={data}
pagination={false}
rowClassName={record => {
return record.editable ? styles.editable : '';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment