2024-09-10 21:48:51 +08:00
|
|
|
|
import type {
|
|
|
|
|
BaseFormComponentType,
|
|
|
|
|
VbenFormSchema as FormSchema,
|
|
|
|
|
VbenFormProps,
|
|
|
|
|
} from '@vben/common-ui';
|
|
|
|
|
|
2024-10-03 14:22:18 +08:00
|
|
|
|
import type { Component, SetupContext } from 'vue';
|
|
|
|
|
import { h } from 'vue';
|
2024-09-10 21:48:51 +08:00
|
|
|
|
|
|
|
|
|
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
AutoComplete,
|
|
|
|
|
Button,
|
|
|
|
|
Checkbox,
|
|
|
|
|
CheckboxGroup,
|
|
|
|
|
DatePicker,
|
|
|
|
|
Divider,
|
|
|
|
|
Input,
|
|
|
|
|
InputNumber,
|
|
|
|
|
InputPassword,
|
|
|
|
|
Mentions,
|
|
|
|
|
Radio,
|
|
|
|
|
RadioGroup,
|
|
|
|
|
RangePicker,
|
|
|
|
|
Rate,
|
|
|
|
|
Select,
|
|
|
|
|
Space,
|
|
|
|
|
Switch,
|
2024-09-22 14:16:06 +08:00
|
|
|
|
Textarea,
|
2024-09-10 21:48:51 +08:00
|
|
|
|
TimePicker,
|
|
|
|
|
TreeSelect,
|
|
|
|
|
Upload,
|
|
|
|
|
} from 'ant-design-vue';
|
|
|
|
|
|
2024-09-22 14:16:06 +08:00
|
|
|
|
// 这里需要自行根据业务组件库进行适配,需要用到的组件都需要在这里类型说明
|
2024-09-10 21:48:51 +08:00
|
|
|
|
export type FormComponentType =
|
|
|
|
|
| 'AutoComplete'
|
|
|
|
|
| 'Checkbox'
|
|
|
|
|
| 'CheckboxGroup'
|
|
|
|
|
| 'DatePicker'
|
|
|
|
|
| 'Divider'
|
|
|
|
|
| 'Input'
|
|
|
|
|
| 'InputNumber'
|
|
|
|
|
| 'InputPassword'
|
|
|
|
|
| 'Mentions'
|
|
|
|
|
| 'Radio'
|
|
|
|
|
| 'RadioGroup'
|
|
|
|
|
| 'RangePicker'
|
|
|
|
|
| 'Rate'
|
|
|
|
|
| 'Select'
|
|
|
|
|
| 'Space'
|
|
|
|
|
| 'Switch'
|
2024-09-22 14:16:06 +08:00
|
|
|
|
| 'Textarea'
|
2024-09-10 21:48:51 +08:00
|
|
|
|
| 'TimePicker'
|
|
|
|
|
| 'TreeSelect'
|
|
|
|
|
| 'Upload'
|
|
|
|
|
| BaseFormComponentType;
|
|
|
|
|
|
2024-10-03 13:04:19 +08:00
|
|
|
|
const withDefaultPlaceholder = <T extends Component>(
|
|
|
|
|
component: T,
|
|
|
|
|
type: 'input' | 'select',
|
|
|
|
|
) => {
|
|
|
|
|
return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
|
|
|
|
|
const placeholder = props?.placeholder || $t(`placeholder.${type}`);
|
2024-10-03 14:22:18 +08:00
|
|
|
|
return h(component, { ...props, ...attrs, placeholder }, slots);
|
2024-10-03 13:04:19 +08:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-10 21:48:51 +08:00
|
|
|
|
// 初始化表单组件,并注册到form组件内部
|
|
|
|
|
setupVbenForm<FormComponentType>({
|
|
|
|
|
components: {
|
|
|
|
|
AutoComplete,
|
|
|
|
|
Checkbox,
|
|
|
|
|
CheckboxGroup,
|
|
|
|
|
DatePicker,
|
|
|
|
|
// 自定义默认的重置按钮
|
|
|
|
|
DefaultResetActionButton: (props, { attrs, slots }) => {
|
|
|
|
|
return h(Button, { ...props, attrs, type: 'default' }, slots);
|
|
|
|
|
},
|
|
|
|
|
// 自定义默认的提交按钮
|
|
|
|
|
DefaultSubmitActionButton: (props, { attrs, slots }) => {
|
|
|
|
|
return h(Button, { ...props, attrs, type: 'primary' }, slots);
|
|
|
|
|
},
|
|
|
|
|
Divider,
|
2024-10-03 13:04:19 +08:00
|
|
|
|
Input: withDefaultPlaceholder(Input, 'input'),
|
|
|
|
|
InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
|
|
|
|
|
InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
|
|
|
|
|
Mentions: withDefaultPlaceholder(Mentions, 'input'),
|
2024-09-10 21:48:51 +08:00
|
|
|
|
Radio,
|
|
|
|
|
RadioGroup,
|
|
|
|
|
RangePicker,
|
|
|
|
|
Rate,
|
2024-10-03 13:04:19 +08:00
|
|
|
|
Select: withDefaultPlaceholder(Select, 'select'),
|
2024-09-10 21:48:51 +08:00
|
|
|
|
Space,
|
|
|
|
|
Switch,
|
2024-10-03 13:04:19 +08:00
|
|
|
|
Textarea: withDefaultPlaceholder(Textarea, 'input'),
|
2024-09-10 21:48:51 +08:00
|
|
|
|
TimePicker,
|
2024-10-03 13:04:19 +08:00
|
|
|
|
TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
|
2024-09-10 21:48:51 +08:00
|
|
|
|
Upload,
|
|
|
|
|
},
|
|
|
|
|
config: {
|
2024-09-22 14:16:06 +08:00
|
|
|
|
// ant design vue组件库默认都是 v-model:value
|
2024-09-10 21:48:51 +08:00
|
|
|
|
baseModelPropName: 'value',
|
2024-09-22 14:16:06 +08:00
|
|
|
|
|
|
|
|
|
// 一些组件是 v-model:checked 或者 v-model:fileList
|
2024-09-10 21:48:51 +08:00
|
|
|
|
modelPropNameMap: {
|
|
|
|
|
Checkbox: 'checked',
|
|
|
|
|
Radio: 'checked',
|
|
|
|
|
Switch: 'checked',
|
|
|
|
|
Upload: 'fileList',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
defineRules: {
|
2024-09-22 14:16:06 +08:00
|
|
|
|
// 输入项目必填国际化适配
|
2024-09-10 21:48:51 +08:00
|
|
|
|
required: (value, _params, ctx) => {
|
2024-09-22 14:16:06 +08:00
|
|
|
|
if (value === undefined || value === null || value.length === 0) {
|
2024-09-10 21:48:51 +08:00
|
|
|
|
return $t('formRules.required', [ctx.label]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2024-09-22 14:16:06 +08:00
|
|
|
|
// 选择项目必填国际化适配
|
2024-09-20 09:46:03 +08:00
|
|
|
|
selectRequired: (value, _params, ctx) => {
|
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
|
return $t('formRules.selectRequired', [ctx.label]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2024-09-10 21:48:51 +08:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const useVbenForm = useForm<FormComponentType>;
|
|
|
|
|
|
|
|
|
|
export { useVbenForm, z };
|
|
|
|
|
|
|
|
|
|
export type VbenFormSchema = FormSchema<FormComponentType>;
|
|
|
|
|
export type { VbenFormProps };
|