Extract tree expand/collapse logic as hook (#618)

This commit is contained in:
Chenlei Hu
2024-08-24 17:43:26 -04:00
committed by GitHub
parent bff1dc91fa
commit 98064f301d
2 changed files with 77 additions and 42 deletions

73
src/hooks/treeHooks.ts Normal file
View File

@@ -0,0 +1,73 @@
import { ref } from 'vue'
import type { TreeNode } from 'primevue/treenode'
export function useTreeExpansion() {
const expandedKeys = ref<Record<string, boolean>>({})
const toggleNode = (node: TreeNode) => {
if (node.key && typeof node.key === 'string') {
if (node.key in expandedKeys.value) {
delete expandedKeys.value[node.key]
} else {
expandedKeys.value[node.key] = true
}
}
}
const toggleNodeRecursive = (node: TreeNode) => {
if (node.key && typeof node.key === 'string') {
if (node.key in expandedKeys.value) {
collapseNode(node)
} else {
expandNode(node)
}
}
}
const expandNode = (node: TreeNode) => {
if (
node.key &&
typeof node.key === 'string' &&
node.children &&
node.children.length
) {
expandedKeys.value[node.key] = true
for (const child of node.children) {
expandNode(child)
}
}
}
const collapseNode = (node: TreeNode) => {
if (
node.key &&
typeof node.key === 'string' &&
node.children &&
node.children.length
) {
delete expandedKeys.value[node.key]
for (const child of node.children) {
collapseNode(child)
}
}
}
const onNonLeafClick = (e: MouseEvent, node: TreeNode) => {
if (e.ctrlKey) {
toggleNodeRecursive(node)
} else {
toggleNode(node)
}
}
return {
expandedKeys,
toggleNode,
toggleNodeRecursive,
expandNode,
collapseNode,
onNonLeafClick
}
}