From 7fa8adec8010579fbe1685ff3c2c2188d6af70d2 Mon Sep 17 00:00:00 2001 From: 2betop <2betop.cn@gmail.com> Date: Fri, 8 May 2020 17:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=B2=A1=E6=9C=89=E5=8F=98?= =?UTF-8?q?=E5=8C=96,=E4=B8=8D=E8=A6=81=E5=8E=BB=E6=9B=B4=E6=96=B0store,?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=B2=A1=E5=BF=85=E8=A6=81=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E7=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderers/Table.tsx | 2 +- src/store/table.ts | 14 ++++---------- src/utils/helper.ts | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/renderers/Table.tsx b/src/renderers/Table.tsx index ec3f36c2..1cd649f4 100644 --- a/src/renderers/Table.tsx +++ b/src/renderers/Table.tsx @@ -408,7 +408,7 @@ export default class Table extends React.Component { item.change(values, savePristine); // 值发生变化了,需要通过 onSelect 通知到外面,否则会出现数据不同步的问题 - this.syncSelected(); + item.modified && this.syncSelected(); if ((!saveImmediately && !propsSaveImmediately) || savePristine) { return; diff --git a/src/store/table.ts b/src/store/table.ts index 52770cc9..0326fb1d 100644 --- a/src/store/table.ts +++ b/src/store/table.ts @@ -21,7 +21,8 @@ import { findTree, flattenTree, eachTree, - difference + difference, + immutableExtends } from '../utils/helper'; import {evalExpression} from '../utils/tpl'; @@ -159,15 +160,8 @@ export const Row = types }, change(values: object, savePristine?: boolean) { - self.data = { - ...self.data, - ...values - }; - - savePristine && - (self.pristine = { - ...self.data - }); + self.data = immutableExtends(self.data, values); + savePristine && (self.pristine = self.data); }, reset() { diff --git a/src/utils/helper.ts b/src/utils/helper.ts index 6d040cbd..61fcea84 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -331,6 +331,29 @@ export function isArrayChildrenModified( return false; } +export function immutableExtends(to: any, from: any, deep = false) { + // 不是对象,不可以merge + if (!isObject(to) || !isObject(from)) { + return to; + } + + let ret = to; + + Object.keys(from).forEach(key => { + const origin = to[key]; + const value = to[key]; + + // todo 支持深度merge + if (origin !== value) { + // 一旦有修改,就创建个新对象。 + ret = ret !== to ? ret : {...to}; + ret[key] = value; + } + }); + + return ret; +} + // 即将抛弃 export function makeColumnClassBuild( steps: number,