diff --git a/src/components/TreeNode.vue b/src/components/TreeNode.vue index 45766a4..45c9013 100644 --- a/src/components/TreeNode.vue +++ b/src/components/TreeNode.vue @@ -12,13 +12,14 @@ @keydown.up.stop="up" @keydown.down.stop="down" > -
-

- - +

+ + - - + + - - + + - - + + - - + + - - + + - - -

+ + +

- + Node API + + + +

- @node-opened="log(`node-opened`)" - @node-closed="log('node-closed')" - @node-focus="log('node-focus')" - @node-toggle="log('node-toggle')" - @node-blur="log('node-blur')" - @node-edit="log('node-edit')" + - - + -

- - + diff --git a/src/setup/useTree.ts b/src/setup/useTree.ts index 108a722..0ea8bd3 100644 --- a/src/setup/useTree.ts +++ b/src/setup/useTree.ts @@ -8,6 +8,37 @@ export default function useTree(props: any, emit: (event: string, ...args: any[] const state = states.get(id); + const open = (id: string) => { + const nodes = state.nodes.value; + const currentNode = nodes[id]; + if (!currentNode) return; + + const nodeArr = Object.entries(nodes); + + for (const [key, node] of nodeArr) { + if (!node.children?.includes(id)) continue; + + const parent = nodeArr.find((el) => el[1]?.children?.includes(key)); + if (parent) open(parent[0]); // open grandparent + + if (!nodes[key].state) nodes[key].state = {} + nodes[key].state.opened = true; // open parent + } + + if (!currentNode.state) currentNode.state = {} + currentNode.state.opened = true // open node + } + + const focus = (id: string) => { + const f = state.focusFunc.get(id); + if (f) f(); + } + + const API = { + open, + focus + } + provide("emitter", emit); provide("state", state); @@ -25,6 +56,7 @@ export default function useTree(props: any, emit: (event: string, ...args: any[] return { element, - style + style, + API }; } \ No newline at end of file