Files
syops/frontend/src/features/kb/components/KbGroupDrawer.vue
T
2026-10-14 20:25:35 +08:00

106 lines
3.3 KiB
Vue

<script setup lang="ts">
/**
* features/kb/components/KbGroupDrawer.vue
* 知识库分组新建/编辑弹窗。
* 依赖:Element Plus 对话框与表单、Vue I18n、父级 KbAdminPanel 传入的表单状态。
* 逻辑:只负责弹窗展示、表单双向同步和提交/关闭事件;接口调用与列表刷新由父组件处理。
* 字段只有名称和描述,用随内容增高的对话框,避免整屏抽屉留下大块空白。
*/
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { FolderAdd } from '@element-plus/icons-vue';
import { ElButton, ElDialog, ElForm, ElFormItem, ElInput } from 'element-plus';
export interface KbGroupFormValue {
name: string;
description: string;
}
const props = defineProps<{
open: boolean;
mode: 'create' | 'edit';
form: KbGroupFormValue;
submitting: boolean;
}>();
const emit = defineEmits<{
(e: 'update:form', value: KbGroupFormValue): void;
(e: 'submit'): void;
(e: 'close'): void;
}>();
const { t } = useI18n();
const formTitle = computed(() => props.mode === 'edit' ? t('kbAdmin.form.editTitle') : t('kbAdmin.form.createTitle'));
const formDescription = computed(() => props.mode === 'edit' ? t('kbAdmin.form.editDesc') : t('kbAdmin.form.createDesc'));
const submitText = computed(() => {
if (props.submitting) return props.mode === 'edit' ? t('kbAdmin.form.saving') : t('kbAdmin.form.submitting');
return props.mode === 'edit' ? t('kbAdmin.form.saveChanges') : t('kbAdmin.form.create');
});
/**
* 更新指定表单字段,并将完整表单值同步回父组件。
* @param field - 要改的字段,只能是名称或描述
* @param value - 输入框当前文本
*/
const updateField = (field: keyof KbGroupFormValue, value: string) => {
emit('update:form', {
...props.form,
[field]: value,
});
};
</script>
<template>
<ElDialog
:model-value="open"
width="480px"
align-center
append-to-body
:close-on-click-modal="!submitting"
:close-on-press-escape="!submitting"
:show-close="!submitting"
@close="emit('close')"
>
<template #header>
<div>
<h3 class="text-lg font-semibold text-primary">{{ formTitle }}</h3>
<p class="mt-1 text-sm text-muted">{{ formDescription }}</p>
</div>
</template>
<ElForm class="kb-group-form" label-position="top" @submit.prevent="emit('submit')">
<ElFormItem :label="t('kbAdmin.form.groupName')">
<ElInput
:model-value="form.name"
:placeholder="t('kbAdmin.form.groupNamePlaceholder')"
@update:model-value="updateField('name', String($event))"
/>
</ElFormItem>
<ElFormItem :label="t('kbAdmin.form.groupDesc')">
<ElInput
:model-value="form.description"
type="textarea"
:rows="4"
:placeholder="t('kbAdmin.form.groupDescPlaceholder')"
@update:model-value="updateField('description', String($event))"
/>
</ElFormItem>
</ElForm>
<template #footer>
<ElButton :disabled="submitting" @click="emit('close')">{{ t('common.cancel') }}</ElButton>
<ElButton type="primary" :icon="FolderAdd" :loading="submitting" @click="emit('submit')">
{{ submitText }}
</ElButton>
</template>
</ElDialog>
</template>
<style scoped>
.kb-group-form :deep(.el-form-item:last-child) {
margin-bottom: 0;
}
</style>