refactor: 通知公告 原生表单(非最终确定版)
This commit is contained in:
parent
e78f4e984d
commit
a66e13eca6
@ -1,14 +1,17 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref } from 'vue';
|
import { computed, reactive, ref } from 'vue';
|
||||||
|
|
||||||
import { useVbenModal } from '@vben/common-ui';
|
import { useVbenModal } from '@vben/common-ui';
|
||||||
|
import { DictEnum } from '@vben/constants';
|
||||||
import { $t } from '@vben/locales';
|
import { $t } from '@vben/locales';
|
||||||
import { cloneDeep } from '@vben/utils';
|
import { cloneDeep } from '@vben/utils';
|
||||||
|
|
||||||
import { useVbenForm } from '#/adapter/form';
|
import { Form, FormItem, Input, RadioGroup } from 'ant-design-vue';
|
||||||
import { noticeAdd, noticeInfo, noticeUpdate } from '#/api/system/notice';
|
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: [] }>();
|
const emit = defineEmits<{ reload: [] }>();
|
||||||
|
|
||||||
@ -17,21 +20,41 @@ const title = computed(() => {
|
|||||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
||||||
});
|
});
|
||||||
|
|
||||||
const [BasicForm, formApi] = useVbenForm({
|
interface FormData {
|
||||||
layout: 'vertical',
|
noticeId?: number;
|
||||||
commonConfig: {
|
noticeTitle?: string;
|
||||||
formItemClass: 'col-span-2',
|
status?: string;
|
||||||
labelWidth: 100,
|
noticeType?: string;
|
||||||
},
|
noticeContent?: string;
|
||||||
schema: modalSchema(),
|
}
|
||||||
showDefaultActions: false,
|
|
||||||
wrapperClass: 'grid-cols-2',
|
const defaultValues: FormData = {
|
||||||
|
noticeId: undefined,
|
||||||
|
noticeTitle: '',
|
||||||
|
status: '0',
|
||||||
|
noticeType: '1',
|
||||||
|
noticeContent: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const formData = ref(defaultValues);
|
||||||
|
|
||||||
|
const formRules = reactive<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') }],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { validate, validateInfos, resetFields } = Form.useForm(
|
||||||
|
formData,
|
||||||
|
formRules,
|
||||||
|
);
|
||||||
|
|
||||||
const [BasicModal, modalApi] = useVbenModal({
|
const [BasicModal, modalApi] = useVbenModal({
|
||||||
|
class: 'w-[800px]',
|
||||||
fullscreenButton: false,
|
fullscreenButton: false,
|
||||||
closeOnClickModal: false,
|
closeOnClickModal: false,
|
||||||
onCancel: handleCancel,
|
onClosed: handleCancel,
|
||||||
onConfirm: handleConfirm,
|
onConfirm: handleConfirm,
|
||||||
onOpenChange: async (isOpen) => {
|
onOpenChange: async (isOpen) => {
|
||||||
if (!isOpen) {
|
if (!isOpen) {
|
||||||
@ -42,7 +65,8 @@ const [BasicModal, modalApi] = useVbenModal({
|
|||||||
isUpdate.value = !!id;
|
isUpdate.value = !!id;
|
||||||
if (isUpdate.value && id) {
|
if (isUpdate.value && id) {
|
||||||
const record = await noticeInfo(id);
|
const record = await noticeInfo(id);
|
||||||
await formApi.setValues(record);
|
const filterRecord = pick(record, Object.keys(defaultValues));
|
||||||
|
formData.value = filterRecord;
|
||||||
}
|
}
|
||||||
modalApi.modalLoading(false);
|
modalApi.modalLoading(false);
|
||||||
},
|
},
|
||||||
@ -51,11 +75,9 @@ const [BasicModal, modalApi] = useVbenModal({
|
|||||||
async function handleConfirm() {
|
async function handleConfirm() {
|
||||||
try {
|
try {
|
||||||
modalApi.modalLoading(true);
|
modalApi.modalLoading(true);
|
||||||
const { valid } = await formApi.validate();
|
const test = await validate();
|
||||||
if (!valid) {
|
console.log(test);
|
||||||
return;
|
const data = cloneDeep(formData.value);
|
||||||
}
|
|
||||||
const data = cloneDeep(await formApi.getValues());
|
|
||||||
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
||||||
emit('reload');
|
emit('reload');
|
||||||
await handleCancel();
|
await handleCancel();
|
||||||
@ -68,12 +90,41 @@ async function handleConfirm() {
|
|||||||
|
|
||||||
async function handleCancel() {
|
async function handleCancel() {
|
||||||
modalApi.close();
|
modalApi.close();
|
||||||
await formApi.resetForm();
|
formData.value = defaultValues;
|
||||||
|
resetFields();
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<BasicModal :title="title" class="w-[800px]">
|
<BasicModal :title="title">
|
||||||
<BasicForm />
|
<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>
|
</BasicModal>
|
||||||
</template>
|
</template>
|
||||||
|
11
apps/web-antd/types/antd.d.ts
vendored
Normal file
11
apps/web-antd/types/antd.d.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import type { RuleObject } from 'ant-design-vue/es/form';
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
type AntdFormRules<T> = {
|
||||||
|
[key: string]: RuleObject[];
|
||||||
|
} & {
|
||||||
|
[K in keyof T]?: RuleObject[];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export {};
|
Loading…
Reference in New Issue
Block a user