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

feat(form): add valueFormat for schema #3873

Merged
merged 1 commit into from
May 31, 2024
Merged
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
19 changes: 19 additions & 0 deletions src/components/Form/src/BasicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import { useDesign } from '@/hooks/web/useDesign';
import { cloneDeep } from 'lodash-es';
import { TableActionType } from '@/components/Table';
import { isFunction } from '@/utils/is';

defineOptions({ name: 'BasicForm' });

Expand Down Expand Up @@ -130,6 +131,9 @@
component,
componentProps = {},
isHandleDateDefaultValue = true,
field,
isHandleDefaultValue = true,
valueFormat,
} = schema;
// handle date type
if (
Expand Down Expand Up @@ -161,6 +165,21 @@
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) {
return schemas.filter(
Expand Down
8 changes: 6 additions & 2 deletions src/components/Form/src/components/FormItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
field,
changeEvent = 'change',
valueField,
valueFormat,
} = props.schema;

const isCheck = component && ['Switch', 'Checkbox'].includes(component);
Expand All @@ -286,9 +287,12 @@
const on = {
[eventKey]: (...args: Nullable<Recordable<any>>[]) => {
const [e] = args;

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);

if (propsData[eventKey]) {
Expand Down
5 changes: 5 additions & 0 deletions src/components/Form/src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ interface BaseFormSchema<T extends ComponentType = any> {
// 是否自动处理与时间相关组件的默认值
isHandleDateDefaultValue?: boolean;

// 是否使用valueFormat自动处理默认值
isHandleDefaultValue?: boolean;

isAdvanced?: boolean;

// Matching details components
Expand Down Expand Up @@ -232,6 +235,8 @@ interface BaseFormSchema<T extends ComponentType = any> {
dynamicReadonly?: boolean | ((renderCallbackParams: RenderCallbackParams) => boolean);

dynamicRules?: (renderCallbackParams: RenderCallbackParams) => Rule[];

valueFormat?: (arg: Partial<RenderCallbackParams> & { value: any }) => any;
}
export interface ComponentFormSchema<T extends ComponentType = any> extends BaseFormSchema<T> {
// render component
Expand Down
Loading