UnisKB/ui/src/components/folder-tree/index.vue

125 lines
2.7 KiB
Vue
Raw Normal View History

2025-05-08 08:23:03 +00:00
<template>
2025-05-12 10:19:28 +00:00
<div>
<el-input
v-model="filterText"
:placeholder="$t('common.search')"
prefix-icon="Search"
clearable
2025-05-15 02:25:05 +00:00
class="p-8"
2025-05-12 10:19:28 +00:00
/>
<div
@click="handleSharedNodeClick"
v-if="isShared"
class="shared-knowledge"
:class="currentNodeKey === 'share' && 'active'"
>
2025-06-13 09:59:21 +00:00
<AppIcon iconName="app-folder-share-active" style="font-size: 18px"></AppIcon>
<span class="ml-8 lighter">{{ $t('views.system.share_knowledge') }}</span>
</div>
2025-05-12 10:19:28 +00:00
<el-tree
ref="treeRef"
:data="data"
:props="defaultProps"
@node-click="handleNodeClick"
:filter-node-method="filterNode"
:default-expanded-keys="[currentNodeKey]"
:current-node-key="currentNodeKey"
highlight-current
node-key="id"
>
<template #default="{ node, data }">
<div class="custom-tree-node flex align-center">
<AppIcon iconName="app-folder" style="font-size: 16px"></AppIcon>
<span class="ml-8">{{ node.label }}</span>
2025-05-12 10:19:28 +00:00
</div>
</template>
</el-tree>
</div>
2025-05-08 08:23:03 +00:00
</template>
<script lang="ts" setup>
2025-05-12 10:19:28 +00:00
import { ref, watch } from 'vue'
import type { TreeInstance } from 'element-plus'
import { t } from '@/locales'
2025-05-08 08:23:03 +00:00
defineOptions({ name: 'FolderTree' })
2025-05-09 06:42:51 +00:00
const props = defineProps({
data: {
type: Array,
default: () => [],
},
2025-05-12 10:19:28 +00:00
currentNodeKey: {
type: String,
default: 'root',
},
isShared: {
type: Boolean,
default: false,
},
2025-05-09 06:42:51 +00:00
})
2025-05-08 08:23:03 +00:00
interface Tree {
2025-05-09 06:42:51 +00:00
name: string
2025-05-08 08:23:03 +00:00
children?: Tree[]
id?: string
2025-05-08 08:23:03 +00:00
}
const defaultProps = {
children: 'children',
2025-05-09 06:42:51 +00:00
label: 'name',
2025-05-08 08:23:03 +00:00
}
2025-05-12 10:19:28 +00:00
const emit = defineEmits(['handleNodeClick'])
const treeRef = ref<TreeInstance>()
const filterText = ref('')
watch(filterText, (val) => {
treeRef.value!.filter(val)
})
const filterNode = (value: string, data: Tree) => {
if (!value) return true
return data.name.includes(value)
}
const handleNodeClick = (data: Tree) => {
emit('handleNodeClick', data)
}
const handleSharedNodeClick = () => {
2025-06-13 09:17:01 +00:00
treeRef.value?.setCurrentKey(undefined)
emit('handleNodeClick', { id: 'share', name: t('views.system.share_knowledge') })
}
2025-05-08 08:23:03 +00:00
</script>
<style lang="scss" scoped>
.shared-knowledge {
padding-left: 8px;
display: flex;
align-items: center;
height: 40px;
position: relative;
margin-bottom: 8px;
border-radius: 4px;
cursor: pointer;
&:hover {
background: var(--app-text-color-light-1);
color: var(--el-menu-text-color);
}
&.active {
color: var(--el-color-primary);
background: var(--el-color-primary-light-9);
}
&::after {
content: '';
position: absolute;
bottom: -4px;
background-color: #1f232926;
left: 0;
width: 100%;
height: 1px;
}
}
</style>