Merge branch 'native_form' of https://gitee.com/dapppp/ruoyi-plus-vben5 into dev
This commit is contained in:
commit
ab003826a3
@ -3,6 +3,7 @@
|
||||
**FEATURES**
|
||||
|
||||
- 代码生成支持路径方式生成
|
||||
- 代码生成 支持选择表单生成类型(需要模板支持)
|
||||
|
||||
# 1.2.1
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@vben/web-antd",
|
||||
"version": "1.2.1",
|
||||
"version": "1.2.2",
|
||||
"homepage": "https://vben.pro",
|
||||
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
|
||||
"repository": {
|
||||
|
1
apps/web-antd/src/api/tool/gen/model.d.ts
vendored
1
apps/web-antd/src/api/tool/gen/model.d.ts
vendored
@ -177,6 +177,7 @@ export interface Info {
|
||||
// 树表需要添加此属性
|
||||
params?: any;
|
||||
popupComponent?: string;
|
||||
formComponent?: string;
|
||||
}
|
||||
|
||||
export interface GenInfo {
|
||||
|
@ -1,14 +1,23 @@
|
||||
<!--
|
||||
2025年03月08日重构为原生表单(反向重构??)
|
||||
该文件作为例子 使用原生表单而非useVbenForm
|
||||
-->
|
||||
<script setup lang="ts">
|
||||
import type { RuleObject } from 'ant-design-vue/es/form';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { DictEnum } from '@vben/constants';
|
||||
import { $t } from '@vben/locales';
|
||||
import { cloneDeep } from '@vben/utils';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import { noticeAdd, noticeInfo, noticeUpdate } from '#/api/system/notice';
|
||||
import { Form, FormItem, Input, RadioGroup } from 'ant-design-vue';
|
||||
import { pick } from 'lodash-es';
|
||||
|
||||
import { modalSchema } from './data';
|
||||
import { noticeAdd, noticeInfo, noticeUpdate } from '#/api/system/notice';
|
||||
import { Tinymce } from '#/components/tinymce';
|
||||
import { getDictOptions } from '#/utils/dict';
|
||||
|
||||
const emit = defineEmits<{ reload: [] }>();
|
||||
|
||||
@ -17,21 +26,59 @@ const title = computed(() => {
|
||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
||||
});
|
||||
|
||||
const [BasicForm, formApi] = useVbenForm({
|
||||
layout: 'vertical',
|
||||
commonConfig: {
|
||||
formItemClass: 'col-span-2',
|
||||
labelWidth: 100,
|
||||
},
|
||||
schema: modalSchema(),
|
||||
showDefaultActions: false,
|
||||
wrapperClass: 'grid-cols-2',
|
||||
/**
|
||||
* 定义表单数据类型
|
||||
*/
|
||||
interface FormData {
|
||||
noticeId?: number;
|
||||
noticeTitle?: string;
|
||||
status?: string;
|
||||
noticeType?: string;
|
||||
noticeContent?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义默认值 用于reset
|
||||
*/
|
||||
const defaultValues: FormData = {
|
||||
noticeId: undefined,
|
||||
noticeTitle: '',
|
||||
status: '0',
|
||||
noticeType: '1',
|
||||
noticeContent: '',
|
||||
};
|
||||
|
||||
/**
|
||||
* 表单数据ref
|
||||
*/
|
||||
const formData = ref(defaultValues);
|
||||
|
||||
type AntdFormRules<T> = Partial<Record<keyof T, RuleObject[]>> & {
|
||||
[key: string]: RuleObject[];
|
||||
};
|
||||
/**
|
||||
* 表单校验规则
|
||||
*/
|
||||
const formRules = ref<AntdFormRules<FormData>>({
|
||||
status: [{ required: true, message: $t('ui.formRules.selectRequired') }],
|
||||
noticeContent: [{ required: true, message: $t('ui.formRules.required') }],
|
||||
noticeType: [{ required: true, message: $t('ui.formRules.selectRequired') }],
|
||||
noticeTitle: [{ required: true, message: $t('ui.formRules.required') }],
|
||||
});
|
||||
|
||||
/**
|
||||
* useForm解构出表单方法
|
||||
*/
|
||||
const { validate, validateInfos, resetFields } = Form.useForm(
|
||||
formData,
|
||||
formRules,
|
||||
);
|
||||
|
||||
const [BasicModal, modalApi] = useVbenModal({
|
||||
class: 'w-[800px]',
|
||||
fullscreenButton: false,
|
||||
closeOnClickModal: false,
|
||||
onCancel: handleCancel,
|
||||
onClosed: handleCancel,
|
||||
onConfirm: handleConfirm,
|
||||
onOpenChange: async (isOpen) => {
|
||||
if (!isOpen) {
|
||||
@ -42,7 +89,9 @@ const [BasicModal, modalApi] = useVbenModal({
|
||||
isUpdate.value = !!id;
|
||||
if (isUpdate.value && id) {
|
||||
const record = await noticeInfo(id);
|
||||
await formApi.setValues(record);
|
||||
// 只赋值存在的字段
|
||||
const filterRecord = pick(record, Object.keys(defaultValues));
|
||||
formData.value = filterRecord;
|
||||
}
|
||||
modalApi.modalLoading(false);
|
||||
},
|
||||
@ -51,11 +100,9 @@ const [BasicModal, modalApi] = useVbenModal({
|
||||
async function handleConfirm() {
|
||||
try {
|
||||
modalApi.modalLoading(true);
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
const data = cloneDeep(await formApi.getValues());
|
||||
await validate();
|
||||
// 可能会做数据处理 使用cloneDeep深拷贝
|
||||
const data = cloneDeep(formData.value);
|
||||
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
||||
emit('reload');
|
||||
await handleCancel();
|
||||
@ -68,12 +115,41 @@ async function handleConfirm() {
|
||||
|
||||
async function handleCancel() {
|
||||
modalApi.close();
|
||||
await formApi.resetForm();
|
||||
formData.value = defaultValues;
|
||||
resetFields();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BasicModal :title="title" class="w-[800px]">
|
||||
<BasicForm />
|
||||
<BasicModal :title="title">
|
||||
<Form layout="vertical">
|
||||
<FormItem label="公告标题" v-bind="validateInfos.noticeTitle">
|
||||
<Input
|
||||
:placeholder="$t('ui.formRules.required')"
|
||||
v-model:value="formData.noticeTitle"
|
||||
/>
|
||||
</FormItem>
|
||||
<div class="grid sm:grid-cols-1 lg:grid-cols-2">
|
||||
<FormItem label="公告状态" v-bind="validateInfos.status">
|
||||
<RadioGroup
|
||||
button-style="solid"
|
||||
option-type="button"
|
||||
v-model:value="formData.status"
|
||||
:options="getDictOptions(DictEnum.SYS_NOTICE_STATUS)"
|
||||
/>
|
||||
</FormItem>
|
||||
<FormItem label="公告类型" v-bind="validateInfos.noticeType">
|
||||
<RadioGroup
|
||||
button-style="solid"
|
||||
option-type="button"
|
||||
v-model:value="formData.noticeType"
|
||||
:options="getDictOptions(DictEnum.SYS_NOTICE_TYPE)"
|
||||
/>
|
||||
</FormItem>
|
||||
</div>
|
||||
<FormItem label="公告内容" v-bind="validateInfos.noticeContent">
|
||||
<Tinymce v-model="formData.noticeContent" />
|
||||
</FormItem>
|
||||
</Form>
|
||||
</BasicModal>
|
||||
</template>
|
||||
|
@ -82,6 +82,7 @@ async function handleSave() {
|
||||
...requestData.params,
|
||||
parentMenuId: requestData.parentMenuId,
|
||||
popupComponent: requestData.popupComponent,
|
||||
formComponent: requestData.formComponent,
|
||||
};
|
||||
}
|
||||
// 保存
|
||||
|
@ -109,9 +109,12 @@ onMounted(async () => {
|
||||
await formApi.setValues(info);
|
||||
// 弹出框类型需要手动赋值
|
||||
if (info.options) {
|
||||
const popupComponent = JSON.parse(info.options)?.popupComponent;
|
||||
const { popupComponent, formComponent } = JSON.parse(info.options);
|
||||
if (popupComponent) {
|
||||
await formApi.setFieldValue('popupComponent', popupComponent);
|
||||
formApi.setFieldValue('popupComponent', popupComponent);
|
||||
}
|
||||
if (formComponent) {
|
||||
formApi.setFieldValue('formComponent', formComponent);
|
||||
}
|
||||
}
|
||||
await Promise.all([initTreeSelect(info.columns), initMenuSelect()]);
|
||||
|
@ -157,6 +157,21 @@ export const formSchema: FormSchemaGetter = () => [
|
||||
fieldName: 'popupComponent',
|
||||
label: '弹窗组件类型',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
buttonStyle: 'solid',
|
||||
options: [
|
||||
{ label: 'useVbenForm', value: 'useForm' },
|
||||
{ label: 'antd原生表单', value: 'native' },
|
||||
],
|
||||
optionType: 'button',
|
||||
},
|
||||
help: '自定义功能, 需要后端支持\n复杂(布局, 联动等)表单建议用antd原生表单',
|
||||
defaultValue: 'useForm',
|
||||
fieldName: 'formComponent',
|
||||
label: '生成表单类型',
|
||||
},
|
||||
{
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
|
Loading…
Reference in New Issue
Block a user