refactor: 字典useBeforeCloseDiff

This commit is contained in:
dap
2025-04-08 10:40:32 +08:00
parent 98f658d46f
commit 5510b6dea4
3 changed files with 39 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ import {
dictDetailInfo,
} from '#/api/system/dict/dict-data';
import { tagTypes } from '#/components/dict';
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
import { drawerSchema } from './data';
import TagStylePicker from './tag-style-picker.vue';
@@ -57,8 +58,16 @@ function setupSelectType(listClass: string) {
selectType.value = isDefault ? 'default' : 'custom';
}
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
);
const [BasicDrawer, drawerApi] = useVbenDrawer({
onCancel: handleCancel,
onBeforeClose,
onCancel: handleClosed,
onConfirm: handleConfirm,
async onOpenChange(isOpen) {
if (!isOpen) {
@@ -75,6 +84,7 @@ const [BasicDrawer, drawerApi] = useVbenDrawer({
setupSelectType(record.listClass);
await formApi.setValues(record);
}
await markInitialized();
drawerApi.drawerLoading(false);
},
@@ -82,7 +92,7 @@ const [BasicDrawer, drawerApi] = useVbenDrawer({
async function handleConfirm() {
try {
drawerApi.drawerLoading(true);
drawerApi.lock(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
@@ -93,19 +103,20 @@ async function handleConfirm() {
data.listClass = '';
}
await (isUpdate.value ? dictDataUpdate(data) : dictDataAdd(data));
resetInitialized();
emit('reload');
await handleCancel();
drawerApi.close();
} catch (error) {
console.error(error);
} finally {
drawerApi.drawerLoading(false);
drawerApi.lock(false);
}
}
async function handleCancel() {
drawerApi.close();
async function handleClosed() {
await formApi.resetForm();
selectType.value = 'default';
resetInitialized();
}
/**
@@ -117,7 +128,7 @@ async function handleDeSelect() {
</script>
<template>
<BasicDrawer :close-on-click-modal="false" :title="title" class="w-[600px]">
<BasicDrawer :title="title" class="w-[600px]">
<BasicForm>
<template #listClass="slotProps">
<TagStylePicker