Cascader
Selects through hierarchical data, such as province / city / district or department / team. Use it for shallow hierarchies with relatively few options per level. Use HuiTreeSelect for deep hierarchies or when the full tree needs to remain visible.
Import
import { HuiCascader } from '@nscode/handy-ui'
Basic Usage
Pass hierarchical data through options. V-model binds the path from the first level to the selected node, as an array of each level's value. By default, only leaf nodes are selectable. Selection closes the dropdown and displays the full path. When empty, the placeholder defaults to the localized selection prompt.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const other = ref<(string | number)[] | null>()
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<!-- Selecting Wudaokou sets value to ['beijing', 'haidian', 'wudaokou'] -->
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiCascader v-model="value" :options="options" style="width: 260px" />
<HuiCascader
v-model="other"
:options="options"
placeholder="Choose your region"
style="width: 260px"
/>
</HuiSpace>
<span>Selected value: {{ JSON.stringify(value) ?? 'undefined' }}</span>
</HuiSpace>
</template>
Expansion Trigger
Trigger controls how the next level opens: click (default) opens on click; hover opens on pointer entry, while selection still requires clicking. Expand-trigger is an equivalent option and takes precedence when both are supplied.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const hoverValue = ref<(string | number)[] | null>()
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiCascader
v-model="value"
:options="options"
trigger="click"
placeholder="Click to expand (click)"
style="width: 260px"
/>
<HuiCascader
v-model="hoverValue"
:options="options"
trigger="hover"
placeholder="Hover to expand (hover)"
style="width: 260px"
/>
</HuiSpace>
</template>
Showing Only the Final Level
Show-all-levels defaults to true, displaying the full path in the input and multiple-selection tags. Set it to false to display only the selected node's text. The bound value remains the full path. The first row uses the default; the second sets false:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>(['beijing', 'haidian', 'wudaokou'])
const values = ref<(string | number)[][]>([
['beijing', 'haidian', 'wudaokou'],
['guangzhou', 'tianhe']
])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">true</span>
<HuiCascader v-model="value" :options="options" style="width: 220px" />
<HuiCascader v-model="values" :options="options" multiple style="width: 320px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">false</span>
<HuiCascader
v-model="value"
:options="options"
:show-all-levels="false"
style="width: 220px"
/>
<HuiCascader
v-model="values"
:options="options"
multiple
:show-all-levels="false"
style="width: 320px"
/>
</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>
Custom Separator
Separator joins level labels in the path. The default is / without surrounding spaces. The input, multiple-selection tags and search results all use it. Include spaces explicitly when needed, such as ' / '.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>(['beijing', 'haidian', 'wudaokou'])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">'/'</span>
<HuiCascader v-model="value" :options="options" style="width: 260px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">' / '</span>
<HuiCascader v-model="value" :options="options" separator=" / " style="width: 260px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">' > '</span>
<HuiCascader v-model="value" :options="options" separator=" > " style="width: 260px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">'-'</span>
<HuiCascader v-model="value" :options="options" separator="-" 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);
white-space: pre;
}
</style>
Clearable
Clearable shows a clear button when nonempty, hidden when empty or disabled. Clearing requests null in single mode or [] in multiple mode, emitting update:modelValue, clear and change in order. Display changes only when the parent accepts the request. Null preserves a field in JSON serialization; 0 and empty strings within paths remain real node values.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>(['beijing', 'haidian', 'wudaokou'])
const values = ref<(string | number)[][]>([['guangzhou', 'tianhe']])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiCascader v-model="value" :options="options" clearable style="width: 260px" />
<HuiCascader v-model="values" :options="options" multiple clearable style="width: 320px" />
</HuiSpace>
<p style="margin-top: 12px">
Single: {{ JSON.stringify(value) ?? 'undefined' }}; multiple: {{ JSON.stringify(values) }}
</p>
</template>
Disabled
Disabled prevents opening the whole selector, hides its clear button and removes close icons from multiple-selection tags. A data node with disabled=true cannot be selected or expanded, making its descendants inaccessible too. Selecting a parent in multiple mode does not select disabled descendants.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const empty = ref<(string | number)[] | null>()
const single = ref<(string | number)[] | null>(['beijing', 'haidian'])
const multi = ref<(string | number)[][]>([
['beijing', 'haidian'],
['beijing', 'dongcheng']
])
const value = ref<(string | number)[] | null>()
const values = ref<(string | number)[][]>([])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{ value: 'haidian', label: 'Haidian District' },
{ value: 'chaoyang', label: 'Chaoyang District (disabled)', disabled: true },
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{ value: 'shanghai', label: 'Shanghai (disabled)', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Entire selector disabled</span>
<HuiCascader v-model="empty" :options="options" disabled style="width: 200px" />
<HuiCascader v-model="single" :options="options" disabled clearable style="width: 200px" />
<HuiCascader v-model="multi" :options="options" multiple disabled style="width: 300px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Disabled options</span>
<HuiCascader v-model="value" :options="options" style="width: 200px" />
<HuiCascader v-model="values" :options="options" multiple style="width: 300px" />
</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>
Selecting Any Level
Check-strictly allows selecting every level. Clicking a node with children selects it and opens its next level without closing the dropdown. Selecting a leaf closes it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiCascader v-model="value" :options="options" check-strictly clearable />
</template>
Multiple Selection
Multiple adds checkboxes and binds an array of paths. Selected items appear as tags.
- Checking a parent selects all enabled leaf descendants. Only leaf paths are stored. A partially selected branch displays an indeterminate parent checkbox.
- Clicking a parent's text only expands it; its checkbox changes selection.
- A tag's close icon removes that selection.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[][]>([
['beijing', 'haidian', 'wudaokou'],
['guangzhou', 'tianhe']
])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiCascader v-model="value" :options="options" multiple clearable style="width: 420px" />
</template>
Combining multiple with check-strictly makes parents and children independent. Checking a parent stores that parent's own path.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[][]>([])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiCascader v-model="value" :options="options" multiple check-strictly />
</template>
Collapsed Tags
Multiple mode enables collapse-tags by default, showing the first max-collapse-tags items, 3 by default, and collapsing the remainder into +N. Set :collapse-tags="false" to show all tags. Collapse-tags-tooltip is disabled by default; enable it to reveal collapsed options on hover over +N.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[][]>([
['beijing', 'haidian', 'zhongguancun'],
['beijing', 'haidian', 'wudaokou'],
['beijing', 'dongcheng'],
['guangzhou', 'tianhe']
])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCascader v-model="value" :options="options" multiple collapse-tags />
<HuiCascader v-model="value" :options="options" multiple collapse-tags :max-collapse-tags="2" />
<HuiCascader v-model="value" :options="options" multiple collapse-tags collapse-tags-tooltip />
</HuiSpace>
</template>
Search
Filterable allows typing while open. Results form a list of full paths. A match at any path level qualifies, without regard to case. Only selectable nodes appear: leaves by default, or all nodes with check-strictly. Multiple mode adds checkboxes to results.
Compare single, multiple and check-strictly modes below by entering Haidian, matching their translated display label:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const values = ref<(string | number)[][]>([])
const strictValue = ref<(string | number)[] | null>()
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiCascader
v-model="value"
:options="options"
filterable
placeholder="Single selection"
style="width: 220px"
/>
<HuiCascader
v-model="values"
:options="options"
filterable
multiple
placeholder="Multiple selection"
style="width: 300px"
/>
<HuiCascader
v-model="strictValue"
:options="options"
filterable
check-strictly
placeholder="Any level"
style="width: 220px"
/>
</HuiSpace>
</template>
Debounce sets the delay after typing before search, defaulting to 300ms; values at or below 0 disable debouncing. Before-filter runs before each search. False, or a Promise resolving false, blocks searching and clears the input.
The left example waits 500ms and blocks queries containing digits. The right simulates a 500ms API check and blocks the literal trigger 测试:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const asyncValue = ref<(string | number)[] | null>()
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
// Do not search queries containing digits
const beforeFilter = (query: string) => !/\d/.test(query)
// Simulate an API validating the query
const checkKeyword = (query: string) =>
new Promise<boolean>((resolve) => {
setTimeout(() => resolve(!query.includes('测试')), 500)
})
</script>
<template>
<HuiSpace wrap>
<HuiCascader
v-model="value"
:options="options"
filterable
:debounce="500"
:before-filter="beforeFilter"
placeholder="Digits block searching"
style="width: 240px"
/>
<HuiCascader
v-model="asyncValue"
:options="options"
filterable
:before-filter="checkKeyword"
placeholder="The literal query 测试 is blocked"
style="width: 240px"
/>
</HuiSpace>
</template>
Async Search Checks and Stale Results
BeforeFilter blocks only false or a Promise resolving false; undefined allows searching. A current check that throws or rejects is caught, produces a development diagnostic, and clears the current input and applied search term, just like false. Selection remains unchanged.
Every input immediately invalidates the previous check without waiting for the next debounce. Even after A→B→A, clearing, or closing and reopening, old checks cannot clear a new query or apply early. Panel restoration still occurs after the closing animation. Cancellation only discards a result and does not abort a business request, so logs may still show an older check completing.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const guardValue = ref<(string | number)[] | null>()
const guardOptions: CascaderOption[] = [
{ value: 'slow', label: '慢 / slow result' },
{ value: 'fast', label: '快 / fast result' },
{ value: 'sync', label: '同步 / synchronous result' }
]
const guardMessages = ref<string[]>([])
const recordGuard = (message: string) => {
guardMessages.value = [message, ...guardMessages.value].slice(0, 8)
}
const guardBeforeFilter = (query: string): boolean | void | Promise<boolean | void> => {
recordGuard(`Check started: ${query}`)
if (query.includes('同步')) {
recordGuard(`Synchronous approval: ${query}`)
return
}
return new Promise<boolean | void>((resolve, reject) => {
setTimeout(
() => {
if (query.includes('异常')) {
recordGuard(`Check error: ${query}`)
reject(new Error('Example search check error'))
return
}
const allowed = !query.includes('拒绝')
recordGuard(`Check completed: ${query}, ${allowed ? 'allowed' : 'blocked'}`)
resolve(allowed ? undefined : false)
},
query.includes('慢') ? 1200 : 250
)
})
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<span
>Type 慢 (slow), then 快 (fast): old checks do not overwrite new results. 同步 returns
undefined to allow searching.</span
>
<span
>Type 拒绝 (deny) or 异常 (error): only a current blocked check clears the query. Selection is
unchanged.</span
>
<HuiCascader
v-model="guardValue"
:options="guardOptions"
:before-filter="guardBeforeFilter"
:debounce="200"
filterable
placeholder="Type 慢, 快, 同步, 拒绝 or 异常"
style="width: 360px; max-width: 100%; flex: none"
/>
<span>Current selection: {{ guardValue?.join('/') || 'Not selected' }}</span>
<ol>
<li v-for="(message, index) in guardMessages" :key="index">{{ message }}</li>
</ol>
</HuiSpace>
</template>
Sizes
Size accepts large, default or small, matching HuiInput and HuiButton: heights 40 / 32 / 24px, font sizes 16 / 14 / 12px and radii 8 / 6 / 4px. Panel row heights are 40 / approximately 36 / 28px, with matching font sizes and horizontal padding 16 / 12 / 8px, aligned to input text. Multiple-mode checkboxes use Cascader's size, 18 / 16 / 14px, rather than the outer Form's. Omitted size follows HuiFormItem, HuiForm and HuiConfigProvider, then default.
Each row compares clearable single selection, multiple selection and a matching button:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>(['beijing', 'dongcheng'])
const values = ref<(string | number)[][]>([['guangzhou', 'tianhe']])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiCascader v-model="value" :options="options" size="large" clearable style="width: 200px" />
<HuiCascader v-model="values" :options="options" size="large" multiple style="width: 260px" />
<HuiButton size="large">Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiCascader v-model="value" :options="options" clearable style="width: 200px" />
<HuiCascader v-model="values" :options="options" multiple style="width: 260px" />
<HuiButton>Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiCascader v-model="value" :options="options" size="small" clearable style="width: 200px" />
<HuiCascader v-model="values" :options="options" size="small" multiple style="width: 260px" />
<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>
Custom Field Names
When data uses fields other than value, label, children or disabled, map them through props.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const departments = [
{
code: 'dev',
name: '研发部',
items: [
{ code: 'fe', name: '前端组' },
{ code: 'be', name: '后端组' },
{ code: 'qa', name: '测试组(停用)', locked: true }
]
},
{ code: 'ops', name: '运维部' }
] as unknown as CascaderOption[]
const fieldNames = { value: 'code', label: 'name', children: 'items', disabled: 'locked' }
</script>
<template>
<HuiCascader v-model="value" :options="departments" :props="fieldNames" style="width: 280px" />
</template>
Empty Data
Empty options or unmatched searches display the localized no-data message. Override it with the empty slot. The left example has no options; in the right, search for a nonexistent region:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const value = ref<(string | number)[] | null>()
const area = ref<(string | number)[] | null>()
const options: CascaderOption[] = [
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiCascader v-model="value" :options="[]" style="width: 240px">
<template #empty>No options available yet</template>
</HuiCascader>
<HuiCascader
v-model="area"
:options="options"
filterable
placeholder="Try typing Shenzhen"
style="width: 240px"
>
<template #empty>Region not found</template>
</HuiCascader>
</HuiSpace>
</template>
Events
Change reports the requested value for user selection, checkbox toggling, tag removal or clearing; the actual display still follows the parent response. Expand-change reports expanded paths, and visible-change reports opening and closing. Remove-tag fires only for tag close-icon clicks; observe unchecking through change. Clear fires for the clear button or clear(). Recent events appear below:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const values = ref<(string | number)[][]>([['beijing', 'dongcheng']])
const logs = ref<string[]>([])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onChange = (val: unknown) => log(`change:${JSON.stringify(val)}`)
const onExpandChange = (path: (string | number)[]) =>
log(`expand-change:${JSON.stringify(path)}`)
const onVisibleChange = (visible: boolean) => log(`visible-change:${visible}`)
const onRemoveTag = (path: (string | number)[]) => log(`remove-tag:${JSON.stringify(path)}`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCascader
v-model="values"
:options="options"
multiple
clearable
style="max-width: 400px"
@change="onChange"
@expand-change="onExpandChange"
@visible-change="onVisibleChange"
@remove-tag="onRemoveTag"
@clear="log('clear')"
/>
<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() and blur() through a ref. SelectedNodes lists the current selections; activePath is the currently expanded dropdown path:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption } from '@nscode/handy-ui'
const cascaderRef = ref<InstanceType<typeof HuiCascader>>()
const value = ref<(string | number)[] | null>(['beijing', 'haidian', 'wudaokou'])
const options: CascaderOption[] = [
{
value: 'beijing',
label: 'Beijing',
children: [
{
value: 'haidian',
label: 'Haidian District',
children: [
{ value: 'zhongguancun', label: 'Zhongguancun' },
{ value: 'wudaokou', label: 'Wudaokou' }
]
},
{ value: 'dongcheng', label: 'Dongcheng District' }
]
},
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
}
]
</script>
<template>
<HuiSpace wrap>
<HuiCascader ref="cascaderRef" v-model="value" :options="options" style="width: 260px" />
<HuiButton @click="cascaderRef?.focus()">Focus (focus)</HuiButton>
<HuiButton @click="cascaderRef?.blur()">Blur (blur)</HuiButton>
</HuiSpace>
<p style="margin-top: 12px">
selectedNodes:{{
cascaderRef?.selectedNodes.map((item) => item.pathLabels.join(' / ')).join('、')
}}<br />
activePath:{{ JSON.stringify(cascaderRef?.activePath ?? []) }}
</p>
</template>
Within a Form
Inside HuiFormItem, value changes trigger change rules and dropdown closing triggers blur rules. Omitted size and disabled props follow the form. Open the region selector and close without choosing to see the required-field message:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiCascader,
HuiForm,
HuiFormItem,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
import type { CascaderOption, FormRule } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = reactive<{ area?: (string | number)[]; areas: (string | number)[][] }>({
area: undefined,
areas: []
})
const rules: Record<string, FormRule[]> = {
area: [{ required: true, message: 'Choose a region', trigger: ['change', 'blur'] }],
areas: [{ required: true, message: 'Select at least one service area', trigger: 'change' }]
}
const options: CascaderOption[] = [
{
value: 'guangzhou',
label: 'Guangzhou',
children: [
{ value: 'tianhe', label: 'Tianhe District' },
{ value: 'yuexiu', label: 'Yuexiu District' }
]
},
{
value: 'shenzhen',
label: 'Shenzhen',
children: [
{ value: 'nanshan', label: 'Nanshan District' },
{ value: 'futian', label: 'Futian District' }
]
}
]
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="Region" prop="area">
<HuiCascader v-model="model.area" :options="options" clearable />
</HuiFormItem>
<HuiFormItem label="Service Areas" prop="areas">
<HuiCascader v-model="model.areas" :options="options" 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. Once focused:
| Key | Action |
|---|---|
| Enter | Opens if closed; otherwise activates the highlighted node, expanding or selecting it |
| Space key | Opens if closed. When open, acts like a click in single mode or toggles the highlighted checkbox in multiple mode |
| ↓ / ↑ | Opens if closed; otherwise moves within the column, skipping disabled nodes and wrapping |
| → | Expands the current node and moves to its first child |
| ← | Returns to the previous level |
| Esc | Closes and returns focus to the selector |
| Tab | Closes and moves to the next element |
During search, up/down navigate results and Enter selects. Space and left/right remain available for text editing.
Internal and Controlled State
Without modelValue, or with only an update listener, single mode stores null and multiple mode stores an empty array internally. A supplied prop makes it controlled; undefined means empty. Display, tags, selection and form validation use the actual value. Rejected selection, removal or clearing preserves the parent's choices. Consecutive multiple requests are recalculated from the parent's actual paths.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderOption, CascaderProps } from '@nscode/handy-ui'
const cascadeOptions: CascaderOption[] = [
{ value: 'a', label: 'A', children: [{ value: 'a1', label: 'A1' }] },
{ value: 'b', label: 'B' },
{ value: 'c', label: 'C' }
]
const cascadeSingle = ref<CascaderProps['modelValue']>(['a', 'a1'])
const cascadeMultiple = ref<CascaderProps['modelValue']>([['a', 'a1']])
const cascadeRedraw = ref(0)
const cascadeSingleLast = ref('None yet')
const cascadeMultipleLast = ref('None yet')
const recordCascadeSingle = (value: CascaderProps['modelValue']) => {
cascadeSingleLast.value = JSON.stringify(value) ?? 'undefined'
}
const recordCascadeMultiple = (value: CascaderProps['modelValue']) => {
cascadeMultipleLast.value = JSON.stringify(value) ?? 'undefined'
}
</script>
<template>
<HuiSpace class="cascader-q1-demo" direction="vertical" fill :data-parent="cascadeRedraw">
<HuiSpace wrap>
<HuiButton class="cascader-redraw" @click="cascadeRedraw++"
>Parent redraw {{ cascadeRedraw }}</HuiButton
>
<span
>Select B and redraw the parent. Fixed and explicit empty values retain the parent's actual
selection.</span
>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace class="cascader-single" direction="vertical" style="width: 370px; max-width: 100%">
<strong>Single selection</strong>
<HuiSpace class="cascader-local" direction="vertical" style="width: 100%">
<span>Model prop omitted</span>
<HuiCascader
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-listener" direction="vertical" style="width: 100%">
<span>Update listener only</span>
<HuiCascader
@update:model-value="recordCascadeSingle"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-empty" direction="vertical" style="width: 100%">
<span>Explicit undefined</span>
<HuiCascader
:model-value="undefined"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-fixed" direction="vertical" style="width: 100%">
<span>Fixed A/A1; parent rejects updates</span>
<HuiCascader
:model-value="['a', 'a1']"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-bound" direction="vertical" style="width: 100%">
<span>Two-way binding</span>
<HuiCascader
v-model="cascadeSingle"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<span class="cascader-status"
>Observed request: {{ cascadeSingleLast }}; bound value:
{{ JSON.stringify(cascadeSingle) }}</span
>
</HuiSpace>
<HuiSpace
class="cascader-multiple"
direction="vertical"
style="width: 370px; max-width: 100%"
>
<strong>Multiple selection</strong>
<HuiSpace class="cascader-local" direction="vertical" style="width: 100%">
<span>Model prop omitted</span>
<HuiCascader
multiple
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-listener" direction="vertical" style="width: 100%">
<span>Update listener only</span>
<HuiCascader
multiple
@update:model-value="recordCascadeMultiple"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-empty" direction="vertical" style="width: 100%">
<span>Explicit undefined</span>
<HuiCascader
multiple
:model-value="undefined"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-fixed" direction="vertical" style="width: 100%">
<span>Fixed A/A1; parent rejects updates</span>
<HuiCascader
multiple
:model-value="[['a', 'a1']]"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<HuiSpace class="cascader-bound" direction="vertical" style="width: 100%">
<span>Two-way binding</span>
<HuiCascader
multiple
v-model="cascadeMultiple"
:options="cascadeOptions"
style="width: 320px; max-width: 100%"
clearable
filterable
:debounce="0"
/>
</HuiSpace>
<span class="cascader-status"
>Observed request: {{ cascadeMultipleLast }}; bound value:
{{ JSON.stringify(cascadeMultiple) }}</span
>
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
Public Clear Method
Clear() shares the icon's entry point: single mode requests null, multiple mode requests [], and events are update:modelValue → clear → change. Clearable controls only the icon; the method can clear without it. A parent rejection preserves paths, tags and selectedNodes. Disabled, form read-only and unmounted calls have no effect. Synchronous callback reentry does not duplicate events. New selection or unmounting stops the remainder of an older clear operation.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCascader, HuiSpace } from '@nscode/handy-ui'
import type { CascaderExpose, CascaderOption, CascaderProps } from '@nscode/handy-ui'
const options: CascaderOption[] = [
{ value: 0, label: 'Zero', children: [{ value: '', label: 'Empty string' }] },
{ value: 'b', label: 'B' }
]
const single = ref<CascaderProps['modelValue']>([0, ''])
const multiple = ref<CascaderProps['modelValue']>([[0, '']])
const singleRef = ref<CascaderExpose>()
const multipleRef = ref<CascaderExpose>()
const fixedSingleRef = ref<CascaderExpose>()
const fixedMultipleRef = ref<CascaderExpose>()
const notices = ref<Record<string, string[]>>({
single: [],
multiple: [],
fixedSingle: [],
fixedMultiple: []
})
const record = (kind: string, event: string, value?: CascaderProps['modelValue']) => {
notices.value[kind].push(value === undefined ? event : `${event}:${JSON.stringify(value)}`)
}
const reset = () => {
single.value = [0, '']
multiple.value = [[0, '']]
notices.value = { single: [], multiple: [], fixedSingle: [], fixedMultiple: [] }
}
</script>
<template>
<HuiSpace class="cascader-clear-demo" direction="vertical" fill>
<span
>The method and icon share one entry point: single requests null, multiple requests [];
rejection preserves actual paths.</span
>
<HuiSpace class="cascade-clear-single" wrap>
<span>Single: parent accepts</span>
<HuiCascader
ref="singleRef"
v-model="single"
:options="options"
clearable
style="width: 240px"
@update:model-value="record('single', 'update', $event)"
@clear="record('single', 'clear')"
@change="record('single', 'change', $event)"
/>
<HuiButton class="cascade-clear-single-button" @click="singleRef?.clear()"
>Clear single selection</HuiButton
>
</HuiSpace>
<span class="cascade-clear-single-status"
>Model: {{ JSON.stringify(single) }}; {{ notices.single.join(' → ') }}</span
>
<HuiSpace class="cascade-clear-fixed-single" wrap>
<span>Single: parent rejects</span>
<HuiCascader
ref="fixedSingleRef"
:model-value="[0, '']"
:options="options"
clearable
style="width: 240px"
@update:model-value="record('fixedSingle', 'update', $event)"
@clear="record('fixedSingle', 'clear')"
@change="record('fixedSingle', 'change', $event)"
/>
<HuiButton class="cascade-clear-fixed-single-button" @click="fixedSingleRef?.clear()"
>Request single clearing</HuiButton
>
</HuiSpace>
<span class="cascade-clear-fixed-single-status">{{ notices.fixedSingle.join(' → ') }}</span>
<HuiSpace class="cascade-clear-multiple" wrap>
<span>Multiple: parent accepts</span>
<HuiCascader
ref="multipleRef"
v-model="multiple"
:options="options"
multiple
clearable
style="width: 240px"
@update:model-value="record('multiple', 'update', $event)"
@clear="record('multiple', 'clear')"
@change="record('multiple', 'change', $event)"
/>
<HuiButton class="cascade-clear-multiple-button" @click="multipleRef?.clear()"
>Clear multiple selection</HuiButton
>
</HuiSpace>
<span class="cascade-clear-multiple-status"
>Model: {{ JSON.stringify(multiple) }}; {{ notices.multiple.join(' → ') }}</span
>
<HuiSpace class="cascade-clear-fixed-multiple" wrap>
<span>Multiple: parent rejects</span>
<HuiCascader
ref="fixedMultipleRef"
:model-value="[[0, '']]"
:options="options"
multiple
clearable
style="width: 240px"
@update:model-value="record('fixedMultiple', 'update', $event)"
@clear="record('fixedMultiple', 'clear')"
@change="record('fixedMultiple', 'change', $event)"
/>
<HuiButton class="cascade-clear-fixed-multiple-button" @click="fixedMultipleRef?.clear()"
>Request multiple clearing</HuiButton
>
</HuiSpace>
<span class="cascade-clear-fixed-multiple-status">{{ notices.fixedMultiple.join(' → ') }}</span>
<HuiButton class="cascade-clear-reset" @click="reset">Restore example</HuiButton>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | number | (string | number)[] | (string | number)[][] | null | - | Internal when omitted or listener-only; controlled when supplied, including undefined/null as empty. Supports v-model. A path array in single mode, an array of paths in multiple mode |
| options | CascaderOption[] | - | Required option data |
| multiple | boolean | false | Whether multiple selection is enabled |
| disabled | boolean | false | Whether the selector is disabled |
| clearable | boolean | false | Whether to show the clear button when nonempty |
| filterable | boolean | false | Whether searching is enabled |
| placeholder | string | Localized selection prompt | Placeholder text |
| size | 'small' | 'default' | 'large' | 'default' | Size |
| trigger | 'click' | 'hover' | 'click' | Opens the next level on click or hover |
| checkStrictly | boolean | false | Selects any level in single mode; makes parent/child selections independent in multiple mode |
| showAllLevels | boolean | true | Shows full paths in input and tags; false shows only the last level |
| separator | string | '/' | Separator between path labels |
| collapseTags | boolean | true | Whether extra multiple-selection tags collapse |
| maxCollapseTags | number | 3 | Visible tag count before collapsing, when collapseTags is enabled |
| collapseTagsTooltip | boolean | false | Whether hovering +N shows collapsed options |
| debounce | number | 300 | Search debounce in milliseconds, disabled at or below 0 |
| beforeFilter | (query: string) => boolean | void | Promise<boolean | void> | - | Runs before search. Undefined allows; false, throw or reject block and clear the current query. Stale results have no effect |
| props | CascaderFieldNames | {} | Custom data field mappings |
| validateEvent | boolean | true | Whether value changes and dropdown closing trigger form-item validation |
Events
| Event | Callback Arguments | Description |
|---|---|---|
| update:modelValue | (value: (string | number)[] | (string | number)[][] | null) | Value changes, used by v-model |
| change | (value: (string | number)[] | (string | number)[][] | null) | User-driven selection, checkbox toggling, tag removal or clearing changes the value |
| clear | - | Clear icon or clear() invocation |
| visibleChange | (visible: boolean) | Dropdown opens or closes |
| expandChange | (path: (string | number)[]) | Expanded path changes, with the current path |
| removeTag | (path: (string | number)[]) | Only tag close-icon clicks. Observe unchecking through change |
Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| empty | - | Empty-options or no-results content, using the localized no-data message by default |
Methods
Call through a component ref.
| Method | Arguments | Return Value | Description |
|---|---|---|---|
| focus | - | void | Focuses the selector, or search when filtering and already open |
| blur | - | void | Blurs the selector |
| clear | - | void | Requests null in single mode or [] in multiple mode. Disabled, form read-only and unmounted calls have no effect; rejection retains the actual value |
The ref also exposes two read-only properties: selectedNodes, a CascaderSelectedItem[] of current selections, and activePath, the expanded path.
Types
import type { CascaderSelectedItem } from '@nscode/handy-ui'
export interface CascaderExpose {
focus: () => void
blur: () => void
clear: () => void
readonly selectedNodes: CascaderSelectedItem[]
readonly activePath: (string | number)[]
}
import type {
CascaderOption,
CascaderFieldNames,
CascaderNode,
CascaderSelectedItem,
CascaderSize,
CascaderTrigger
} from '@nscode/handy-ui'
interface CascaderOption {
value: string | number
label: string
children?: CascaderOption[]
disabled?: boolean
[key: string]: unknown
}
/** Structure of props: corresponding field names in the data */
interface CascaderFieldNames {
value?: string
label?: string
children?: string
disabled?: string
}
/** Each item in selectedNodes on the ref */
interface CascaderSelectedItem {
node: CascaderNode
path: (string | number)[]
pathLabels: string[]
}
interface CascaderNode {
value: string | number
label: string
level: number
path: (string | number)[]
pathLabels: string[]
children?: CascaderNode[]
/** The node or any ancestor is disabled */
disabled: boolean
hasChildren: boolean
isLeaf: boolean
rawOption: CascaderOption
}
type CascaderSize = 'small' | 'default' | 'large'
type CascaderTrigger = 'click' | 'hover'