feat(form): add valueFormat for schema (#3873)
This commit is contained in:
parent
5d36b1a560
commit
0bc01d8528
|
|
@ -64,6 +64,7 @@
|
||||||
import { useDesign } from '@/hooks/web/useDesign';
|
import { useDesign } from '@/hooks/web/useDesign';
|
||||||
import { cloneDeep } from 'lodash-es';
|
import { cloneDeep } from 'lodash-es';
|
||||||
import { TableActionType } from '@/components/Table';
|
import { TableActionType } from '@/components/Table';
|
||||||
|
import { isFunction } from '@/utils/is';
|
||||||
|
|
||||||
defineOptions({ name: 'BasicForm' });
|
defineOptions({ name: 'BasicForm' });
|
||||||
|
|
||||||
|
|
@ -130,6 +131,9 @@
|
||||||
component,
|
component,
|
||||||
componentProps = {},
|
componentProps = {},
|
||||||
isHandleDateDefaultValue = true,
|
isHandleDateDefaultValue = true,
|
||||||
|
field,
|
||||||
|
isHandleDefaultValue = true,
|
||||||
|
valueFormat,
|
||||||
} = schema;
|
} = schema;
|
||||||
// handle date type
|
// handle date type
|
||||||
if (
|
if (
|
||||||
|
|
@ -161,6 +165,21 @@
|
||||||
schema.defaultValue = def;
|
schema.defaultValue = def;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle schema.valueFormat
|
||||||
|
if (
|
||||||
|
isHandleDefaultValue &&
|
||||||
|
defaultValue &&
|
||||||
|
component &&
|
||||||
|
isFunction(valueFormat)
|
||||||
|
) {
|
||||||
|
schema.defaultValue = valueFormat({
|
||||||
|
value: defaultValue,
|
||||||
|
schema,
|
||||||
|
model: formModel,
|
||||||
|
field,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (unref(getProps).showAdvancedButton) {
|
if (unref(getProps).showAdvancedButton) {
|
||||||
return schemas.filter(
|
return schemas.filter(
|
||||||
|
|
|
||||||
|
|
@ -277,6 +277,7 @@
|
||||||
field,
|
field,
|
||||||
changeEvent = 'change',
|
changeEvent = 'change',
|
||||||
valueField,
|
valueField,
|
||||||
|
valueFormat,
|
||||||
} = props.schema;
|
} = props.schema;
|
||||||
|
|
||||||
const isCheck = component && ['Switch', 'Checkbox'].includes(component);
|
const isCheck = component && ['Switch', 'Checkbox'].includes(component);
|
||||||
|
|
@ -288,7 +289,10 @@
|
||||||
const [e] = args;
|
const [e] = args;
|
||||||
|
|
||||||
const target = e ? e.target : null;
|
const target = e ? e.target : null;
|
||||||
const value = target ? (isCheck ? target.checked : target.value) : e;
|
let value = target ? (isCheck ? target.checked : target.value) : e;
|
||||||
|
if(isFunction(valueFormat)){
|
||||||
|
value = valueFormat({...unref(getValues),value});
|
||||||
|
}
|
||||||
props.setFormModel(field, value, props.schema);
|
props.setFormModel(field, value, props.schema);
|
||||||
|
|
||||||
if (propsData[eventKey]) {
|
if (propsData[eventKey]) {
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,9 @@ interface BaseFormSchema<T extends ComponentType = any> {
|
||||||
// 是否自动处理与时间相关组件的默认值
|
// 是否自动处理与时间相关组件的默认值
|
||||||
isHandleDateDefaultValue?: boolean;
|
isHandleDateDefaultValue?: boolean;
|
||||||
|
|
||||||
|
// 是否使用valueFormat自动处理默认值
|
||||||
|
isHandleDefaultValue?: boolean;
|
||||||
|
|
||||||
isAdvanced?: boolean;
|
isAdvanced?: boolean;
|
||||||
|
|
||||||
// Matching details components
|
// Matching details components
|
||||||
|
|
@ -232,6 +235,8 @@ interface BaseFormSchema<T extends ComponentType = any> {
|
||||||
dynamicReadonly?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);
|
dynamicReadonly?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);
|
||||||
|
|
||||||
dynamicRules?: (renderCallbackParams: RenderCallbackParams) => Rule[];
|
dynamicRules?: (renderCallbackParams: RenderCallbackParams) => Rule[];
|
||||||
|
|
||||||
|
valueFormat?: (arg: Partial<RenderCallbackParams> & { value: any }) => any;
|
||||||
}
|
}
|
||||||
export interface ComponentFormSchema<T extends ComponentType = any> extends BaseFormSchema<T> {
|
export interface ComponentFormSchema<T extends ComponentType = any> extends BaseFormSchema<T> {
|
||||||
// render component
|
// render component
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue