<template>
|
<div class="category-picker">
|
<n-select
|
v-model:value="selected"
|
size="small"
|
clearable
|
:options="options"
|
:placeholder="placeholder"
|
:disabled="disabled"
|
@update:value="handleChange"
|
/>
|
<!-- 允许现场新建:上传前才发现没有合适分类是很常见的情况,
|
强迫用户先去别处建好再回来会打断操作流 -->
|
<n-button size="small" quaternary :disabled="disabled" @click="showCreate = true">
|
+ 新建分类
|
</n-button>
|
|
<n-modal
|
v-model:show="showCreate"
|
preset="card"
|
title="新建分类"
|
class="create-modal"
|
:bordered="false"
|
>
|
<n-form label-placement="top">
|
<n-form-item label="分类名称">
|
<n-input
|
v-model:value="newName"
|
placeholder="例如:产品文档、售后政策、技术规范"
|
maxlength="64"
|
show-count
|
@keyup.enter="handleCreate"
|
/>
|
</n-form-item>
|
<n-form-item label="说明(可选)">
|
<n-input
|
v-model:value="newDescription"
|
placeholder="便于他人理解这个分类装什么"
|
maxlength="255"
|
/>
|
</n-form-item>
|
<n-form-item v-if="allowGlobal" label="可见范围">
|
<n-radio-group v-model:value="newGlobal">
|
<n-radio :value="false">仅自己</n-radio>
|
<n-radio :value="true">所有用户(全局分类)</n-radio>
|
</n-radio-group>
|
</n-form-item>
|
</n-form>
|
<template #footer>
|
<div class="modal-footer">
|
<n-button size="small" @click="showCreate = false">取消</n-button>
|
<n-button size="small" type="primary" :loading="creating" @click="handleCreate">
|
创建
|
</n-button>
|
</div>
|
</template>
|
</n-modal>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed, onMounted, ref } from 'vue'
|
import {
|
NButton,
|
NForm,
|
NFormItem,
|
NInput,
|
NModal,
|
NRadio,
|
NRadioGroup,
|
NSelect,
|
useMessage
|
} from 'naive-ui'
|
import { createCategory, listCategories } from '@/api/categoryApi'
|
|
const props = defineProps({
|
/** 当前选中的分类 id(支持 v-model:value) */
|
value: { type: Number, default: null },
|
/** 是否允许创建全局分类(仅管理员页面传 true) */
|
allowGlobal: { type: Boolean, default: false },
|
disabled: { type: Boolean, default: false },
|
placeholder: { type: String, default: '选择分类(可不选)' }
|
})
|
|
const emit = defineEmits(['update:value', 'created'])
|
|
const message = useMessage()
|
const categories = ref([])
|
const selected = ref(props.value)
|
const showCreate = ref(false)
|
const creating = ref(false)
|
const newName = ref('')
|
const newDescription = ref('')
|
const newGlobal = ref(false)
|
|
const options = computed(() =>
|
categories.value.map((c) => ({
|
label: c.isGlobal ? `${c.name}(全局)` : c.name,
|
value: c.id
|
}))
|
)
|
|
// 父组件异步设置初值时同步过来
|
import { watch } from 'vue'
|
watch(
|
() => props.value,
|
(v) => {
|
selected.value = v
|
}
|
)
|
|
onMounted(load)
|
|
async function load() {
|
try {
|
categories.value = (await listCategories()) || []
|
} catch (error) {
|
message.error(error.message || '加载分类失败')
|
}
|
}
|
|
function handleChange(value) {
|
emit('update:value', value ?? null)
|
}
|
|
async function handleCreate() {
|
const name = newName.value.trim()
|
if (!name) {
|
message.warning('请输入分类名称')
|
return
|
}
|
creating.value = true
|
try {
|
const created = await createCategory(name, newDescription.value || null, newGlobal.value)
|
categories.value = [...categories.value, created]
|
// 新建后直接选中,省掉一次手动点击
|
selected.value = created.id
|
emit('update:value', created.id)
|
emit('created', created)
|
message.success(`分类「${created.name}」已创建`)
|
|
showCreate.value = false
|
newName.value = ''
|
newDescription.value = ''
|
newGlobal.value = false
|
} catch (error) {
|
message.error(error.message || '创建失败')
|
} finally {
|
creating.value = false
|
}
|
}
|
</script>
|
|
<style scoped>
|
.category-picker {
|
display: flex;
|
align-items: center;
|
gap: 6px;
|
}
|
|
.category-picker :deep(.n-select) {
|
flex: 1;
|
min-width: 0;
|
}
|
|
.create-modal {
|
width: 440px;
|
}
|
|
.modal-footer {
|
display: flex;
|
justify-content: flex-end;
|
gap: 8px;
|
}
|
</style>
|