CascaderPanel
An always visible, multi-column cascader. A single selection stores a full path; multiple selections store an array of paths. Use Cascader when an input and popup are required.
Import
import { HuiCascaderPanel } from '@nscode/handy-ui'
import type { CascaderPanelValue, CascaderPanelExpose } from '@nscode/handy-ui'
Single and multiple selection
Omitting modelValue enables internal state. Providing the property, including undefined, makes the panel controlled. A parent that rejects an update keeps its selection. Picking the same leaf again still emits change. Numeric and string values are compared strictly.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascaderPanel, HuiSpace } from '@nscode/handy-ui'
import type { CascaderPanelOption, CascaderPanelValue } from '@nscode/handy-ui'
const single = ref<CascaderPanelValue>(null)
const multiple = ref<CascaderPanelValue>([])
const options: CascaderPanelOption[] = [
{
value: 'a',
label: 'Area A',
children: [
{ value: 1, label: 'Number 1' },
{ value: '1', label: 'String 1' },
{ value: 2, label: 'Disabled', disabled: true }
]
},
{ value: 'b', label: 'Area B' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCascaderPanel v-model="single" :options="options" aria-label="Single area" />
<span>Single: {{ JSON.stringify(single) }}</span>
<HuiCascaderPanel v-model="multiple" :options="options" multiple aria-label="Multiple areas" />
<span>Multiple: {{ JSON.stringify(multiple) }}</span>
</HuiSpace>
</template>
Multiple selection links parents and children by default and stores selectable leaf paths. A parent checkbox selects its available descendants; partial selection is shown as indeterminate. Clicking parent text expands the next column.
Expansion and independent selection
trigger supports click and hover. checkStrictly also allows selecting intermediate nodes independently. Arrow keys move within a column or enter/leave child columns; Home/End reach the first/last available item. Enter selects, and Space toggles a checkbox in multiple mode. Actual focus stays on the panel root. Inputs in custom slots keep their native keyboard behavior.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascaderPanel, HuiSpace } from '@nscode/handy-ui'
const last = ref('[]')
const options = [{ value: 'a', label: 'Parent', children: [{ value: 'a1', label: 'Leaf' }] }]
const expand = (path: (string | number)[]) => {
last.value = JSON.stringify(path)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace align="start" wrap>
<HuiCascaderPanel
:options="options"
trigger="click"
aria-label="Click to expand"
@expand-change="expand"
/>
<HuiCascaderPanel
:options="options"
trigger="hover"
check-strictly
aria-label="Hover and independent selection"
@expand-change="expand"
/>
</HuiSpace>
<span>Last expanded path: {{ last }}</span>
</HuiSpace>
</template>
Field mapping and node slot
Each props mapping accepts a field name or a function. The node slot receives a normalized node and checked. node.rawOption retains the original business object; path and child-node structures are independent snapshots.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascaderPanel, HuiSpace } from '@nscode/handy-ui'
import type { CascaderPanelFieldNames, CascaderPanelValue } from '@nscode/handy-ui'
const value = ref<CascaderPanelValue>(null)
const options = [
{
id: 0,
name: 'Head office',
count: 12,
nodes: [
{ id: 1, name: 'First department' },
{ id: 2, name: 'Disabled department', blocked: true }
]
}
]
const fields: CascaderPanelFieldNames = {
value: (item) => item.id,
label: 'name',
children: (item) => item.nodes,
disabled: 'blocked'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCascaderPanel
v-model="value"
:options="options"
:props="fields"
aria-label="Business departments"
>
<template #node="{ node, checked }"
><span
>{{ node.label }}{{ checked ? ' ✓' : ''
}}{{ node.rawOption.count ? `(${node.rawOption.count})` : '' }}</span
></template
>
</HuiCascaderPanel>
<span>Path: {{ JSON.stringify(value) }}</span>
</HuiSpace>
</template>
Size, border, column height, and empty state
Size follows the component, InputGroup, FormItem, Form, component defaults, then ConfigProvider. height sets each column’s actual height and defaults to 300px. Numbers use pixels; strings accept CSS lengths. Wide panels scroll horizontally. border defaults to true.
<script setup lang="ts">
import { HuiCascaderPanel, HuiSpace } from '@nscode/handy-ui'
const sizes = ['small', 'default', 'large'] as const
const options = [
{ value: 'a', label: 'Available' },
{ value: 'b', label: 'Disabled', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace align="start" wrap>
<HuiCascaderPanel
v-for="size in sizes"
:key="size"
:size="size"
:options="options"
:height="140"
:border="size !== 'default'"
:aria-label="`${size} panel`"
/>
</HuiSpace>
<HuiSpace align="start" wrap>
<HuiCascaderPanel :options="options" height="9rem" disabled aria-label="Disabled panel" />
<HuiCascaderPanel :options="[]" :height="140" aria-label="Empty panel"
><template #empty><strong>No available options</strong></template></HuiCascaderPanel
>
</HuiSpace>
</HuiSpace>
</template>
Methods, events, and controlled values
clearCheck() does not open a confirmation dialog. Confirm in the business action before calling it. This example retains the selection while waiting and ignores results after cancellation, dismissal, a selection change, or unmounting. Clearing emits update:modelValue → clear → change and collapses local columns. Single values become null; multiple values become []. A parent that rejects clearing retains its selected data.
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { HuiCascaderPanel, HuiButton, HuiMessageBox, HuiSpace } from '@nscode/handy-ui'
import type { CascaderPanelExpose, CascaderPanelValue } from '@nscode/handy-ui'
const panel = ref<CascaderPanelExpose>()
const value = ref<CascaderPanelValue>(['b'])
const last = ref('Waiting for an action')
const pending = ref(false)
const hasValue = computed(() => Array.isArray(value.value) && value.value.length > 0)
const options = [
{ value: 'a', label: 'A' },
{ value: 'b', label: 'B' }
]
let alive = true
let revision = 0
watch(
value,
() => {
revision++
},
{ deep: true, flush: 'sync' }
)
onBeforeUnmount(() => {
alive = false
revision++
})
const record = (next: CascaderPanelValue) => {
last.value = JSON.stringify(next)
}
const clear = async () => {
if (pending.value || !hasValue.value) return
const request = revision
pending.value = true
try {
const result = await HuiMessageBox.confirm(
'Clear the current selection and collapse all expanded columns?',
'Clear area selection',
{ type: 'warning' }
)
if (result.action === 'confirm' && alive && request === revision && hasValue.value)
panel.value?.clearCheck()
} catch {
/* 取消或关闭时保留选择。 */
} finally {
if (alive) pending.value = false
}
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCascaderPanel
ref="panel"
v-model="value"
:options="options"
aria-label="Editable area selection"
@change="record"
/>
<HuiSpace>
<HuiButton @click="panel?.focus()">Focus</HuiButton>
<HuiButton @click="panel?.blur()">Blur</HuiButton>
<HuiButton :disabled="pending || !hasValue" @click="clear"
>Clear after confirmation</HuiButton
>
</HuiSpace>
<HuiCascaderPanel
:model-value="['a']"
:options="options"
aria-label="Fixed parent value"
@change="record"
/>
<span>Actual value: {{ JSON.stringify(value) }};Last request: {{ last }}</span>
</HuiSpace>
</template>
Form integration
Form disabled and readonly states are upper bounds. disabled=false on the panel cannot override them. Actual value changes trigger change validation; leaving the entire panel triggers blur validation. validateEvent=false disables these validation events. Rejected controlled updates do not validate an unchanged value.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiCascaderPanel, HuiForm, HuiFormItem, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { CascaderPanelValue, FormProps } from '@nscode/handy-ui'
const readonly = ref(false)
const validateEvent = ref(true)
const model = reactive<{ area: CascaderPanelValue }>({ area: null })
const options = [
{ value: 'a', label: 'Area A' },
{ value: 'b', label: 'Area B' }
]
const rules: FormProps['rules'] = {
area: [{ required: true, message: 'Choose an area', trigger: ['change', 'blur'] }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace>
<HuiButton @click="readonly = !readonly">Readonly: {{ readonly }}</HuiButton>
<HuiButton @click="validateEvent = !validateEvent"
>Validation events: {{ validateEvent }}</HuiButton
>
</HuiSpace>
<HuiForm :model="model" :rules="rules" :readonly="readonly" size="small">
<HuiFormItem label="Area" prop="area" tip="Use arrow keys to browse"
><HuiCascaderPanel v-model="model.area" :options="options" :validate-event="validateEvent"
/></HuiFormItem>
</HuiForm>
</HuiSpace>
</template>
Props
Recreate the component with a new key when changing the algorithm of a props mapping function.
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | CascaderPanelValue | null / [] | Controlled when present; supports v-model |
| options | CascaderPanelOption[] | [] | Tree options |
| multiple | boolean | false | Multiple path selection |
| checkStrictly | boolean | false | Independent parent/child selection |
| trigger | 'click' | 'hover' | click | Child-column expansion |
| props | CascaderPanelFieldNames | Matching field names | Field names or getter functions |
| disabled | boolean | false | Disables selection, keyboard actions, and clearing |
| size | ComponentSize | Inherited | small/default/large |
| border | boolean | true | Outer border |
| height | number | string | 300 | Height of each column |
| validateEvent | boolean | true | Form validation events |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | CascaderPanelValue | Selection or clearing request |
| change | CascaderPanelValue | User selection or method clearing; independent path copies |
| clear | None | clearCheck requested clearing |
| expandChange | CascaderPanelPath | User action changed the expanded path; use @expand-change in templates |
| focus | FocusEvent | Focus entered the panel |
| blur | FocusEvent | Focus left the entire panel |
Slots
| Slot | Scope | Description |
|---|---|---|
| node | { node: CascaderPanelNode, checked: boolean } | Node text; original option is node.rawOption |
| empty | None | Empty-column content |
Methods
| Method | Description |
|---|---|
| focus() | Focuses the root entry; ignored when disabled or unmounted |
| blur() | Blurs only an active element owned by this panel |
| clearCheck() | Requests clearing and collapses columns; ignored when disabled |
Types
import type {
CascaderPanelProps,
CascaderPanelEmits,
CascaderPanelExpose,
CascaderPanelValue,
CascaderPanelPath,
CascaderPanelOption,
CascaderPanelFieldNames,
CascaderPanelNode,
CascaderPanelSlots,
CascaderPanelNodeSlotProps
} from '@nscode/handy-ui'
type CascaderPanelPath = (string | number)[]
type CascaderPanelValue = CascaderPanelPath | CascaderPanelPath[] | null
CascaderPanelFieldNames derives value/label/children/disabled from CollectionFieldNames. Each mapping is a field name or (item: CascaderPanelOption) => unknown.