refactor: 通知公告 原生表单(非最终确定版)
This commit is contained in:
parent
e78f4e984d
commit
a66e13eca6
@ -1,14 +1,17 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
import { computed, reactive, 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 +20,41 @@ 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;
|
||||
}
|
||||
|
||||
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({
|
||||
class: 'w-[800px]',
|
||||
fullscreenButton: false,
|
||||
closeOnClickModal: false,
|
||||
onCancel: handleCancel,
|
||||
onClosed: handleCancel,
|
||||
onConfirm: handleConfirm,
|
||||
onOpenChange: async (isOpen) => {
|
||||
if (!isOpen) {
|
||||
@ -42,7 +65,8 @@ 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 +75,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());
|
||||
const test = await validate();
|
||||
console.log(test);
|
||||
const data = cloneDeep(formData.value);
|
||||
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
||||
emit('reload');
|
||||
await handleCancel();
|
||||
@ -68,12 +90,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>
|
||||
|
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