diff --git a/docs/zh-CN/components/form/options.md b/docs/zh-CN/components/form/options.md index 7c224d5713e..6c232a84493 100755 --- a/docs/zh-CN/components/form/options.md +++ b/docs/zh-CN/components/form/options.md @@ -1609,3 +1609,4 @@ order: 2 | itemHeight | `number` | `32` | 每个选项的高度,用于虚拟渲染 | | virtualThreshold | `number` | `100` | 在选项数量超过多少时开启虚拟渲染 | | valuesNoWrap | `boolean` | `false` | 默认情况下多选所有选项都会显示,通过这个可以最多显示一行,超出的部分变成 ... | +| clearValueOnSourceChange | `boolean` | | `source`从数据域取值时,数据域值变化后是否自动清空 | diff --git a/packages/amis-core/src/renderers/Options.tsx b/packages/amis-core/src/renderers/Options.tsx index 62007bb3381..d6b717d71e1 100644 --- a/packages/amis-core/src/renderers/Options.tsx +++ b/packages/amis-core/src/renderers/Options.tsx @@ -196,6 +196,11 @@ export interface FormOptionsControl extends FormBaseControl { * 选项删除提示文字。 */ deleteConfirmText?: string; + + /** + * source从数据域取值时,数据域值变化后是否自动清空 + */ + clearValueOnSourceChange?: boolean; } export interface OptionsBasicConfig extends FormItemBasicConfig { @@ -434,7 +439,8 @@ export function registerOptionsControl(config: OptionsConfig) { formItem.loadOptionsFromDataScope( props.source as string, props.data, - this.changeOptionValue + this.changeOptionValue, + props.clearValueOnSourceChange ); this.normalizeValue(); diff --git a/packages/amis-core/src/store/formItem.ts b/packages/amis-core/src/store/formItem.ts index b6d18ba4d56..f6aaa13d06c 100644 --- a/packages/amis-core/src/store/formItem.ts +++ b/packages/amis-core/src/store/formItem.ts @@ -866,7 +866,12 @@ export const FormItemStore = StoreNode.named('FormItemStore') function loadOptionsFromDataScope( source: string, ctx: Record, - onChange?: (value: any) => void + onChange?: ( + value: any, + submitOnChange?: boolean, + changeImmediately?: boolean + ) => void, + clearValue?: boolean ) { let options: any[] = resolveVariableAndFilter(source, ctx, '| raw'); @@ -892,6 +897,13 @@ export const FormItemStore = StoreNode.named('FormItemStore') setOptions(options, onChange, ctx); + // source从数据域获取,同时发生变化时,需要清空当前表单项 + if (clearValue && !self.selectFirst) { + self.selectedOptions.some((item: any) => item.__unmatched) && + onChange && + onChange('', false, true); + } + return options; }