fix: repair the unexpected form default value (#5567)
* fix: Fix inconsistent spacing around search form (issue #5429) * fix: repair the unexpected default value in validated form.(issue #5451) * Update packages/@core/ui-kit/form-ui/src/use-form-context.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: Jin Mao <50581550+jinmao88@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
parent
97894a940e
commit
af186f878d
@ -10,7 +10,7 @@ import { createContext } from '@vben-core/shadcn-ui';
|
|||||||
import { isString, mergeWithArrayOverride, set } from '@vben-core/shared/utils';
|
import { isString, mergeWithArrayOverride, set } from '@vben-core/shared/utils';
|
||||||
|
|
||||||
import { useForm } from 'vee-validate';
|
import { useForm } from 'vee-validate';
|
||||||
import { object } from 'zod';
|
import { object, ZodIntersection, ZodNumber, ZodObject, ZodString } from 'zod';
|
||||||
import { getDefaultsForSchema } from 'zod-defaults';
|
import { getDefaultsForSchema } from 'zod-defaults';
|
||||||
|
|
||||||
type ExtendFormProps = VbenFormProps & { formApi: ExtendedFormApi };
|
type ExtendFormProps = VbenFormProps & { formApi: ExtendedFormApi };
|
||||||
@ -52,7 +52,12 @@ export function useFormInitial(
|
|||||||
if (Reflect.has(item, 'defaultValue')) {
|
if (Reflect.has(item, 'defaultValue')) {
|
||||||
set(initialValues, item.fieldName, item.defaultValue);
|
set(initialValues, item.fieldName, item.defaultValue);
|
||||||
} else if (item.rules && !isString(item.rules)) {
|
} else if (item.rules && !isString(item.rules)) {
|
||||||
|
// 检查规则是否适合提取默认值
|
||||||
|
const customDefaultValue = getCustomDefaultValue(item.rules);
|
||||||
zodObject[item.fieldName] = item.rules;
|
zodObject[item.fieldName] = item.rules;
|
||||||
|
if (customDefaultValue !== undefined) {
|
||||||
|
initialValues[item.fieldName] = customDefaultValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -64,6 +69,38 @@ export function useFormInitial(
|
|||||||
}
|
}
|
||||||
return mergeWithArrayOverride(initialValues, zodDefaults);
|
return mergeWithArrayOverride(initialValues, zodDefaults);
|
||||||
}
|
}
|
||||||
|
// 自定义默认值提取逻辑
|
||||||
|
function getCustomDefaultValue(rule: any): any {
|
||||||
|
if (rule instanceof ZodString) {
|
||||||
|
return ''; // 默认为空字符串
|
||||||
|
} else if (rule instanceof ZodNumber) {
|
||||||
|
return null; // 默认为 null(避免显示 0)
|
||||||
|
} else if (rule instanceof ZodObject) {
|
||||||
|
// 递归提取嵌套对象的默认值
|
||||||
|
const defaultValues: Record<string, any> = {};
|
||||||
|
for (const [key, valueSchema] of Object.entries(rule.shape)) {
|
||||||
|
defaultValues[key] = getCustomDefaultValue(valueSchema);
|
||||||
|
}
|
||||||
|
return defaultValues;
|
||||||
|
} else if (rule instanceof ZodIntersection) {
|
||||||
|
// 对于交集类型,从schema 提取默认值
|
||||||
|
const leftDefaultValue = getCustomDefaultValue(rule._def.left);
|
||||||
|
const rightDefaultValue = getCustomDefaultValue(rule._def.right);
|
||||||
|
|
||||||
|
// 如果左右两边都能提取默认值,合并它们
|
||||||
|
if (
|
||||||
|
typeof leftDefaultValue === 'object' &&
|
||||||
|
typeof rightDefaultValue === 'object'
|
||||||
|
) {
|
||||||
|
return { ...leftDefaultValue, ...rightDefaultValue };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 否则优先使用左边的默认值
|
||||||
|
return leftDefaultValue ?? rightDefaultValue;
|
||||||
|
} else {
|
||||||
|
return undefined; // 其他类型不提供默认值
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
delegatedSlots,
|
delegatedSlots,
|
||||||
|
Loading…
Reference in New Issue
Block a user