From 3b31bc3264ecfbb46f2be401c59dc19135a0eb77 Mon Sep 17 00:00:00 2001 From: liaoxuezhi Date: Tue, 13 Jul 2021 18:19:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=201.2=20=E4=B8=AD?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=20selectFirst=20=E6=97=A0=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#2269)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderers/Form/Options.tsx | 34 +++++++++++++++++++++++++--------- src/store/formItem.ts | 5 +---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/renderers/Form/Options.tsx b/src/renderers/Form/Options.tsx index a0d5b4f9e..9c655b347 100644 --- a/src/renderers/Form/Options.tsx +++ b/src/renderers/Form/Options.tsx @@ -291,7 +291,7 @@ export function registerOptionsControl(config: OptionsConfig) { } = props; if (formItem) { - formItem.setOptions(normalizeOptions(options), onChange); + formItem.setOptions(normalizeOptions(options), this.changeOptionValue); this.reaction = reaction( () => JSON.stringify([formItem.loading, formItem.filteredOptions]), @@ -353,7 +353,7 @@ export function registerOptionsControl(config: OptionsConfig) { if (prevProps.options !== props.options && formItem) { formItem.setOptions( normalizeOptions(props.options || []), - props.onChange + this.changeOptionValue ); this.normalizeValue(); } else if ( @@ -378,7 +378,7 @@ export function registerOptionsControl(config: OptionsConfig) { if (prevOptions !== options) { formItem.setOptions( normalizeOptions(options || []), - props.onChange + this.changeOptionValue ); this.normalizeValue(); } @@ -397,7 +397,7 @@ export function registerOptionsControl(config: OptionsConfig) { props.data, undefined, true, - props.onChange + this.changeOptionValue ) .then(() => this.normalizeValue()); } @@ -654,7 +654,7 @@ export function registerOptionsControl(config: OptionsConfig) { normalizeOptions( resolveVariableAndFilter(source as string, data, '| raw') || [] ), - onChange + this.changeOptionValue ); return; } else if (!formItem || !isEffectiveApi(source, data)) { @@ -704,13 +704,29 @@ export function registerOptionsControl(config: OptionsConfig) { this.input && this.input.focus && this.input.focus(); } + @autobind + changeOptionValue(value: any) { + const { + onChange, + formInited, + setPrinstineValue, + value: originValue + } = this.props; + + if (formInited === false) { + originValue === undefined && setPrinstineValue?.(value); + } else { + onChange?.(value); + } + } + @autobind setOptions(options: Array, skipNormalize = false) { const formItem = this.props.formItem as IFormItemStore; formItem && formItem.setOptions( skipNormalize ? options : normalizeOptions(options || []), - this.props.onChange + this.changeOptionValue ); } @@ -843,7 +859,7 @@ export function registerOptionsControl(config: OptionsConfig) { ? options.splice(idx, 0, {...result}) : options.push({...result}); } - model.setOptions(options, this.props.onChange); + model.setOptions(options, this.changeOptionValue); } } @@ -939,7 +955,7 @@ export function registerOptionsControl(config: OptionsConfig) { ...origin, ...result }), - this.props.onChange + this.changeOptionValue ); } } @@ -997,7 +1013,7 @@ export function registerOptionsControl(config: OptionsConfig) { if (indexes) { model.setOptions( spliceTree(options, indexes, 1), - this.props.onChange + this.changeOptionValue ); } } diff --git a/src/store/formItem.ts b/src/store/formItem.ts index 92af19105..234fbeaf1 100644 --- a/src/store/formItem.ts +++ b/src/store/formItem.ts @@ -442,10 +442,7 @@ export const FormItemStore = StoreNode.named('FormItemStore') ? list : list[0]; - // @issue 这个判断不太准确 - if (form.inited && onChange) { - onChange(value); - } + onChange?.(value); } }