Files
admin/src/pages/article/components/ArticleDialog.vue
T

179 lines
5.2 KiB
Vue

<template>
<t-dialog
width="60%"
:header="header"
:confirmBtn="confirmBtn"
:closeOnOverlayClick="false"
@close="handleCloseDialog"
@confirm="saveArticleData"
>
<t-space direction="vertical" style="width: 100%">
<TForm ref="form" :data="articleData" :rules="rules" resetType="initial">
<TFormItem label="咨询标题:" name="title" help="名称长度大于2个汉字,小于120个汉字">
<t-input v-model.trim="articleData.title" clearable placeholder="请输入咨询标题" />
</TFormItem>
<TFormItem label="咨询分类:" name="categoryId">
<t-select-input
:value="articleData.categoryName"
placeholder="请选择咨询分类"
:popup-visible="popupVisible"
:popup-props="{ overlayInnerStyle: { padding: '6px' } }"
clearable
allow-input
@popup-visible-change="onPopupVisibleChange"
@clear="onClear"
@input-change="onInputChange"
>
<template #panel>
<ul class="flex flex-col gap-1 p-0">
<li
v-for="item in categoryList"
:key="item.id"
class="block rounded-sm cursor-pointer px-2 py-0.5 text-gray-900 transition-colors duration-200 whitespace-nowrap overflow-hidden text-ellipsis hover:bg-gray-200"
@click="() => onOptionClick({ id: item.id, value: item.name })"
>
{{ item.name }}
</li>
</ul>
</template>
<template #suffixIcon>
<chevron-down-icon />
</template>
</t-select-input>
</TFormItem>
<TFormItem label="内容:" name="content"> </TFormItem>
</TForm>
</t-space>
</t-dialog>
</template>
<script lang="ts" setup>
import type { Category } from '@/model/category'
import type { Article } from '@/model/article'
import { computed, ref, reactive, watch } from 'vue'
import type { FormInstanceFunctions, FormProps, SelectInputProps } from 'tdesign-vue-next'
import { MessagePlugin } from 'tdesign-vue-next'
import { ChevronDownIcon } from 'tdesign-icons-vue-next'
import { updateArticlesByIdRequest, createArticlesRequest } from '@/api/article'
const props = defineProps<{
isEdit?: Boolean | null
headerTitle?: String | null
article?: Article | {}
categoryList: Category[] | []
}>()
const formRef = ref<FormInstanceFunctions | null>(null)
const headerText = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`)
const confirmBtnText = computed(() => (props.isEdit ? '保存' : '确定'))
const articleData: FormProps['data'] = reactive({ categoryName: '' })
const popupVisible = ref(false)
const onOptionClick = (item: { id: string; value: string }) => {
articleData.categoryName = item.value
articleData.categoryId = item.id
// 选中后立即关闭浮层
popupVisible.value = false
}
const onClear: SelectInputProps['onClear'] = () => {
articleData.categoryId = ''
articleData.categoryName = undefined
}
const onPopupVisibleChange: SelectInputProps['onPopupVisibleChange'] = (val) => {
popupVisible.value = val
}
const onInputChange: SelectInputProps['onInputChange'] = (val, context) => {
// 过滤功能
console.log(val, context)
}
const rules: FormProps['rules'] = {
name: [
{
required: true,
message: '咨询标题不能为空',
type: 'error',
trigger: 'blur'
},
{
required: true,
message: '咨询标题不能为空',
type: 'error',
trigger: 'change'
},
{
whitespace: true,
message: '咨询标题不能为空'
},
{
min: 4,
message: '输入汉字长度应在2到120之间',
type: 'error',
trigger: 'blur'
},
{
max: 240,
message: '输入汉字长度应在2到120之间',
type: 'error',
trigger: 'blur'
}
],
code: [
{
required: true,
message: '咨询分类不能为空',
type: 'error',
trigger: 'blur'
},
{
required: true,
message: '咨询分类不能为空',
type: 'error',
trigger: 'change'
},
{
whitespace: true,
message: '咨询分类不能为空'
}
],
content: [
{
max: 20000,
message: '内容不超过10000字',
type: 'error',
trigger: 'blur'
}
]
}
watch(
() => props.article,
(newArticleData) => {
Object.assign(articleData, newArticleData)
},
{ deep: true }
)
const emit = defineEmits<{
success: [void]
onInputChange: [void]
}>()
const saveArticleData = async () => {
if (form.value) {
const valid = await form.value.validate()
if (valid && typeof valid === 'boolean') {
const { title, categoryId, content, id } = articleData
if (!props.isEdit) {
await createArticlesRequest({ title, categoryId, content })
} else {
await updateArticlesByIdRequest(id, { title, categoryId, content })
}
MessagePlugin.success(`${props.isEdit ? '编辑' : '创建'}咨询成功`)
emit('success')
return
}
}
}
const handleCloseDialog = () => {
// 数据&&规则校验结果重置
if (form.value) {
form.value.reset()
form.value.clearValidate()
}
}
</script>