Vue Teatree
A simple treeview component for VueJS with no added dependencies.
Install
https://www.npmjs.com/package/vue-teatree
Example
- Import your components:
Teatree
is the main component you will pass your data to (below).NodeType
contains the Typescript interface that defines each node type.NodeName
is a pre-built component you will pass into a slot.NodeToggle
is a pre-built component you will pass into a slot.
NodeName
and NodeToggle
are provided as a convenience. Feel free to write your own.
- Prepare your data
- Pass it into Teatree
More details
- The
Teatree
treeview is purely a function of your data. If you want to make changes to the treeview (e.g. toggling children, hiding nodes etc.) you should modify the data object. NodeToggle
andNodeName
can be replaced with your own components and passed into thenode-toggle
andnode-name
slots respectively.
NodeType
Teatree accepts an array of NodeType
as its roots
prop. This means you can render multiple roots in the treeview.
NodeToggle
It is a pre-built component to render the node's toggle. If you want to implement your own, take a look at the source code!
Props:
Name | Type | Required | Notes |
---|---|---|---|
node |
NodeType |
Yes |
NodeName
It is a pre-built component to render the node's name. If you want to implement your own, take a look at the source code!
It has a number of props that can be wired up to provide additional functionality (track clicks etc.):
Name | Type | Required | Notes |
---|---|---|---|
node |
NodeType |
Yes | |
handleNodeLeftClick |
(event: any, node: NodeType) => any |
Yes | Pass an empty function () => {} if you don't have one. |
handleNodeRightClick |
(event: any, node: NodeType) => any |
Yes | Pass an empty function () => {} if you don't have one. |
Styling
Import default styles using: @import "~vue-teatree/build/Teatree.css";
Here are all the default styles. Override them to your liking:
Teatree
NodeToggle
NodeName
Common use cases
The following code/advice is not well tested. Apologies for any errors.
Toggle everything shut
You can achieve an effect similar to VSCode where you can toggle/collapse all nodes closed. You need to write a simple tree-traversal:
Hide leaves
If you're using the treeview for a file explorer, it might be useful to hide the leaves, and only show the parent directories. You can use the show
node property to hide the leaf nodes.
Show a right-click menu
You'll need something like: https://github.com/rawilk/vue-context.
Use the handleNodeRightClick
component prop to call the context menu: