refactor: 重构client-drawer

This commit is contained in:
dap 2024-09-23 08:38:55 +08:00
parent b13fafdaf5
commit 1f78061ec9
2 changed files with 47 additions and 47 deletions

View File

@ -5,7 +5,7 @@ import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { useVbenForm } from '#/adapter';
import { clientAdd, clientUpdate } from '#/api/system/client';
import { clientAdd, clientInfo, clientUpdate } from '#/api/system/client';
import { drawerSchema } from './data';
import SecretInput from './secret-input.vue';
@ -14,7 +14,7 @@ const emit = defineEmits<{ reload: [] }>();
interface DrawerProps {
update: boolean;
record?: any;
id?: number | string;
}
const isUpdate = ref(false);
@ -32,45 +32,28 @@ const [BasicForm, formApi] = useVbenForm({
wrapperClass: 'grid-cols-2',
});
function setupForm(update: boolean, record?: any) {
formApi.setState((prev) => {
return {
...prev,
schema: prev.schema?.map((item) => {
if (item.fieldName === 'clientId') {
return {
...item,
dependencies: {
show: () => update,
triggerFields: [''],
},
};
}
if (
item.fieldName === 'clientKey' ||
item.fieldName === 'clientSecret'
) {
return {
...item,
componentProps: {
...item.componentProps,
disabled: update,
},
};
}
if (item.fieldName === 'status') {
return {
...item,
componentProps: {
...item.componentProps,
disabled: record?.id === 1,
},
};
}
return item;
}),
};
});
function setupForm(update: boolean) {
formApi.updateSchema([
{
dependencies: {
show: () => update,
triggerFields: [''],
},
fieldName: 'clientId',
},
{
componentProps: {
disabled: update,
},
fieldName: 'clientKey',
},
{
componentProps: {
disabled: update,
},
fieldName: 'clientSecret',
},
]);
}
const [BasicDrawer, drawerApi] = useVbenDrawer({
@ -81,13 +64,24 @@ const [BasicDrawer, drawerApi] = useVbenDrawer({
return null;
}
drawerApi.drawerLoading(true);
const { record, update } = drawerApi.getData() as DrawerProps;
const { id, update } = drawerApi.getData() as DrawerProps;
isUpdate.value = update;
//
setupForm(update, record);
if (update && record) {
setupForm(update);
if (update && id) {
const record = await clientInfo(id);
// id1
formApi.updateSchema([
{
componentProps: {
disabled: record.id === 1,
},
fieldName: 'status',
},
]);
for (const key in record) {
await formApi.setFieldValue(key, record[key]);
await formApi.setFieldValue(key, record[key as keyof typeof record]);
}
}
drawerApi.drawerLoading(false);
@ -102,7 +96,6 @@ async function handleConfirm() {
return;
}
const data = await formApi.getValues();
console.log(data);
await (isUpdate.value ? clientUpdate(data) : clientAdd(data));
emit('reload');
await handleCancel();
@ -130,7 +123,11 @@ async function handleCancel() {
</template>
<style lang="scss" scoped>
/**
自定义组件校验失败样式
*/
:deep(.form-valid-error .ant-input[name='clientSecret']) {
border-color: hsl(var(--destructive));
box-shadow: 0 0 0 2px rgb(255 38 5 / 6%);
}
</style>

View File

@ -1,5 +1,6 @@
<script setup lang="ts">
import { Page, useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';
import clientDrawer from './client-drawer.vue';
@ -15,7 +16,9 @@ function handleAdd() {
<template>
<Page>
<a-button type="primary" @click="handleAdd">add</a-button>
<a-button type="primary" @click="handleAdd">
{{ $t('pages.common.add') }}
</a-button>
<ClientDrawer />
</Page>
</template>