Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: select的source是表达式时,数据域发生变化后已选值不会自动清空 issue#10359 #10361

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/zh-CN/components/form/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1609,3 +1609,4 @@ order: 2
| itemHeight | `number` | `32` | 每个选项的高度,用于虚拟渲染 |
| virtualThreshold | `number` | `100` | 在选项数量超过多少时开启虚拟渲染 |
| valuesNoWrap | `boolean` | `false` | 默认情况下多选所有选项都会显示,通过这个可以最多显示一行,超出的部分变成 ... |
| clearValueOnSourceChange | `boolean` | | `source`从数据域取值时,数据域值变化后是否自动清空 |
8 changes: 7 additions & 1 deletion packages/amis-core/src/renderers/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ export interface FormOptionsControl extends FormBaseControl {
* 选项删除提示文字。
*/
deleteConfirmText?: string;

/**
* source从数据域取值时,数据域值变化后是否自动清空
*/
clearValueOnSourceChange?: boolean;
}

export interface OptionsBasicConfig extends FormItemBasicConfig {
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 13 additions & 1 deletion packages/amis-core/src/store/formItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,12 @@ export const FormItemStore = StoreNode.named('FormItemStore')
function loadOptionsFromDataScope(
source: string,
ctx: Record<string, any>,
onChange?: (value: any) => void
onChange?: (
value: any,
submitOnChange?: boolean,
changeImmediately?: boolean
) => void,
clearValue?: boolean
) {
let options: any[] = resolveVariableAndFilter(source, ctx, '| raw');

Expand All @@ -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;
}

Expand Down
Loading