fix: form fieldMappingTime improve and modelPropName support (#5335)

* 表单的fieldMappingTime支持将格式化掩码设为null以便原值映射,这样可以支持非日期时间类型的组件;
* 表单增加modelPropName设置组件的双向绑定属性名,用于支持未提前注册的双向绑定属性为非默认名称的组件。
* 增加一些经常会有人提到的组合字段演示,
This commit is contained in:
Netfan
2025-01-09 22:49:28 +08:00
committed by GitHub
parent 99c7fd72f8
commit 516d0b8dc8
8 changed files with 115 additions and 21 deletions

View File

@@ -368,17 +368,21 @@ export class FormApi {
}
const [startTime, endTime] = values[field];
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
? format
: [format, format];
values[startTimeKey] = startTime
? formatDate(startTime, startTimeFormat)
: undefined;
values[endTimeKey] = endTime
? formatDate(endTime, endTimeFormat)
: undefined;
if (format === null) {
values[startTimeKey] = startTime;
values[endTimeKey] = endTime;
} else {
const [startTimeFormat, endTimeFormat] = Array.isArray(format)
? format
: [format, format];
values[startTimeKey] = startTime
? formatDate(startTime, startTimeFormat)
: undefined;
values[endTimeKey] = endTime
? formatDate(endTime, endTimeFormat)
: undefined;
}
// delete values[field];
Reflect.deleteProperty(values, field);
},

View File

@@ -41,6 +41,7 @@ const {
label,
labelClass,
labelWidth,
modelPropName,
renderComponentContent,
rules,
} = defineProps<
@@ -202,9 +203,9 @@ function fieldBindEvent(slotProps: Record<string, any>) {
const modelValue = slotProps.componentField.modelValue;
const handler = slotProps.componentField['onUpdate:modelValue'];
const bindEventField = isString(component)
? componentBindEventMap.value?.[component]
: null;
const bindEventField =
modelPropName ||
(isString(component) ? componentBindEventMap.value?.[component] : null);
let value = modelValue;
// antd design 的一些组件会传递一个 event 对象

View File

@@ -98,6 +98,7 @@ const computedSchema = computed(
hideRequiredMark = false,
labelClass = '',
labelWidth = 100,
modelPropName = '',
wrapperClass = '',
} = mergeWithArrayOverride(props.commonConfig, props.globalCommonConfig);
return (props.schema || []).map((schema, index) => {
@@ -118,6 +119,7 @@ const computedSchema = computed(
hideLabel,
hideRequiredMark,
labelWidth,
modelPropName,
wrapperClass,
...schema,
commonComponentProps: componentProps,

View File

@@ -4,7 +4,7 @@ import type { ZodTypeAny } from 'zod';
import type { Component, HtmlHTMLAttributes, Ref } from 'vue';
import type { VbenButtonProps } from '@vben-core/shadcn-ui';
import type { ClassType } from '@vben-core/typings';
import type { ClassType, Nullable } from '@vben-core/typings';
import type { FormApi } from './form-api';
@@ -197,6 +197,11 @@ export interface FormCommonConfig {
* 所有表单项的label宽度
*/
labelWidth?: number;
/**
* 所有表单项的model属性名
* @default "modelValue"
*/
modelPropName?: string;
/**
* 所有表单项的wrapper样式
*/
@@ -219,7 +224,7 @@ export type HandleResetFn = (
export type FieldMappingTime = [
string,
[string, string],
([string, string] | string)?,
([string, string] | Nullable<string>)?,
][];
export interface FormSchema<
@@ -330,7 +335,7 @@ export interface VbenFormProps<
*/
actionWrapperClass?: ClassType;
/**
* 表单字段映射成时间格式
* 表单字段映射
*/
fieldMappingTime?: FieldMappingTime;
/**