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(BasicForm): 修复表单项通过ifShow控制隐藏后其表单值还存在 #3836

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 23 additions & 1 deletion src/components/Form/src/components/FormItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
NO_AUTO_LINK_COMPONENTS,
setComponentRuleType,
} from '../helper';
import { cloneDeep, upperFirst } from 'lodash-es';
import { cloneDeep, isNil, upperFirst } from 'lodash-es';
import { useItemLabelWidth } from '../hooks/useLabelWidth';
import { useI18n } from '@/hooks/web/useI18n';

Expand Down Expand Up @@ -78,6 +78,28 @@
const getValues = computed(() => {
const { allDefaultValues, formModel, schema } = props;
const { mergeDynamicData } = props.formProps;
if (
(isFunction(schema.ifShow) &&
!schema.ifShow({
values: {
...mergeDynamicData,
...allDefaultValues,
...formModel,
} as Recordable<any>,
schema,
model: formModel,
field: schema.field,
})) ||
(isBoolean(schema.ifShow) && schema.ifShow === false)
) {
formModel[schema.field] = undefined;
} else {
formModel[schema.field] = !isNil(formModel[schema.field])
? formModel[schema.field]
: !isNil(schema.defaultValue)
? schema.defaultValue
: undefined;
}
return {
field: schema.field,
model: formModel,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface FormActionType {
validateFields: (nameList?: NamePath[]) => Promise<any>;
validate: <T = Recordable>(nameList?: NamePath[] | false) => Promise<T>;
scrollToField: (name: NamePath, options?: ScrollOptions) => Promise<void>;
resetDefaultField:(name?: NamePath[]) => void;
resetDefaultField: (name?: NamePath[]) => void;
}

export type RegisterFn = (formInstance: FormActionType) => void;
Expand Down
39 changes: 38 additions & 1 deletion src/views/demo/form/DynamicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<a-button @click="deleteField" class="mr-2"> 删除字段11 </a-button>
</div>
<CollapseContainer title="动态表单示例,动态根据表单内其他值改变">
<BasicForm @register="register" />
<BasicForm @register="register" @submit="handleSubmit" />
</CollapseContainer>

<CollapseContainer class="mt-5" title="componentProps动态改变">
Expand Down Expand Up @@ -87,6 +87,39 @@
span: 8,
},
},
{
field: 'field10',
component: 'Input',
label: '字段2',
colProps: {
span: 8,
},
ifShow: false,
},
{
field: 'field12',
component: 'CheckboxGroup',
label: '字段12',
colProps: {
span: 8,
},
defaultValue: ['1', '2'],
componentProps: {
options: [
{
label: '选项1',
value: '1',
},
{
label: '选项2',
value: '2',
},
],
},
ifShow: ({ values }) => {
return !!values.field6;
},
},
{
field: 'field5',
component: 'Switch',
Expand Down Expand Up @@ -226,4 +259,8 @@
function deleteField() {
removeSchemaByField('field11');
}

function handleSubmit(values) {
console.log(values);
}
</script>
Loading