OrganizationChart
Display department or personnel hierarchies from tree data. Parents sit above their children, which are arranged side by side and connected by lines. Customize, collapse, or select nodes, or switch to a horizontal hierarchy.
Import
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
Basic usage
Pass a root node through data. Every node needs a unique id; label supplies card text and children supplies descendants. Cards default to a white background, thin border, and a theme-colored top edge. Connecting lines use CSS.
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' },
{ id: 'qa', label: 'QA team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [
{ id: 'east', label: 'East region' },
{ id: 'south', label: 'South region' }
]
},
{ id: 'hr', label: 'Human resources' }
]
}
</script>
<template>
<HuiOrganizationChart :data="data" aria-label="Galaxy Technology organization chart" />
</template>
Multiple roots
data also accepts an array. Roots appear side by side as independent trees.
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData[] = [
{
id: 'bj',
label: 'Beijing branch',
children: [
{ id: 'bj-sales', label: 'Sales' },
{ id: 'bj-service', label: 'Customer service' }
]
},
{
id: 'sh',
label: 'Shanghai branch',
children: [{ id: 'sh-sales', label: 'Sales' }]
}
]
</script>
<template>
<HuiOrganizationChart :data="data" />
</template>
Custom field names
When fields differ from id, label, children, or disabled, map them with props: key identifies nodes, label provides text, children identifies descendants, and disabled identifies disabled state.
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
deptCode: 'HQ',
deptName: 'Headquarters',
subDepts: [
{ deptCode: 'FIN', deptName: 'Finance center' },
{
deptCode: 'OPS',
deptName: 'Operations center',
subDepts: [
{ deptCode: 'OPS-1', deptName: 'Customer service' },
{ deptCode: 'OPS-2', deptName: 'Logistics' }
]
}
]
}
const fieldNames = { key: 'deptCode', label: 'deptName', children: 'subDepts' }
</script>
<template>
<HuiOrganizationChart :data="data" :props="fieldNames" />
</template>
Custom nodes
The default slot customizes card content and receives node, level (0 for roots), expanded, selected, and disabled. Card borders, corners, theme colors, and selection highlighting remain. This example displays an avatar, name, role, and subordinate count:
<script setup lang="ts">
import { HuiAvatar, HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 1,
name: '王建国',
title: 'General manager',
children: [
{
id: 2,
name: '李晓明',
title: 'Technical director',
children: [
{ id: 4, name: '陈思', title: 'Frontend lead' },
{ id: 5, name: '赵磊', title: 'Backend lead' }
]
},
{
id: 3,
name: '刘芳',
title: 'Marketing director',
children: [{ id: 6, name: '孙悦', title: 'Brand manager' }]
}
]
}
const firstChar = (node: OrganizationChartNodeData) => String(node.name).slice(0, 1)
const count = (node: OrganizationChartNodeData) =>
Array.isArray(node.children) ? node.children.length : 0
</script>
<template>
<HuiOrganizationChart :data="data">
<template #default="{ node, level }">
<div class="person">
<HuiAvatar :label="firstChar(node)" :size="level === 0 ? 'default' : 'small'" />
<div class="person__text">
<div class="person__name">{{ node.name }}</div>
<div class="person__title">
{{ node.title }}<template v-if="count(node)"> · {{ count(node) }} people</template>
</div>
</div>
</div>
</template>
</HuiOrganizationChart>
</template>
<style scoped>
.person {
display: flex;
gap: var(--hui-spacing-2);
align-items: center;
text-align: left;
}
.person__name {
font-weight: var(--hui-font-weight-semibold);
}
.person__title {
font-size: var(--hui-font-size-sm);
color: var(--hui-text-color-secondary);
white-space: nowrap;
}
</style>
Node classes
node-class adds classes to .hui-organization-chart__card. A string applies to all cards; a function receives node data and level and returns a Vue-compatible class value (string, array, or object). Component styles are scoped. Define your classes globally, or use :deep() in scoped styles with an additional ancestor class for sufficient specificity.
This example uses a primary-colored company card, green department top borders, and orange team top borders:
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
type: 'company',
children: [
{
id: 'rd',
label: 'R&D center',
type: 'dept',
children: [
{ id: 'fe', label: 'Frontend team', type: 'team' },
{ id: 'be', label: 'Backend team', type: 'team' }
]
},
{
id: 'mk',
label: 'Marketing',
type: 'dept',
children: [{ id: 'east', label: 'East region', type: 'team' }]
}
]
}
const nodeClass = (node: OrganizationChartNodeData) => `org-card--${node.type}`
</script>
<template>
<HuiOrganizationChart class="org-chart" :data="data" :node-class="nodeClass" />
</template>
<style scoped>
.org-chart :deep(.org-card--company) {
color: var(--hui-color-white);
background-color: var(--hui-color-primary);
}
.org-chart :deep(.org-card--dept) {
border-top-color: var(--hui-color-success);
}
.org-chart :deep(.org-card--team) {
border-top-color: var(--hui-color-warning);
}
</style>
Collapsing nodes
With collapsible, nodes with children have a small toggle below the card. Click to expand or collapse; a minus indicates expansion. Collapsed branches are not rendered. Without collapsible, all nodes stay expanded and no toggle is shown.
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [
{ id: 'east', label: 'East region' },
{ id: 'south', label: 'South region' }
]
},
{ id: 'hr', label: 'Human resources' }
]
}
</script>
<template>
<HuiOrganizationChart :data="data" collapsible />
</template>
Default expansion depth
default-expand-level sets the initially expanded depth, counting the root as level 1. 0 collapses everything except the visible root. 1 expands the root to show level 2. 2 expands level 2 to show level 3. Omitting it expands all nodes. It determines initial state only; this example changes key to recreate the chart when switching options.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{
id: 'fe',
label: 'Frontend team',
children: [
{ id: 'web', label: 'Web team' },
{ id: 'app', label: 'App team' }
]
},
{ id: 'be', label: 'Backend team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [{ id: 'east', label: 'East region' }]
}
]
}
const level = ref<string | number>(1)
const options = [
{ label: '0', value: 0 },
{ label: '1', value: 1 },
{ label: '2', value: 2 },
{ label: 'Omitted (expand all)', value: 'all' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="level" :options="options" />
<HuiOrganizationChart
:key="level"
:data="data"
collapsible
:default-expand-level="level === 'all' ? undefined : Number(level)"
/>
</HuiSpace>
</template>
Default expanded nodes
default-expanded-keys selects initially expanded nodes and also expands their ancestors. Other nodes stay collapsed. If combined with default-expand-level, the union of both configurations is expanded. This example expands only the Frontend team path, revealing its two teams while Marketing stays collapsed:
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{
id: 'fe',
label: 'Frontend team',
children: [
{ id: 'web', label: 'Web team' },
{ id: 'app', label: 'App team' }
]
},
{ id: 'be', label: 'Backend team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [{ id: 'east', label: 'East region' }]
}
]
}
</script>
<template>
<HuiOrganizationChart :data="data" collapsible :default-expanded-keys="['fe']" />
</template>
Controlled expansion and methods
v-model:expanded-keys controls expanded nodes. When supplied, it determines expansion and supersedes default-expand-level and default-expanded-keys. Methods are also available through a ref: expand(key) expands a node and its ancestors, collapse(key) collapses it, expandAll() and collapseAll() affect all nodes, and getExpandedKeys() reads expanded keys.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type {
OrganizationChartExpose,
OrganizationChartKey,
OrganizationChartNodeData
} from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [
{ id: 'east', label: 'East region' },
{ id: 'south', label: 'South region' }
]
}
]
}
const chartRef = ref<OrganizationChartExpose>()
const expandedKeys = ref<OrganizationChartKey[]>(['co'])
const result = ref('')
const read = () => {
result.value = `getExpandedKeys():${JSON.stringify(chartRef.value?.getExpandedKeys())}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="chartRef?.expandAll()">Expand all</HuiButton>
<HuiButton @click="chartRef?.collapseAll()">Collapse all</HuiButton>
<HuiButton @click="chartRef?.expand('east')">Expand to the East region</HuiButton>
<HuiButton @click="chartRef?.collapse('rd')">Collapse R&D center</HuiButton>
<HuiButton @click="expandedKeys = ['co', 'mk']">Expand only Marketing</HuiButton>
<HuiButton @click="read">Read expanded keys</HuiButton>
</HuiSpace>
<HuiOrganizationChart
ref="chartRef"
v-model:expanded-keys="expandedKeys"
:data="data"
collapsible
/>
<p>v-model:{{ expandedKeys }}</p>
<p>{{ result || 'Try Read expanded keys' }}</p>
</HuiSpace>
</template>
Single selection
With selection-mode="single", clicking a card selects it with a primary border and tinted background. Clicking another selects that node; clicking the current node again deselects it. v-model:selection is a selected key or null.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartSelection } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
},
{ id: 'mk', label: 'Marketing' },
{ id: 'hr', label: 'Human resources' }
]
}
const selection = ref<OrganizationChartSelection>('rd')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiOrganizationChart v-model:selection="selection" :data="data" selection-mode="single" />
<p>Selected keys: {{ selection ?? 'None' }}</p>
</HuiSpace>
</template>
Multiple selection
With selection-mode="multiple", clicking cards toggles selection, and v-model:selection is a key array. Nodes with disabled: true cannot be selected and do not emit nodeClick.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartSelection } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' },
{ id: 'qa', label: 'QA team' }
]
},
{ id: 'mk', label: 'Marketing' },
{ id: 'legal', label: 'Legal (disabled)', disabled: true }
]
}
const selection = ref<OrganizationChartSelection>(['fe', 'mk'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiOrganizationChart v-model:selection="selection" :data="data" selection-mode="multiple" />
<p>Selected keys: {{ selection }}</p>
</HuiSpace>
</template>
Events
Clicking, expanding, collapsing, and selecting emit events with node data and key. This example displays recent events:
nodeClick: emitted after a card click or Enter/Space on a focused node; also includeslevel. Expansion toggles and disabled nodes do not emit it.nodeExpandandnodeCollapse: emitted for expansion changes made by buttons or keyboard, but not method calls.selectionChange: emitted when selection changes;selectionis the requested result andnodescontains the selected node data.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type {
OrganizationChartNodeClickPayload,
OrganizationChartNodeData,
OrganizationChartNodeTogglePayload,
OrganizationChartSelectionChangePayload
} from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [{ id: 'east', label: 'East region' }]
}
]
}
const events = ref<string[]>([])
const record = (text: string) => {
events.value = [text, ...events.value].slice(0, 6)
}
const onClick = ({ node, level }: OrganizationChartNodeClickPayload) =>
record(`nodeClick:${node.label}(level ${level + 1})`)
const onExpand = ({ node }: OrganizationChartNodeTogglePayload) =>
record(`nodeExpand:${node.label}`)
const onCollapse = ({ node }: OrganizationChartNodeTogglePayload) =>
record(`nodeCollapse:${node.label}`)
const onSelectionChange = ({ nodes }: OrganizationChartSelectionChangePayload) =>
record(`selectionChange: selected ${nodes.map((node) => node.label).join('、') || 'None'}`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiOrganizationChart
:data="data"
collapsible
selection-mode="multiple"
@node-click="onClick"
@node-expand="onExpand"
@node-collapse="onCollapse"
@selection-change="onSelectionChange"
/>
<div>
<p v-if="events.length === 0">Try clicking a card or an expansion button</p>
<p v-for="(text, index) in events" :key="index" style="margin: 0 0 4px">{{ text }}</p>
</div>
</HuiSpace>
</template>
Horizontal layout
direction accepts vertical (default), with parents above side-by-side children, or horizontal, with parents to the left of vertically arranged children. Horizontal layout suits deeper trees with fewer siblings. Expansion buttons move to the right and arrow-key behavior rotates accordingly (see Keyboard interaction).
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartDirection, OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' },
{ id: 'qa', label: 'QA team' }
]
},
{
id: 'mk',
label: 'Marketing',
children: [
{ id: 'east', label: 'East region' },
{ id: 'south', label: 'South region' }
]
},
{ id: 'hr', label: 'Human resources' }
]
}
const direction = ref<OrganizationChartDirection>('horizontal')
const options = [
{ label: 'Vertical', value: 'vertical' },
{ label: 'Horizontal', value: 'horizontal' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="direction" :options="options" />
<HuiOrganizationChart :data="data" :direction="direction" collapsible />
</HuiSpace>
</template>
Scrolling wide content
Content wider than the component produces a horizontal scrollbar. With a height or max-height, taller content also scrolls vertically. Narrower content is centered, or left-aligned in horizontal mode.
focus(key) focuses a node and scrolls its card into view, first expanding ancestors if needed. This example has 12 divisions below headquarters. Use the buttons to locate and select a division:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type {
OrganizationChartExpose,
OrganizationChartKey,
OrganizationChartNodeData,
OrganizationChartSelection
} from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'root',
label: 'Headquarters',
children: Array.from({ length: 12 }, (_, i) => ({
id: `d${i + 1}`,
label: `Division ${i + 1}`,
children: [
{ id: `d${i + 1}-product`, label: 'Product team' },
{ id: `d${i + 1}-ops`, label: 'Operations team' }
]
}))
}
const chartRef = ref<OrganizationChartExpose>()
const selection = ref<OrganizationChartSelection>(null)
const locate = (key: OrganizationChartKey) => {
selection.value = key
chartRef.value?.focus(key)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="locate('d1')">Locate division 1</HuiButton>
<HuiButton @click="locate('d7')">Locate division 7</HuiButton>
<HuiButton @click="locate('d12-ops')">Locate division 12 Operations team</HuiButton>
</HuiSpace>
<HuiOrganizationChart
ref="chartRef"
v-model:selection="selection"
:data="data"
selection-mode="single"
collapsible
:default-expand-level="1"
style="max-height: 280px; border: 1px solid var(--hui-border-color-light)"
/>
</HuiSpace>
</template>
Spacing and line colors
Set these CSS variables on the component or any ancestor to configure hierarchy spacing, sibling-branch spacing, and line colors. The component does not redefine public values at its root; recursive nodes and expansion buttons inherit the same configuration. Unset values use the defaults below. Nested charts also inherit them; override on an inner chart when different values are needed. This example configures an ancestor:
| Variable | Default | Description |
|---|---|---|
--hui-organization-chart-level-gap | var(--hui-spacing-10) | Parent–child card spacing (horizontal spacing in horizontal layout), 40px |
--hui-organization-chart-node-gap | var(--hui-spacing-4) | Spacing between sibling branches (vertical in horizontal layout), 16px |
--hui-organization-chart-line-color | var(--hui-border-color) | Connecting-line and expansion-button border color |
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: 'Galaxy Technology',
children: [
{
id: 'rd',
label: 'R&D center',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
},
{ id: 'mk', label: 'Marketing' }
]
}
</script>
<template>
<div
style="--hui-organization-chart-level-gap: 64px; --hui-organization-chart-node-gap: 40px; --hui-organization-chart-line-color: var(--hui-color-primary)"
>
<HuiOrganizationChart :data="data" />
</div>
</template>
Keyboard interaction
The chart occupies one Tab stop. Entry returns to the last focused node. On first entry, or if that node is hidden, focus goes to the first visible selected node, then the first root as a fallback. The focused card has a primary-colored outline and scrolls into view. Arrow keys move within the chart:
| Key (vertical) | Key (horizontal) | Action |
|---|---|---|
| ↑ | ← | Focus the parent |
| ↓ | → | Expand a collapsed node; otherwise focus its first child |
| ← / → | ↑ / ↓ | Focus the previous/next visible node at the same level, including across parents |
| Home / End | Home / End | Focus the first root / last visible node |
| Enter / Space | Enter / Space | Act like a card click: toggle selection when enabled and emit nodeClick |
| + / - | + / - | Expand / collapse when collapsible is enabled |
Keys with Ctrl, Alt, or Meta are left to the browser. Buttons and inputs inside custom content handle their own keyboard interactions.
Internal and controlled selection
Selection control depends independently on whether selection is supplied. Omission or listeners alone keep internal selection; an explicit prop, including undefined or null, is controlled and an empty value selects nothing. After the parent accepts, rewrites, or rejects an update, highlighting, ARIA, and slot selected reflect the actual value. selectionChange continues to report the user's requested selection and nodes. Expansion configuration is independent.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartSelection } from '@nscode/handy-ui'
const selectionData: OrganizationChartNodeData[] = [
{ id: 'a', label: 'A' },
{ id: 'b', label: 'B' },
{ id: 'c', label: 'C' }
]
const selectionSingle = ref<OrganizationChartSelection>('a')
const selectionMultiple = ref<OrganizationChartSelection>(['a'])
const selectionRedraw = ref(0)
const selectionSingleLast = ref('None yet')
const selectionMultipleLast = ref('None yet')
const recordSelectionSingle = (value: OrganizationChartSelection) => {
selectionSingleLast.value = JSON.stringify(value)
}
const recordSelectionMultiple = (value: OrganizationChartSelection) => {
selectionMultipleLast.value = JSON.stringify(value)
}
</script>
<template>
<HuiSpace class="organization-selection-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="selection-redraw" @click="selectionRedraw++"
>Parent redraw {{ selectionRedraw }}</HuiButton
>
<span
>Click B, then redraw the parent to compare internal state with the actual controlled
selection.</span
>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace class="selection-single" direction="vertical" style="width: 370px; max-width: 100%">
<strong>Single selection</strong>
<HuiSpace class="selection-local" direction="vertical">
<span>Selection omitted</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
/>
</HuiSpace>
<HuiSpace class="selection-listener" direction="vertical">
<span>Update listener only</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
@update:selection="recordSelectionSingle"
/>
</HuiSpace>
<HuiSpace class="selection-empty" direction="vertical">
<span>Explicit undefined</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
:selection="undefined"
/>
</HuiSpace>
<HuiSpace class="selection-fixed" direction="vertical">
<span>Fixed A; parent rejects updates</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
selection="a"
/>
</HuiSpace>
<HuiSpace class="selection-bound" direction="vertical">
<span>Two-way binding</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
v-model:selection="selectionSingle"
/>
</HuiSpace>
<span class="selection-status"
>Requested value: {{ selectionSingleLast }}; bound value: {{ selectionSingle }}</span
>
</HuiSpace>
<HuiSpace
class="selection-multiple"
direction="vertical"
style="width: 370px; max-width: 100%"
>
<strong>Multiple selection</strong>
<HuiSpace class="selection-local" direction="vertical">
<span>Selection omitted</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
/>
</HuiSpace>
<HuiSpace class="selection-listener" direction="vertical">
<span>Update listener only</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
@update:selection="recordSelectionMultiple"
/>
</HuiSpace>
<HuiSpace class="selection-empty" direction="vertical">
<span>Explicit undefined</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
:selection="undefined"
/>
</HuiSpace>
<HuiSpace class="selection-fixed" direction="vertical">
<span>Fixed A; parent rejects updates</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
:selection="['a']"
/>
</HuiSpace>
<HuiSpace class="selection-bound" direction="vertical">
<span>Two-way binding</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
v-model:selection="selectionMultiple"
/>
</HuiSpace>
<span class="selection-status"
>Requested value: {{ selectionMultipleLast }}; bound value: {{ selectionMultiple }}</span
>
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
Internal and controlled expansion
expandedKeys and selection determine control independently. Omission or listeners alone keep internal expansion; an explicit prop, including undefined as empty, delegates control to the parent. Default configuration is copied at creation. Later data or newly appearing child branches initialize from that creation configuration, while nodes already operated on preserve user expansion. Initialization emits no extra updates.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartKey } from '@nscode/handy-ui'
const expandedData: OrganizationChartNodeData = {
id: 'r',
label: 'Root',
children: [
{ id: 'a', label: 'A', children: [{ id: 'a1', label: 'A1' }] },
{ id: 'b', label: 'B' }
]
}
const expandedBound = ref<OrganizationChartKey[]>([])
const expandedRedraw = ref(0)
const expandedLast = ref('None yet')
const recordExpanded = (keys: OrganizationChartKey[]) => {
expandedLast.value = JSON.stringify(keys)
}
</script>
<template>
<HuiSpace class="organization-expanded-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="expanded-redraw" @click="expandedRedraw++"
>Parent redraw {{ expandedRedraw }}</HuiButton
>
<span
>Focus the root and press + or click its expansion button, then redraw the parent to compare
expansion.</span
>
</HuiSpace>
<HuiSpace class="expanded-local" direction="vertical">
<span>Expansion prop omitted</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
/>
</HuiSpace>
<HuiSpace class="expanded-listener" direction="vertical">
<span>Update listener only</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
@update:expanded-keys="recordExpanded"
/>
</HuiSpace>
<HuiSpace class="expanded-empty" direction="vertical">
<span>Explicit undefined</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
:expanded-keys="undefined"
/>
</HuiSpace>
<HuiSpace class="expanded-fixed" direction="vertical">
<span>Fixed empty array; parent rejects updates</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
:expanded-keys="[]"
/>
</HuiSpace>
<HuiSpace class="expanded-bound" direction="vertical">
<span>Two-way binding</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
v-model:expanded-keys="expandedBound"
/>
</HuiSpace>
<span class="expanded-status"
>Requested value: {{ expandedLast }}; bound expansion: {{ expandedBound }}</span
>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| data | OrganizationChartNodeData | OrganizationChartNodeData[] | null | — | A root node or an array of independent roots displayed side by side; null or undefined displays nothing |
| props | OrganizationChartFieldNames | {} | Custom field names: key (default 'id'), label, children, and disabled |
| direction | 'vertical' | 'horizontal' | 'vertical' | Layout direction: parents above (vertical) or to the left (horizontal) |
| collapsible | boolean | false | Allow collapse/expansion toggles for nodes with children; otherwise all nodes remain expanded |
| defaultExpandLevel | number | — | Default expansion depth, counting roots as level 1. Read at creation and applied when a node first gains children. Without either default configuration, all nodes expand |
| defaultExpandedKeys | OrganizationChartKey[] | — | Default keys copied at creation; initializes nodes and ancestors when child branches appear. Combined with defaultExpandLevel as a union |
| expandedKeys | OrganizationChartKey[] | — | Supports v-model:expanded-keys. Explicit values, including undefined as empty, are controlled; omission/listeners alone store internal expansion. Defaults never override the parent's actual value |
| selectionMode | 'single' | 'multiple' | — | Single or multiple selection; omitted means nodes are not selectable |
| selection | OrganizationChartSelection | — | Selected key or null in single mode, key array in multiple mode. Supports v-model:selection; omission/listeners alone use internal state, explicit undefined/null is controlled empty |
| nodeClass | OrganizationChartClassValue | ((node: OrganizationChartNodeData, level: number) => OrganizationChartClassValue) | — | Card classes; a function receives node data and zero-based level |
Events
Event declarations and script listeners use camelCase. Templates may use @node-click, @node-expand, @node-collapse, and @selection-change. Payload objects and emission order are unchanged.
| Event | Parameters | Description |
|---|---|---|
| nodeClick | (data: { node: OrganizationChartNodeData; key: OrganizationChartKey; level: number }) | Emitted for a card click or Enter/Space, but not expansion buttons or disabled nodes |
| nodeExpand | (data: { node: OrganizationChartNodeData; key: OrganizationChartKey }) | Emitted for button/keyboard expansion, not method calls |
| nodeCollapse | (data: { node: OrganizationChartNodeData; key: OrganizationChartKey }) | Emitted for button/keyboard collapse, not method calls |
| selectionChange | (data: { selection: OrganizationChartSelection; nodes: OrganizationChartNodeData[] }) | Emitted for user selection changes; nodes follows selection order |
| update:selection | (value: OrganizationChartSelection) | Selection update used by v-model:selection |
| update:expandedKeys | (keys: OrganizationChartKey[]) | Expanded-key update from buttons, keyboard, or methods; used by v-model:expanded-keys |
Slots
| Slot | Scope | Description |
|---|---|---|
| default | { node: OrganizationChartNodeData, level: number, expanded: boolean, selected: boolean, disabled: boolean } | Custom card content; defaults to the node label. Root level is 0 |
Methods
Call through the component ref.
| Method | Parameters | Description | | --------------- | ------------------------------ | ------------------------ | --------------------------------------------------------------------------------------------------------------- | | expand | (key: OrganizationChartKey) | void | Expand a node and its ancestors | | collapse | (key: OrganizationChartKey) | void | Collapse a node | | expandAll | — | void | Expand all nodes | | collapseAll | — | void | Collapse all nodes, leaving roots visible | | getExpandedKeys | — | OrganizationChartKey[] | Currently expanded keys of nodes with children; all such keys when collapsible is off | | focus | (key?: OrganizationChartKey) | void | Focus a node and scroll it into view, expanding ancestors when hidden; omitted key focuses the current Tab stop |
Types
import type {
OrganizationChartKey,
OrganizationChartDirection,
OrganizationChartSelectionMode,
OrganizationChartSelection,
OrganizationChartNodeData,
OrganizationChartFieldNames,
OrganizationChartClassValue,
OrganizationChartProps,
OrganizationChartSlotProps,
OrganizationChartNodeClickPayload,
OrganizationChartNodeTogglePayload,
OrganizationChartSelectionChangePayload,
OrganizationChartExpose
} from '@nscode/handy-ui'
type OrganizationChartKey = string | number
/** Single mode uses a key or null; multiple mode uses a key array */
type OrganizationChartSelection = OrganizationChartKey | OrganizationChartKey[] | null
interface OrganizationChartNodeData {
id?: OrganizationChartKey
label?: string
children?: OrganizationChartNodeData[]
disabled?: boolean
/** Additional fields are allowed for custom node content */
[key: string]: unknown
}
/** Shape of the props mapping to fields in node data */
interface OrganizationChartFieldNames {
key?: string
label?: string
children?: string
disabled?: string
}