2024-09-10 13:48:51 +00:00
|
|
|
|
import type {
|
|
|
|
|
BaseFormComponentType,
|
|
|
|
|
VbenFormSchema as FormSchema,
|
|
|
|
|
VbenFormProps,
|
|
|
|
|
} from '@vben/common-ui';
|
|
|
|
|
|
2024-10-03 06:22:18 +00:00
|
|
|
|
import type { Component, SetupContext } from 'vue';
|
2024-09-10 13:48:51 +00:00
|
|
|
|
import { h } from 'vue';
|
|
|
|
|
|
|
|
|
|
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
NButton,
|
|
|
|
|
NCheckbox,
|
|
|
|
|
NCheckboxGroup,
|
|
|
|
|
NDatePicker,
|
|
|
|
|
NDivider,
|
|
|
|
|
NInput,
|
|
|
|
|
NInputNumber,
|
|
|
|
|
NRadioGroup,
|
|
|
|
|
NSelect,
|
|
|
|
|
NSpace,
|
|
|
|
|
NSwitch,
|
|
|
|
|
NTimePicker,
|
|
|
|
|
NTreeSelect,
|
|
|
|
|
NUpload,
|
|
|
|
|
} from 'naive-ui';
|
|
|
|
|
// 业务表单组件适配
|
|
|
|
|
|
|
|
|
|
export type FormComponentType =
|
|
|
|
|
| 'Checkbox'
|
|
|
|
|
| 'CheckboxGroup'
|
|
|
|
|
| 'DatePicker'
|
|
|
|
|
| 'Divider'
|
|
|
|
|
| 'Input'
|
|
|
|
|
| 'InputNumber'
|
|
|
|
|
| 'RadioGroup'
|
|
|
|
|
| 'Select'
|
|
|
|
|
| 'Space'
|
|
|
|
|
| 'Switch'
|
|
|
|
|
| 'TimePicker'
|
|
|
|
|
| 'TreeSelect'
|
|
|
|
|
| 'Upload'
|
|
|
|
|
| BaseFormComponentType;
|
|
|
|
|
|
2024-10-03 06:22:18 +00: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}`);
|
|
|
|
|
return h(component, { ...props, ...attrs, placeholder }, slots);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-10 13:48:51 +00:00
|
|
|
|
// 初始化表单组件,并注册到form组件内部
|
|
|
|
|
setupVbenForm<FormComponentType>({
|
|
|
|
|
components: {
|
|
|
|
|
Checkbox: NCheckbox,
|
|
|
|
|
CheckboxGroup: NCheckboxGroup,
|
|
|
|
|
DatePicker: NDatePicker,
|
|
|
|
|
// 自定义默认的重置按钮
|
|
|
|
|
DefaultResetActionButton: (props, { attrs, slots }) => {
|
|
|
|
|
return h(NButton, { ...props, attrs, text: false, type: 'info' }, slots);
|
|
|
|
|
},
|
|
|
|
|
// 自定义默认的提交按钮
|
|
|
|
|
DefaultSubmitActionButton: (props, { attrs, slots }) => {
|
|
|
|
|
return h(
|
|
|
|
|
NButton,
|
|
|
|
|
{ ...props, attrs, text: false, type: 'primary' },
|
|
|
|
|
slots,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
Divider: NDivider,
|
2024-10-03 06:22:18 +00:00
|
|
|
|
Input: withDefaultPlaceholder(NInput, 'input'),
|
|
|
|
|
InputNumber: withDefaultPlaceholder(NInputNumber, 'input'),
|
2024-09-10 13:48:51 +00:00
|
|
|
|
RadioGroup: NRadioGroup,
|
2024-10-03 06:22:18 +00:00
|
|
|
|
Select: withDefaultPlaceholder(NSelect, 'select'),
|
2024-09-10 13:48:51 +00:00
|
|
|
|
Space: NSpace,
|
|
|
|
|
Switch: NSwitch,
|
|
|
|
|
TimePicker: NTimePicker,
|
2024-10-03 06:22:18 +00:00
|
|
|
|
TreeSelect: withDefaultPlaceholder(NTreeSelect, 'select'),
|
2024-09-10 13:48:51 +00:00
|
|
|
|
Upload: NUpload,
|
|
|
|
|
},
|
|
|
|
|
config: {
|
2024-10-05 09:09:42 +00:00
|
|
|
|
// naive-ui组件不接受onChang事件,所以需要禁用
|
2024-10-03 06:22:18 +00:00
|
|
|
|
disabledOnChangeListener: true,
|
2024-10-05 09:09:42 +00:00
|
|
|
|
// naive-ui组件的空值为null,不能是undefined,否则重置表单时不生效
|
|
|
|
|
emptyStateValue: null,
|
2024-09-10 13:48:51 +00:00
|
|
|
|
baseModelPropName: 'value',
|
|
|
|
|
modelPropNameMap: {
|
|
|
|
|
Checkbox: 'checked',
|
|
|
|
|
Radio: 'checked',
|
|
|
|
|
Upload: 'fileList',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
defineRules: {
|
|
|
|
|
required: (value, _params, ctx) => {
|
2024-09-22 06:16:06 +00:00
|
|
|
|
if (value === undefined || value === null || value.length === 0) {
|
2024-09-10 13:48:51 +00:00
|
|
|
|
return $t('formRules.required', [ctx.label]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2024-09-20 01:46:03 +00:00
|
|
|
|
selectRequired: (value, _params, ctx) => {
|
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
|
return $t('formRules.selectRequired', [ctx.label]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2024-09-10 13:48:51 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const useVbenForm = useForm<FormComponentType>;
|
|
|
|
|
|
|
|
|
|
export { useVbenForm, z };
|
|
|
|
|
|
|
|
|
|
export type VbenFormSchema = FormSchema<FormComponentType>;
|
|
|
|
|
export type { VbenFormProps };
|