<template>
<div class="tree-demo-wrap">
<TsTree :data="treeData" :showCheckbox="false" defaultExpandAll>
<template #label-prefix="{ hasChildren, expanded }">
<TsIcon style="margin-right: 6px;" v-if="hasChildren" :name="expanded ? 'FolderOpenIcon' : 'FolderCloseIcon'" font-size="14px" class="folder-icon" />
</template>
<template #label-suffix="{ node, hasChildren }">
<span style="margin-left: 6px;" v-if="hasChildren && getChildCount(node) > 0" class="tree-count">{{ getChildCount(node) }}</span>
</template>
</TsTree>
<div class="tree-demo-note">该示例使用 FolderCloseIcon / FolderOpenIcon 构建文件夹目录,并隐藏了复选框。</div>
</div>
</template>
<script setup lang="ts">
import { TsIcon } from 'tui';
const treeData = [
{
key: 'root-docs',
label: 'docs',
children: [
{
key: 'root-docs-guides',
label: 'guides',
children: [
{ key: 'root-docs-guides-getting-started', label: 'getting-started.md' },
{ key: 'root-docs-guides-tree-component', label: 'tree-component.md' },
],
},
{
key: 'root-docs-examples',
label: 'examples',
children: [
{ key: 'root-docs-examples-tree-basic', label: 'tree-basic.vue' },
{ key: 'root-docs-examples-tree-folder', label: 'tree-folder.vue' },
],
},
],
},
{
key: 'root-src',
label: 'src',
children: [
{
key: 'root-src-components',
label: 'components',
children: [
{ key: 'root-src-components-tree', label: 'Tree.vue' },
{ key: 'root-src-components-tree-node', label: 'TreeNode.vue' },
],
},
],
},
];
const getChildCount = (node: any): number => {
return Array.isArray(node?.children) ? node.children.length : 0;
};
</script>
<style scoped>
.tree-demo-wrap {
display: grid;
gap: 10px;
max-width: 560px;
}
.folder-icon {
color: var(--ts-color-blue);
}
.tree-count {
font-size: 12px;
line-height: 1;
padding: 2px 6px;
border-radius: 10px;
background-color: rgba(13, 142, 247, 0.12);
color: var(--ts-color-blue);
}
.tree-demo-note {
font-size: 13px;
color: var(--ts-color-desc);
}
</style>