fix: fixed the form component componentProps using the function that did not meet expectations (#4426)

pull/48/MERGE
Vben 2024-09-18 23:01:40 +08:00 committed by GitHub
parent 834cb4c470
commit 0c73cf8d3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 48 deletions

View File

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import type { ZodType } from 'zod'; import type { ZodType } from 'zod';
import type { FormSchema } from '../types'; import type { FormSchema, MaybeComponentProps } from '../types';
import { computed } from 'vue'; import { computed } from 'vue';
@ -26,6 +26,7 @@ import { isEventObjectLike } from './helper';
interface Props extends FormSchema {} interface Props extends FormSchema {}
const { const {
commonComponentProps,
component, component,
componentProps, componentProps,
dependencies, dependencies,
@ -38,7 +39,11 @@ const {
labelWidth, labelWidth,
renderComponentContent, renderComponentContent,
rules, rules,
} = defineProps<Props>(); } = defineProps<
{
commonComponentProps: MaybeComponentProps;
} & Props
>();
const { componentBindEventMap, componentMap, isVertical } = useFormContext(); const { componentBindEventMap, componentMap, isVertical } = useFormContext();
const formRenderProps = injectRenderFormProps(); const formRenderProps = injectRenderFormProps();
@ -133,6 +138,7 @@ const computedProps = computed(() => {
: componentProps; : componentProps;
return { return {
...commonComponentProps,
...finalComponentProps, ...finalComponentProps,
...dynamicComponentProps.value, ...dynamicComponentProps.value,
}; };

View File

@ -1,5 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
import type { GenericObject } from 'vee-validate';
import type { ZodTypeAny } from 'zod'; import type { ZodTypeAny } from 'zod';
import type { FormRenderProps, FormSchema, FormShape } from '../types'; import type { FormRenderProps, FormSchema, FormShape } from '../types';
@ -7,7 +6,9 @@ import type { FormRenderProps, FormSchema, FormShape } from '../types';
import { computed } from 'vue'; import { computed } from 'vue';
import { Form } from '@vben-core/shadcn-ui'; import { Form } from '@vben-core/shadcn-ui';
import { cn, isString, merge } from '@vben-core/shared/utils'; import { cn, isString } from '@vben-core/shared/utils';
import { type GenericObject } from 'vee-validate';
import { provideFormRenderProps } from './context'; import { provideFormRenderProps } from './context';
import { useExpandable } from './expandable'; import { useExpandable } from './expandable';
@ -70,51 +71,54 @@ const formCollapsed = computed(() => {
return props.collapsed && isCalculated.value; return props.collapsed && isCalculated.value;
}); });
const computedSchema = computed((): FormSchema[] => { const computedSchema = computed(
const { (): ({ commonComponentProps: Record<string, any> } & FormSchema)[] => {
componentProps = {}, const {
controlClass = '', componentProps = {},
disabled, controlClass = '',
formFieldProps = {},
formItemClass = '',
hideLabel = false,
hideRequiredMark = false,
labelClass = '',
labelWidth = 100,
wrapperClass = '',
} = props.commonConfig;
return (props.schema || []).map((schema, index): FormSchema => {
const keepIndex = keepFormItemIndex.value;
const hidden =
// & &
props.showCollapseButton && !!formCollapsed.value && keepIndex
? keepIndex <= index
: false;
return {
disabled, disabled,
hideLabel, formFieldProps = {},
hideRequiredMark, formItemClass = '',
labelWidth, hideLabel = false,
wrapperClass, hideRequiredMark = false,
...schema, labelClass = '',
componentProps: merge({}, schema.componentProps, componentProps), labelWidth = 100,
controlClass: cn(controlClass, schema.controlClass), wrapperClass = '',
formFieldProps: { } = props.commonConfig;
...formFieldProps, return (props.schema || []).map((schema, index) => {
...schema.formFieldProps, const keepIndex = keepFormItemIndex.value;
},
formItemClass: cn( const hidden =
'flex-shrink-0', // & &
{ hidden }, props.showCollapseButton && !!formCollapsed.value && keepIndex
formItemClass, ? keepIndex <= index
schema.formItemClass, : false;
),
labelClass: cn(labelClass, schema.labelClass), return {
}; disabled,
}); hideLabel,
}); hideRequiredMark,
labelWidth,
wrapperClass,
...schema,
commonComponentProps: componentProps,
componentProps: schema.componentProps,
controlClass: cn(controlClass, schema.controlClass),
formFieldProps: {
...formFieldProps,
...schema.formFieldProps,
},
formItemClass: cn(
'flex-shrink-0',
{ hidden },
formItemClass,
schema.formItemClass,
),
labelClass: cn(labelClass, schema.labelClass),
};
});
},
);
</script> </script>
<template> <template>