TreeSelect
Select from hierarchical data in a dropdown tree, such as departments or categories. The dropdown uses HuiTree and follows its expansion, checkbox, and disabled-node rules.
Import
import { HuiTreeSelect } from '@nscode/handy-ui'
Basic usage
Pass tree data through data and bind the selected node key with v-model. Keys use the id field by default. In single selection, only leaf nodes are selectable by default; clicking a parent expands or collapses it. An empty selection is null and displays the localized placeholder.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const other = ref<string | number | null>(null)
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiTreeSelect v-model="value" :data="treeData" style="width: 240px" />
<HuiTreeSelect
v-model="other"
:data="treeData"
placeholder="Choose a category"
style="width: 240px"
/>
</HuiSpace>
<span>Selected value: {{ JSON.stringify(value) }}</span>
</HuiSpace>
</template>
Controlled updates and standard empty values
Passing modelValue makes the component controlled, including an explicit undefined. Omitting the prop enables internal state; listening for updates alone does not make it controlled. The parent can accept, normalize, or reject requests. Display text, tags, Tree checkboxes, and form validation read the actual model. Adding or removing the model prop during use produces a development diagnostic. Removing it restores the previous internal state.
The following example compares ten binding patterns. Parent rerenders preserve internal selections. Clearing a single selection keeps a JSON field with null; clearing multiple selections keeps [].
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeSelectExpose, TreeSelectValue } from '@nscode/handy-ui'
const data = [
{ id: 'p', label: 'R&D department', children: [{ id: 'a', label: 'Frontend team' }] },
{ id: 'b', label: 'Product department' },
{ id: 'c', label: 'Operations department' }
]
const redraw = ref(0)
const accepted = ref<TreeSelectValue>('a')
const acceptedMultiple = ref<TreeSelectValue>(['a'])
const normalized = ref<TreeSelectValue>('a')
const normalizedMultiple = ref<TreeSelectValue>(['a'])
const methodRef = ref<TreeSelectExpose>()
const messages = ref<string[]>([])
const log = (name: string, value?: unknown) => {
messages.value = [
`${name}${value === undefined ? '' : `: ${JSON.stringify(value)}`}`,
...messages.value
].slice(0, 6)
}
const normalizeSingle = (value: TreeSelectValue) => {
normalized.value = value === null ? null : 'c'
log('Single normalization request', value)
}
const normalizeMultiple = (value: TreeSelectValue) => {
normalizedMultiple.value = Array.isArray(value) && value.length ? ['c'] : []
log('Multiple normalization request', value)
}
</script>
<template>
<HuiSpace direction="vertical" fill :data-parent="redraw" data-testid="tree-select-state-demo">
<HuiSpace wrap>
<HuiButton data-testid="tree-select-redraw" @click="redraw++"
>Parent rerender {{ redraw }}</HuiButton
>
<HuiButton data-testid="tree-select-method-clear" @click="methodRef?.clear()"
>Call single-selection clear()</HuiButton
>
</HuiSpace>
<p>
Omitting modelValue uses internal state; passing the prop makes it controlled. Rejected
requests keep the current value. Clearing requests null for single selection and [] for
multiple selection.
</p>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical" style="width: 250px" data-case="self-single">
<span>Internal single selection; listens for updates only</span>
<HuiTreeSelect
ref="methodRef"
:data="data"
clearable
@update:model-value="log('Internal single update', $event)"
@clear="log('Internal single clear')"
@change="log('Internal single change', $event)"
/>
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="self-multiple">
<span>Internal multiple selection; no model binding</span>
<HuiTreeSelect :data="data" multiple clearable />
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="accepted-single">
<span>Single selection accepted by the parent: {{ JSON.stringify(accepted) }}</span>
<HuiTreeSelect v-model="accepted" :data="data" clearable />
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="accepted-multiple">
<span
>Multiple selection accepted by the parent: {{ JSON.stringify(acceptedMultiple) }}</span
>
<HuiTreeSelect v-model="acceptedMultiple" :data="data" multiple clearable />
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="rejected-single">
<span>Single selection fixed to Product by the parent</span>
<HuiTreeSelect
model-value="b"
:data="data"
clearable
@update:model-value="log('Rejected single request', $event)"
/>
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="rejected-multiple">
<span>Multiple selection fixed to Product by the parent</span>
<HuiTreeSelect
:model-value="['b']"
:data="data"
multiple
clearable
@update:model-value="log('Rejected multiple request', $event)"
/>
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="normalized-single">
<span>Single selection normalized to Operations: {{ JSON.stringify(normalized) }}</span>
<HuiTreeSelect
:model-value="normalized"
:data="data"
clearable
@update:model-value="normalizeSingle"
/>
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="normalized-multiple">
<span
>Multiple selection normalized to Operations:
{{ JSON.stringify(normalizedMultiple) }}</span
>
<HuiTreeSelect
:model-value="normalizedMultiple"
:data="data"
multiple
clearable
@update:model-value="normalizeMultiple"
/>
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="undefined-single">
<span>Explicit undefined in single selection; parent rejects updates</span>
<HuiTreeSelect
:model-value="undefined"
:data="data"
@update:model-value="log('Undefined single request', $event)"
/>
</HuiSpace>
<HuiSpace direction="vertical" style="width: 250px" data-case="undefined-multiple">
<span>Explicit undefined in multiple selection; parent rejects updates</span>
<HuiTreeSelect
:model-value="undefined"
:data="data"
multiple
@update:model-value="log('Undefined multiple request', $event)"
/>
</HuiSpace>
</HuiSpace>
<p role="status" data-testid="tree-select-state-status">
{{ messages.join('; ') || 'Select or clear nodes to inspect requests and event order.' }}
</p>
</HuiSpace>
</template>
Selecting any level
With check-strictly, parent nodes are selectable in single selection. Clicking their text selects them; click the preceding arrow to expand them. This is usually paired with :expand-on-click-node="false".
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiTreeSelect
v-model="value"
:data="treeData"
check-strictly
:expand-on-click-node="false"
default-expand-all
/>
</template>
Multiple selection
Add multiple to display a checkbox before each node. The bound value is an array of keys and selected nodes appear as tags. Clicking leaf text also toggles its checkbox. Clicking parent text expands the node; use its checkbox to select it.
Parent and child selection is linked by default. Checking a parent selects its enabled descendants. The bound array includes both parent and child keys.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<(string | number)[]>([21])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiTreeSelect v-model="value" :data="treeData" multiple clearable style="width: 400px" />
</template>
Add check-strictly to make every checkbox independent.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<(string | number)[]>([])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiTreeSelect v-model="value" :data="treeData" multiple check-strictly />
</template>
Clearable
With clearable, a clear button appears when a value exists and the selector is enabled. Clicking it requests null for single selection or [] for multiple selection. A controlled parent decides whether to accept the request.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(111)
const values = ref<(string | number)[]>([21, 22])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiTreeSelect v-model="value" :data="treeData" clearable style="width: 240px" />
<HuiTreeSelect v-model="values" :data="treeData" multiple clearable style="width: 320px" />
</HuiSpace>
<p style="margin-top: 12px">
Single: {{ JSON.stringify(value) }}; multiple: {{ JSON.stringify(values) }}
</p>
</template>
Filtering
With filterable, type in the open dropdown to filter node text without case sensitivity. Matches, their ancestors, and their descendants remain visible. Ancestors expand automatically. An empty result displays the localized empty message. When a value is selected, the search input uses the localized search placeholder.
The left selector uses single selection and the right uses multiple selection. Try searching for “1-1” or “2-2”:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const values = ref<(string | number)[]>([])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiTreeSelect
v-model="value"
:data="treeData"
filterable
placeholder="Search by keyword"
style="width: 240px"
/>
<HuiTreeSelect
v-model="values"
:data="treeData"
filterable
multiple
placeholder="Search by keyword"
style="width: 320px"
/>
</HuiSpace>
</template>
Disabled
disabled disables the entire selector: the dropdown cannot open, the clear button is hidden, and tags have no close button. A node with disabled: true cannot be selected, checked, expanded, or collapsed. Checking its parent does not select the disabled node.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const empty = ref<string | number | null>(null)
const single = ref<string | number | null>(11)
const multi = ref<(string | number)[]>([11, 13])
const value = ref<string | number | null>(null)
const values = ref<(string | number)[]>([])
const data: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{ id: 11, label: 'Level 2: 1-1' },
{ id: 12, label: 'Level 2: 1-2 (disabled)', disabled: true },
{ id: 13, label: 'Level 2: 1-3' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Disabled</span>
<HuiTreeSelect v-model="empty" :data="data" disabled style="width: 200px" />
<HuiTreeSelect v-model="single" :data="data" disabled clearable style="width: 200px" />
<HuiTreeSelect v-model="multi" :data="data" multiple disabled style="width: 260px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Disabled node</span>
<HuiTreeSelect v-model="value" :data="data" default-expand-all style="width: 200px" />
<HuiTreeSelect
v-model="values"
:data="data"
multiple
default-expand-all
style="width: 260px"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Sizes
size accepts large, default, and small, matching HuiInput and HuiButton: heights are 40 / 32 / 24px, font sizes are 16 / 14 / 12px, and border radii are 8 / 6 / 4px. The dropdown tree also follows this size, with row heights of 40 / 32 / 28px and font sizes of 16 / 14 / 12px. In multiple selection, checkbox sizes follow TreeSelect (18 / 16 / 14px), independently of the surrounding Form. Without an explicit size, the selector inherits from HuiFormItem, HuiForm, and HuiConfigProvider in that order, then falls back to default.
Each row compares one size: a clearable single selector, a multiple selector, and a button of the same size.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(111)
const values = ref<(string | number)[]>([21, 22])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiTreeSelect v-model="value" :data="treeData" size="large" clearable style="width: 200px" />
<HuiTreeSelect v-model="values" :data="treeData" size="large" multiple style="width: 280px" />
<HuiButton size="large">Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiTreeSelect v-model="value" :data="treeData" clearable style="width: 200px" />
<HuiTreeSelect v-model="values" :data="treeData" multiple style="width: 280px" />
<HuiButton>Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiTreeSelect v-model="value" :data="treeData" size="small" clearable style="width: 200px" />
<HuiTreeSelect v-model="values" :data="treeData" size="small" multiple style="width: 280px" />
<HuiButton size="small">Button</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Expansion
default-expand-all initially expands the dropdown tree. accordion permits only one expanded node at each level. expand-on-click-node defaults to true, so clicking parent text toggles expansion. With false, only the arrow toggles expansion; in multiple selection, clicking parent text then toggles its checkbox.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const values = ref<(string | number)[]>([])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTreeSelect
v-model="value"
:data="treeData"
default-expand-all
placeholder="Expand all (default-expand-all)"
/>
<HuiTreeSelect
v-model="value"
:data="treeData"
accordion
placeholder="Expand one node per level (accordion)"
/>
<HuiTreeSelect
v-model="values"
:data="treeData"
multiple
:expand-on-click-node="false"
placeholder="Click parent text to check (expand-on-click-node is false)"
/>
</HuiSpace>
</template>
Custom field names
Use props when data fields differ from id, label, children, or disabled. If only the key field differs, use node-key.
The left example maps all four fields with props. The right only renames the key to code, so node-key="code" is sufficient:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const code = ref<string | number | null>(null)
const departments = [
{
code: 'dev',
name: 'R&D department',
items: [
{ code: 'fe', name: 'Frontend team' },
{ code: 'be', name: 'Backend team' },
{ code: 'qa', name: 'QA team (disabled)', locked: true }
]
},
{ code: 'ops', name: 'IT Operations' }
]
const fieldNames = { id: 'code', label: 'name', children: 'items', disabled: 'locked' }
const regions = [
{
code: 'east',
label: 'East China',
children: [
{ code: 'shanghai', label: 'Shanghai' },
{ code: 'hangzhou', label: 'Hangzhou' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiTreeSelect
v-model="value"
:data="departments"
:props="fieldNames"
default-expand-all
style="width: 240px"
/>
<HuiTreeSelect
v-model="code"
:data="regions"
node-key="code"
default-expand-all
style="width: 240px"
/>
</HuiSpace>
<p style="margin-top: 12px">
Left: {{ JSON.stringify(value) }}; right: {{ JSON.stringify(code) }}
</p>
</template>
Dropdown maximum height
The dropdown is at most 300px high by default and scrolls when content exceeds this height. A numeric max-height uses pixels; strings may include a unit. The examples compare :max-height="120" with max-height="10em". Open them to see scrolling:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{
id: 11,
label: 'Level 2: 1-1',
children: [
{ id: 111, label: 'Level 3: 1-1-1' },
{ id: 112, label: 'Level 3: 1-1-2' }
]
}
]
},
{
id: 2,
label: 'Level 1: 2',
children: [
{ id: 21, label: 'Level 2: 2-1' },
{ id: 22, label: 'Level 2: 2-2' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiTreeSelect
v-model="value"
:data="treeData"
:max-height="120"
default-expand-all
style="width: 240px"
/>
<HuiTreeSelect
v-model="value"
:data="treeData"
max-height="10em"
default-expand-all
style="width: 240px"
/>
</HuiSpace>
</template>
Empty data
When data is empty or filtering finds no matches, the dropdown displays the localized empty message. Replace it with the empty slot. The left example has no data; enter a nonexistent term in the right example:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const dept = ref<string | number | null>(null)
const data: TreeNodeData[] = [
{
id: 'dev',
label: 'R&D department',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiTreeSelect v-model="value" :data="[]" style="width: 240px">
<template #empty>No departments available</template>
</HuiTreeSelect>
<HuiTreeSelect
v-model="dept"
:data="data"
filterable
placeholder="Try “Finance”"
style="width: 240px"
>
<template #empty>No matching department</template>
</HuiTreeSelect>
</HuiSpace>
</template>
Events
node-click fires for tree-node clicks, including nodes that are not selectable. check fires when a checkbox is toggled in multiple selection. User selection, tag removal, and clearing emit change, including repeated selection of the same value. visible-change reports dropdown opening and closing. clear reports a clear request. Recent events appear below the single and multiple selectors:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const value = ref<string | number | null>(null)
const values = ref<(string | number)[]>([])
const logs = ref<string[]>([])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{ id: 11, label: 'Level 2: 1-1' },
{ id: 12, label: 'Level 2: 1-2' }
]
},
{ id: 2, label: 'Level 1: 2' }
]
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onChange = (val: unknown) => log(`change: ${JSON.stringify(val)}`)
const onVisibleChange = (visible: boolean) => log(`visible-change: ${visible}`)
const onNodeClick = (data: { node: TreeNodeData; key: string | number }) =>
log(`node-click: ${data.node.label}`)
const onCheck = (data: { checkedKeys: (string | number)[]; key: string | number }) =>
log(`check: ${data.key}, checked keys: ${JSON.stringify(data.checkedKeys)}`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiTreeSelect
v-model="value"
:data="treeData"
clearable
style="width: 200px"
@change="onChange"
@visible-change="onVisibleChange"
@node-click="onNodeClick"
@clear="log('clear')"
/>
<HuiTreeSelect
v-model="values"
:data="treeData"
multiple
clearable
style="width: 280px"
@change="onChange"
@check="onCheck"
@clear="log('clear')"
/>
</HuiSpace>
<div class="logs">
<div v-if="logs.length === 0">No events yet</div>
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
</div>
</HuiSpace>
</template>
<style scoped>
.logs {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Calling methods
Call focus(), blur(), and clear() through a ref. clear() shares the clear-button path: it requests null for single selection and [] for multiple selection, with events ordered update:modelValue → clear → change. A controlled parent still decides whether to update its value. Calls have no effect when disabled, inside a readonly form, or after unmount. The readonly selectedNodes property lists the current selection as { key, label, node }:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTreeSelect } from '@nscode/handy-ui'
import type { TreeNodeData, TreeSelectExpose } from '@nscode/handy-ui'
const treeSelectRef = ref<TreeSelectExpose>()
const values = ref<(string | number)[]>([11])
const treeData: TreeNodeData[] = [
{
id: 1,
label: 'Level 1: 1',
children: [
{ id: 11, label: 'Level 2: 1-1' },
{ id: 12, label: 'Level 2: 1-2' }
]
},
{ id: 2, label: 'Level 1: 2' }
]
</script>
<template>
<HuiSpace wrap>
<HuiTreeSelect
ref="treeSelectRef"
v-model="values"
:data="treeData"
multiple
style="width: 280px"
/>
<HuiButton @click="treeSelectRef?.focus()">Focus</HuiButton>
<HuiButton @click="treeSelectRef?.blur()">Blur</HuiButton>
<HuiButton @click="treeSelectRef?.clear()">Clear (request [])</HuiButton>
</HuiSpace>
<p style="margin-top: 12px">
selectedNodes: {{ treeSelectRef?.selectedNodes.map((item) => item.label).join(', ') }}
</p>
</template>
Using in a form
Inside HuiFormItem, actual value changes trigger change rules and closing the dropdown triggers blur rules. The selector inherits size and disabled when omitted. In this example, opening and closing the department selector without a selection shows a required-field error:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiMessage,
HuiSpace,
HuiTreeSelect
} from '@nscode/handy-ui'
import type { FormRule, TreeNodeData } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = reactive<{ dept?: string | number; members: (string | number)[] }>({
dept: null as string | number | null,
members: []
})
const rules: Record<string, FormRule[]> = {
dept: [{ required: true, message: 'Choose a department', trigger: ['change', 'blur'] }],
members: [{ required: true, message: 'Select at least one member', trigger: 'change' }]
}
const depts: TreeNodeData[] = [
{
id: 'dev',
label: 'R&D department',
children: [
{ id: 'fe', label: 'Frontend team' },
{ id: 'be', label: 'Backend team' }
]
}
]
const members: TreeNodeData[] = [
{
id: 'fe',
label: 'Frontend team',
children: [
{ id: 'zhangsan', label: 'Zhang San' },
{ id: 'lisi', label: 'Li Si' }
]
}
]
const submit = async () => {
const result = await formRef.value?.validate()
if (result?.valid) HuiMessage.success('Validation passed')
}
</script>
<template>
<HuiForm ref="formRef" :model="model" :rules="rules" label-width="80px" style="max-width: 420px">
<HuiFormItem label="Department" prop="dept">
<HuiTreeSelect v-model="model.dept" :data="depts" clearable />
</HuiFormItem>
<HuiFormItem label="Members" prop="members">
<HuiTreeSelect v-model="model.members" :data="members" multiple clearable />
</HuiFormItem>
<HuiFormItem>
<HuiSpace>
<HuiButton type="primary" @click="submit">Submit</HuiButton>
<HuiButton @click="formRef?.resetFields()">Reset</HuiButton>
</HuiSpace>
</HuiFormItem>
</HuiForm>
</template>
Keyboard interaction
Use Tab to focus the selector, then:
| Key | Action |
|---|---|
| Enter, Space | Toggle the dropdown, except while typing in the search input |
| ↓ | Open the dropdown, or move focus to its first node when already open |
| Esc | Close the dropdown and return focus to the selector |
| Tab | Close the dropdown |
Once focus enters the dropdown, the tree handles navigation: ↑ and ↓ move between nodes, → expands, ← collapses or moves to a parent, Enter acts as a node click (selecting in single selection), and Space toggles a checkbox in multiple selection.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | TreeSelectValue | undefined | null for single selection, [] for multiple selection | Controlled whenever the prop is present; internal state otherwise. Explicit undefined is a controlled empty value |
| data | TreeNodeData[] | - | Required tree data |
| multiple | boolean | false | Enable multiple selection |
| checkStrictly | boolean | false | Allow parent selection in single selection; make checkboxes independent in multiple selection |
| disabled | boolean | false | Disable the selector |
| clearable | boolean | false | Show a clear button when a value exists |
| filterable | boolean | false | Enable filtering by node text |
| placeholder | string | Localized | Placeholder text |
| size | 'small' | 'default' | 'large' | 'default' | Component size |
| props | TreeSelectFieldNames | {} | Custom data field names; id takes priority over nodeKey |
| nodeKey | string | 'id' | Unique node-key field; overridden by props.id when both are supplied |
| defaultExpandAll | boolean | false | Expand every node initially |
| expandOnClickNode | boolean | true | Toggle expansion by clicking node text; with false, use the arrow |
| accordion | boolean | false | Allow only one expanded node per level |
| maxHeight | number | string | 300 | Dropdown maximum height; numeric values use px |
| validateEvent | boolean | true | Trigger form-item validation on actual value changes and dropdown closing |
| collapseTags | boolean | true | Collapse excess selection tags |
| maxCollapseTags | number | 3 | Number of tags displayed before collapsing |
Events
| Event | Parameters | Description |
|---|---|---|
| update:modelValue | (value: TreeSelectValue) | Request a value update; used by v-model |
| change | (value: TreeSelectValue) | User selection, checkbox toggling, tag removal, or clearing; repeated selection also emits, programmatic updates do not |
| clear | - | A clear-button or clear() request; ordered update:modelValue → clear → change |
| visibleChange | (visible: boolean) | Dropdown visibility changes |
| nodeClick | (data: { node: TreeNodeData; key: string | number }) | Tree-node clicks, including nodes that are not selectable |
| check | (data: { checkedKeys: (string | number)[]; node: TreeNodeData; key: string | number }) | Checkbox toggling in multiple selection; checkedKeys contains all requested keys |
Slots
| Slot | Scope | Description |
|---|---|---|
| empty | - | Content shown for empty data or no filtering results; localized by default |
Methods
Call through a component ref.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| focus | - | void | Focus the selector, or its search input when filtering is enabled and the dropdown is open |
| blur | - | void | Blur the selector |
| clear | - | void | Request null for single selection or [] for multiple selection; no effect while disabled, in a readonly form, or after unmount |
The ref also exposes readonly selectedNodes, an array of { key, label, node }. node is the matching data node, or null when no matching node exists.
Types
import type {
TreeNodeData,
TreeSelectFieldNames,
TreeSelectSize,
TreeSelectValue,
TreeSelectSelectedNode,
TreeSelectExpose
} from '@nscode/handy-ui'
interface TreeNodeData {
id?: string | number
label?: string
children?: TreeNodeData[]
disabled?: boolean
[key: string]: unknown
}
/** Structure of the props mapping: field names in the data */
interface TreeSelectFieldNames {
label?: string
children?: string
disabled?: string
isLeaf?: string
id?: string
}
type TreeSelectSize = 'small' | 'default' | 'large'
type TreeSelectValue = string | number | (string | number)[] | null
interface TreeSelectSelectedNode {
key: string | number
label: string
node: TreeNodeData | null
}
interface TreeSelectExpose {
focus: () => void
blur: () => void
clear: () => void
readonly selectedNodes: TreeSelectSelectedNode[]
}