From 058324c9e838fbdfcb0f2120b8a05331369b59a4 Mon Sep 17 00:00:00 2001 From: 2betop <2betop.cn@gmail.com> Date: Wed, 25 Dec 2019 20:31:09 +0800 Subject: [PATCH] memorized formValue --- src/renderers/Form/Combo.tsx | 32 ++++++++++++++++++++++++-------- src/store/combo.ts | 9 +-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/renderers/Form/Combo.tsx b/src/renderers/Form/Combo.tsx index 8afc42260..4e8072686 100644 --- a/src/renderers/Form/Combo.tsx +++ b/src/renderers/Form/Combo.tsx @@ -21,6 +21,19 @@ import Select from '../../components/Select'; import {dataMapping} from '../../utils/tpl-builtin'; import {isEffectiveApi} from '../../utils/api'; import {Alert2} from '../../components'; +import memoize from 'fast-memoize'; + +const formatValue = memoize( + (value: any, index: number, data: any) => { + return createObject( + extendObject(data, {index, __index: index, ...data}), + value + ); + }, + { + serializer: (args: Array) => JSON.stringify(args.slice(0, 2)) + } +); export interface Condition { test: string; @@ -505,6 +518,11 @@ export default class ComboControl extends React.Component { refsMap: { [propName: number]: any; } = {}; + + makeFormRef = memoize((index: number) => (ref: any) => + this.formRef(ref, index) + ); + formRef(ref: any, index: number = 0) { if (ref) { while (ref && ref.getWrappedInstance) { @@ -523,7 +541,7 @@ export default class ComboControl extends React.Component { } formatValue(value: any, index: number) { - const {flat, data} = this.props; + const {flat, data, store} = this.props; if (flat) { value = { @@ -532,10 +550,8 @@ export default class ComboControl extends React.Component { } value = value || this.defaultValue; - return createObject( - extendObject(data, {index, __index: index, ...data}), - value - ); + + return formatValue(value, index, data); } pickCondition(value: any): Condition | null { @@ -754,7 +770,7 @@ export default class ComboControl extends React.Component { onChange: this.handleChange, onInit: this.handleFormInit, onAction: this.handleAction, - ref: (ref: any) => this.formRef(ref, index), + ref: this.makeFormRef(index), canAccessSuperData, value: undefined, formItemValue: undefined @@ -926,7 +942,7 @@ export default class ComboControl extends React.Component { onChange: this.handleChange, onInit: this.handleFormInit, onAction: this.handleAction, - ref: (ref: any) => this.formRef(ref, index), + ref: this.makeFormRef(index), canAccessSuperData, value: undefined, formItemValue: undefined @@ -1058,7 +1074,7 @@ export default class ComboControl extends React.Component { disabled: disabled, data: isObject(value) ? value : this.defaultValue, onChange: this.handleSingleFormChange, - ref: (ref: any) => this.formRef(ref), + ref: this.makeFormRef(0), onInit: this.handleSingleFormInit, canAccessSuperData } diff --git a/src/store/combo.ts b/src/store/combo.ts index 6ebffb9c9..fb147f9e0 100644 --- a/src/store/combo.ts +++ b/src/store/combo.ts @@ -1,11 +1,4 @@ -import { - types, - SnapshotIn, - getParent, - flow, - getEnv, - getRoot -} from 'mobx-state-tree'; +import {types, SnapshotIn} from 'mobx-state-tree'; import {iRendererStore} from './iRenderer'; import {FormItemStore, IFormItemStore} from './formItem'; import {FormStore, IFormStore} from './form';