Commit f9c0fff9 authored by afc163's avatar afc163

Should not mutate state, close #602

parent 9c690972
...@@ -17,8 +17,8 @@ export default class TableForm extends PureComponent { ...@@ -17,8 +17,8 @@ export default class TableForm extends PureComponent {
}); });
} }
} }
getRowByKey(key) { getRowByKey(key, newData) {
return this.state.data.filter(item => item.key === key)[0]; return (newData || this.state.data).filter(item => item.key === key)[0];
} }
index = 0; index = 0;
cacheOriginData = {}; cacheOriginData = {};
...@@ -35,14 +35,15 @@ export default class TableForm extends PureComponent { ...@@ -35,14 +35,15 @@ export default class TableForm extends PureComponent {
} }
toggleEditable(e, key) { toggleEditable(e, key) {
e.preventDefault(); e.preventDefault();
const target = this.getRowByKey(key); const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (target) { if (target) {
// 进入编辑状态时保存原始数据 // 进入编辑状态时保存原始数据
if (!target.editable) { if (!target.editable) {
this.cacheOriginData[key] = { ...target }; this.cacheOriginData[key] = { ...target };
} }
target.editable = !target.editable; target.editable = !target.editable;
this.setState({ data: [...this.state.data] }); this.setState({ data: newData });
} }
} }
remove(key) { remove(key) {
...@@ -51,7 +52,7 @@ export default class TableForm extends PureComponent { ...@@ -51,7 +52,7 @@ export default class TableForm extends PureComponent {
this.props.onChange(newData); this.props.onChange(newData);
} }
newMember = () => { newMember = () => {
const newData = [...this.state.data]; const newData = this.state.data.map(item => ({ ...item }));
newData.push({ newData.push({
key: `NEW_TEMP_ID_${this.index}`, key: `NEW_TEMP_ID_${this.index}`,
workId: '', workId: '',
...@@ -69,8 +70,8 @@ export default class TableForm extends PureComponent { ...@@ -69,8 +70,8 @@ export default class TableForm extends PureComponent {
} }
} }
handleFieldChange(e, fieldName, key) { handleFieldChange(e, fieldName, key) {
const newData = [...this.state.data]; const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key); const target = this.getRowByKey(key, newData);
if (target) { if (target) {
target[fieldName] = e.target.value; target[fieldName] = e.target.value;
this.setState({ data: newData }); this.setState({ data: newData });
...@@ -102,13 +103,14 @@ export default class TableForm extends PureComponent { ...@@ -102,13 +103,14 @@ export default class TableForm extends PureComponent {
cancel(e, key) { cancel(e, key) {
this.clickedCancel = true; this.clickedCancel = true;
e.preventDefault(); e.preventDefault();
const target = this.getRowByKey(key); const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (this.cacheOriginData[key]) { if (this.cacheOriginData[key]) {
Object.assign(target, this.cacheOriginData[key]); Object.assign(target, this.cacheOriginData[key]);
target.editable = false; target.editable = false;
delete this.cacheOriginData[key]; delete this.cacheOriginData[key];
} }
this.setState({ data: [...this.state.data] }); this.setState({ data: newData });
} }
render() { render() {
const columns = [{ const columns = [{
......
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