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 React, { PureComponent, Fragment } from 'react';
import { Table, Button, Input, message, Popconfirm, Divider } from 'antd'; import { Table, Button, Input, message, Popconfirm, Divider } from 'antd';
import isEqual from 'lodash.isequal';
import styles from './style.less'; import styles from './style.less';
export default class TableForm extends PureComponent { export default class TableForm extends PureComponent {
...@@ -13,18 +14,20 @@ export default class TableForm extends PureComponent { ...@@ -13,18 +14,20 @@ export default class TableForm extends PureComponent {
this.state = { this.state = {
data: props.value, data: props.value,
loading: false, loading: false,
editData: [], /* eslint-disable-next-line react/no-unused-state */
value: props.value,
}; };
} }
static getDerivedStateFromProps(nextProps) { static getDerivedStateFromProps(nextProps, preState) {
if ('value' in nextProps) { if (isEqual(nextProps.value, preState.value)) {
return null;
}
return { return {
data: nextProps.value, data: nextProps.value,
value: nextProps.value,
}; };
} }
return null;
}
getRowByKey(key, newData) { getRowByKey(key, newData) {
const { data } = this.state; const { data } = this.state;
...@@ -47,49 +50,34 @@ export default class TableForm extends PureComponent { ...@@ -47,49 +50,34 @@ export default class TableForm extends PureComponent {
}; };
newMember = () => { newMember = () => {
this.index += 1; const { data } = this.state;
this.setState({ const newData = data.map(item => ({ ...item }));
editData: [ newData.push({
{
key: `NEW_TEMP_ID_${this.index}`, key: `NEW_TEMP_ID_${this.index}`,
workId: '', workId: '',
name: '', name: '',
department: '', department: '',
editable: true, editable: true,
isNew: true, isNew: true,
},
],
}); });
this.index += 1;
this.setState({ data: newData });
}; };
remove(key) { remove(key) {
const { editData, data } = this.state; const { data } = this.state;
const { onChange } = this.props; 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); const newData = data.filter(item => item.key !== key);
this.setState({ data: newData }); this.setState({ data: newData });
onChange(newData); onChange(newData);
} }
handleKeyPress(e, key) {
if (e.key === 'Enter') {
this.saveRow(e, key);
}
}
handleFieldChange(e, fieldName, key) { handleFieldChange(e, fieldName, key) {
const { data } = this.state; const { data } = this.state;
const newData = data.map(item => ({ ...item })); const newData = data.map(item => ({ ...item }));
...@@ -101,8 +89,6 @@ export default class TableForm extends PureComponent { ...@@ -101,8 +89,6 @@ export default class TableForm extends PureComponent {
} }
saveRow(e, key) { saveRow(e, key) {
const { data } = this.state;
const { onChange } = this.props;
e.persist(); e.persist();
this.setState({ this.setState({
loading: true, loading: true,
...@@ -121,6 +107,8 @@ export default class TableForm extends PureComponent { ...@@ -121,6 +107,8 @@ export default class TableForm extends PureComponent {
}); });
return; return;
} }
const { data } = this.state;
const { onChange } = this.props;
delete target.isNew; delete target.isNew;
this.toggleEditable(e, key); this.toggleEditable(e, key);
onChange(data); onChange(data);
...@@ -131,9 +119,9 @@ export default class TableForm extends PureComponent { ...@@ -131,9 +119,9 @@ export default class TableForm extends PureComponent {
} }
cancel(e, key) { cancel(e, key) {
const { data } = this.state;
this.clickedCancel = true; this.clickedCancel = true;
e.preventDefault(); e.preventDefault();
const { data } = this.state;
const newData = data.map(item => ({ ...item })); const newData = data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData); const target = this.getRowByKey(key, newData);
if (this.cacheOriginData[key]) { if (this.cacheOriginData[key]) {
...@@ -245,14 +233,15 @@ export default class TableForm extends PureComponent { ...@@ -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 ( return (
<Fragment> <Fragment>
<Table <Table
loading={loading} loading={loading}
columns={columns} columns={columns}
dataSource={dataSource} dataSource={data}
pagination={false} pagination={false}
rowClassName={record => { rowClassName={record => {
return record.editable ? styles.editable : ''; 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