Skip to content

Tree

A hierarchical tree component for displaying nested data structures.

Basic Usage

vue
<script setup>
import { ref } from 'vue'

const treeData = [
  {
    id: 1,
    label: 'Level 1',
    children: [
      { id: 11, label: 'Level 1-1' },
      { id: 12, label: 'Level 1-2' }
    ]
  },
  {
    id: 2,
    label: 'Level 2',
    children: [
      { id: 21, label: 'Level 2-1' }
    ]
  }
]

const selectedKey = ref(null)
const expandedKeys = ref([1])

const handleSelect = (node) => {
  selectedKey.value = node.id
}

const handleExpand = (key, expanded) => {
  if (expanded) {
    expandedKeys.value.push(key)
  } else {
    expandedKeys.value = expandedKeys.value.filter(k => k !== key)
  }
}
</script>

<template>
  <MTree 
    :data="treeData"
    :selected-key="selectedKey"
    :expanded-keys="expandedKeys"
    @select="handleSelect"
    @expand="handleExpand"
  />
</template>

Custom Icons

vue
<template>
  <MTree :data="treeData">
    <template #icon="{ node, expanded }">
      <span v-if="node.children">{{ expanded ? '📂' : '📁' }}</span>
      <span v-else>📄</span>
    </template>
  </MTree>
</template>

With Actions

vue
<template>
  <MTree :data="treeData">
    <template #actions="{ node }">
      <MButton size="small" @click="handleAdd(node)">Add</MButton>
      <MButton size="small" type="danger" @click="handleDelete(node)">Delete</MButton>
    </template>
  </MTree>
</template>

Props

NameTypeDefaultDescription
dataTreeNode[][]Tree data
nodeKeystring'id'Property name for unique node key
labelKeystring'label'Property name for node label
childrenKeystring'children'Property name for children array
selectedKeystring | number | nullnullCurrently selected node key
expandedKeys(string | number)[][]Array of expanded node keys

Events

NameParametersDescription
select(node)Triggered when a node is clicked
expand(key, expanded)Triggered when a node is expanded/collapsed

Slots

NameScopeDescription
icon{ node, expanded }Custom node icon
actions{ node }Action buttons for each node

Released under the MIT License.