提交 | 用户 | 时间
|
e7c126
|
1 |
<script lang="ts" setup> |
H |
2 |
import { ref, unref } from 'vue' |
|
3 |
import { createFormSchema, updateFormSchema } from './${classNameVar}.data' |
|
4 |
import { useI18n } from '@/hooks/web/useI18n' |
|
5 |
import { useMessage } from '@/hooks/web/useMessage' |
|
6 |
import { BasicForm, useForm } from '@/components/Form' |
|
7 |
import { BasicModal, useModalInner } from '@/components/Modal' |
|
8 |
import { create${simpleClassName}, get${simpleClassName}, update${simpleClassName} } from '@/api/${table.moduleName}/${table.businessName}' |
|
9 |
|
|
10 |
defineOptions({ name: '${table.className}Modal' }) |
|
11 |
|
|
12 |
const emit = defineEmits(['success', 'register']) |
|
13 |
|
|
14 |
const { t } = useI18n() |
|
15 |
const { createMessage } = useMessage() |
|
16 |
const isUpdate = ref(true) |
|
17 |
|
|
18 |
const [registerForm, { setFieldsValue, resetFields, resetSchema, validate }] = useForm({ |
|
19 |
labelWidth: 120, |
|
20 |
baseColProps: { span: 24 }, |
|
21 |
schemas: createFormSchema, |
|
22 |
showActionButtonGroup: false, |
|
23 |
actionColOptions: { span: 23 }, |
|
24 |
}) |
|
25 |
|
|
26 |
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => { |
|
27 |
resetFields() |
|
28 |
setModalProps({ confirmLoading: false }) |
|
29 |
isUpdate.value = !!data?.isUpdate |
|
30 |
if (unref(isUpdate)) { |
|
31 |
resetSchema(updateFormSchema) |
|
32 |
const res = await get${simpleClassName}(data.record.id) |
|
33 |
setFieldsValue({ ...res }) |
|
34 |
} |
|
35 |
}) |
|
36 |
|
|
37 |
async function handleSubmit() { |
|
38 |
try { |
|
39 |
const values = await validate() |
|
40 |
setModalProps({ confirmLoading: true }) |
|
41 |
if (unref(isUpdate)) |
|
42 |
await update${simpleClassName}(values) |
|
43 |
else |
|
44 |
await create${simpleClassName}(values) |
|
45 |
|
|
46 |
closeModal() |
|
47 |
emit('success') |
|
48 |
createMessage.success(t('common.saveSuccessText')) |
|
49 |
} finally { |
|
50 |
setModalProps({ confirmLoading: false }) |
|
51 |
} |
|
52 |
} |
|
53 |
</script> |
|
54 |
<template> |
|
55 |
<BasicModal v-bind="$attrs" :title="isUpdate ? t('action.edit') : t('action.create')" @register="registerModal" @ok="handleSubmit"> |
|
56 |
<BasicForm @register="registerForm" /> |
|
57 |
</BasicModal> |
|
58 |
</template> |