dict-data-drawer.vue
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep } from '@vben/utils';
import { useVbenForm } from '#/adapter/form';
import {
dictDataAdd,
dictDataUpdate,
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';
const emit = defineEmits<{ reload: [] }>();
interface DrawerProps {
id?: string;
dictType: string;
}
const isUpdate = ref(false);
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 80,
},
schema: drawerSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
});
/**
* 标签样式选择器
* default: 预设标签样式
* custom: 自定义标签样式
*/
const selectType = ref('default');
/**
* 根据标签样式判断是自定义还是默认
* @param listClass 标签样式
*/
function setupSelectType(listClass: string | null | undefined) {
if (!listClass) {
selectType.value = 'default';
return;
}
// 判断是自定义还是预设
const isDefault = Reflect.has(tagTypes, listClass);
selectType.value = isDefault ? 'default' : 'custom';
}
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
);
const [BasicDrawer, drawerApi] = useVbenDrawer({
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
async onOpenChange(isOpen) {
if (!isOpen) {
return null;
}
drawerApi.drawerLoading(true);
const { id, dictType } = drawerApi.getData() as DrawerProps;
isUpdate.value = !!id;
await formApi.setFieldValue('dictType', dictType);
if (id && isUpdate.value) {
const record = await dictDetailInfo(id);
setupSelectType(record.listClass || '');
await formApi.setValues(record);
}
await markInitialized();
drawerApi.drawerLoading(false);
},
});
async function handleConfirm() {
try {
drawerApi.lock(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
const data = cloneDeep(await formApi.getValues());
// 需要置空的情况 undefined不会提交给后端 需要改为空字符串
if (!data.listClass) {
data.listClass = '';
}
await (isUpdate.value ? dictDataUpdate(data) : dictDataAdd(data));
resetInitialized();
emit('reload');
drawerApi.close();
} catch (error) {
console.error(error);
} finally {
drawerApi.lock(false);
}
}
async function handleClosed() {
await formApi.resetForm();
selectType.value = 'default';
resetInitialized();
}
/**
* 取消标签选中 必须设置为undefined才行
*/
async function handleDeSelect() {
await formApi.setFieldValue('listClass', undefined);
}
</script>
<template>
<BasicDrawer :title="title" class="w-[600px]">
<BasicForm>
<template #listClass="slotProps">
<TagStylePicker
v-bind="slotProps"
v-model:select-type="selectType"
@deselect="handleDeSelect"
/>
</template>
</BasicForm>
</BasicDrawer>
</template>