CascaderPanel 级联面板
常显的多列级联选择。单选保存完整路径,多选保存路径数组;需要输入框和弹出层时使用 Cascader。
引入
import { HuiCascaderPanel } from '@nscode/handy-ui'
import type { CascaderPanelValue, CascaderPanelExpose } from '@nscode/handy-ui'
单选与多选
不传 modelValue 时自行保存;传入属性(含 undefined)即受控。父组件拒绝回填时,选中状态保留父值。再次选择同一叶子也会通知 change。数字与字符串值严格区分。
<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: '区域甲',
children: [
{ value: 1, label: '数字 1' },
{ value: '1', label: '字符串 1' },
{ value: 2, label: '禁用项', disabled: true }
]
},
{ value: 'b', label: '区域乙' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCascaderPanel v-model="single" :options="options" aria-label="单选区域" />
<span>单选:{{ JSON.stringify(single) }}</span>
<HuiCascaderPanel v-model="multiple" :options="options" multiple aria-label="多选区域" />
<span>多选:{{ JSON.stringify(multiple) }}</span>
</HuiSpace>
</template>
默认多选父子关联,只保存可选叶子的路径。父节点的复选框联动子项,部分选中显示半选;点击父节点文字只展开。
展开方式与独立选择
trigger 支持 click、hover;checkStrictly 让父节点也能独立选择。四个方向键在列内移动或进出子列,Home/End 到当前列首尾,跳过禁用项。Enter 选择,Space 在多选时切换勾选;实际焦点留在面板根。用户插槽里的输入保留原生按键。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCascaderPanel, HuiSpace } from '@nscode/handy-ui'
const last = ref('[]')
const options = [{ value: 'a', label: '父节点', children: [{ value: 'a1', label: '叶子节点' }] }]
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="点击展开"
@expand-change="expand"
/>
<HuiCascaderPanel
:options="options"
trigger="hover"
check-strictly
aria-label="悬停与独立选择"
@expand-change="expand"
/>
</HuiSpace>
<span>最近展开路径:{{ last }}</span>
</HuiSpace>
</template>
字段映射与节点插槽
props 的四个字段均支持字段名或函数。node 插槽得到规范化节点、checked;node.rawOption 保留原始业务数据,路径和子节点结构为独立快照。
<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: '总部',
count: 12,
nodes: [
{ id: 1, name: '第一部门' },
{ id: 2, name: '停用部门', 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="业务部门">
<template #node="{ node, checked }"
><span
>{{ node.label }}{{ checked ? ' ✓' : ''
}}{{ node.rawOption.count ? `(${node.rawOption.count})` : '' }}</span
></template
>
</HuiCascaderPanel>
<span>路径:{{ JSON.stringify(value) }}</span>
</HuiSpace>
</template>
尺寸、边框、列高和空状态
size 依次读取自身、InputGroup、FormItem、Form、组件 defaults、ConfigProvider。height 是每列的实际高度,默认 300px;数字按 px,字符串可写 CSS 长度。面板过宽时横向滚动。border 默认 true。
<script setup lang="ts">
import { HuiCascaderPanel, HuiSpace } from '@nscode/handy-ui'
const sizes = ['small', 'default', 'large'] as const
const options = [
{ value: 'a', label: '可用项' },
{ value: 'b', label: '禁用项', 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}面板`"
/>
</HuiSpace>
<HuiSpace align="start" wrap>
<HuiCascaderPanel :options="options" height="9rem" disabled aria-label="禁用面板" />
<HuiCascaderPanel :options="[]" :height="140" aria-label="空面板"
><template #empty><strong>暂无可选项</strong></template></HuiCascaderPanel
>
</HuiSpace>
</HuiSpace>
</template>
方法、事件与受控父值
clearCheck() 不弹确认框。业务按钮先确认,再调用;下例在等待时保留值,取消、关闭、切换选择或卸载后不清空。清空顺序为 update:modelValue → clear → change,单选 null、多选 [],同时收起本地展开列。固定父值拒绝请求时,选中数据保持原值。
<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('等待操作')
const pending = ref(false)
const hasValue = computed(() => Array.isArray(value.value) && value.value.length > 0)
const options = [
{ value: 'a', label: '甲' },
{ value: 'b', label: '乙' }
]
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('清空当前选择并收起所有展开列?', '清空地区选择', {
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="可操作地区"
@change="record"
/>
<HuiSpace>
<HuiButton @click="panel?.focus()">聚焦</HuiButton>
<HuiButton @click="panel?.blur()">失焦</HuiButton>
<HuiButton :disabled="pending || !hasValue" @click="clear">确认后清空</HuiButton>
</HuiSpace>
<HuiCascaderPanel
:model-value="['a']"
:options="options"
aria-label="固定父值"
@change="record"
/>
<span>实际值:{{ JSON.stringify(value) }};最近请求:{{ last }}</span>
</HuiSpace>
</template>
表单组合
Form 的禁用、只读是上限,单独 disabled=false 不能放开。实际值变化触发 change 校验;焦点离开整个面板触发 blur 校验。validateEvent=false 可关闭,父拒绝选择请求不触发值校验。
<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: '甲区域' },
{ value: 'b', label: '乙区域' }
]
const rules: FormProps['rules'] = {
area: [{ required: true, message: '请选择区域', trigger: ['change', 'blur'] }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace>
<HuiButton @click="readonly = !readonly">只读:{{ readonly }}</HuiButton>
<HuiButton @click="validateEvent = !validateEvent">校验事件:{{ validateEvent }}</HuiButton>
</HuiSpace>
<HuiForm :model="model" :rules="rules" :readonly="readonly" size="small">
<HuiFormItem label="区域" prop="area" tip="可用方向键浏览"
><HuiCascaderPanel v-model="model.area" :options="options" :validate-event="validateEvent"
/></HuiFormItem>
</HuiForm>
</HuiSpace>
</template>
Props
props 的映射函数换了算法时,换 key 重建组件。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| modelValue | CascaderPanelValue | 单选 null,多选 [] | 属性存在即受控,支持 v-model |
| options | CascaderPanelOption[] | [] | 树形业务选项 |
| multiple | boolean | false | 多选路径 |
| checkStrictly | boolean | false | 父子独立选择 |
| trigger | 'click' | 'hover' | click | 子列展开方式 |
| props | CascaderPanelFieldNames | 默认同名字段 | 字段名或函数 |
| disabled | boolean | false | 禁止选择、键盘与清空 |
| size | ComponentSize | 按继承链 | small/default/large |
| border | boolean | true | 显示外框 |
| height | number | string | 300 | 每列高度 |
| validateEvent | boolean | true | 是否触发表单校验 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
| update:modelValue | CascaderPanelValue | 选择或清空请求 |
| change | CascaderPanelValue | 用户选择或方法清空;值同 update,参数为独立路径副本 |
| clear | 无 | clearCheck 已发清空请求 |
| expandChange | CascaderPanelPath | 用户操作改变展开路径;模板写 @expand-change |
| focus | FocusEvent | 焦点进入整个面板 |
| blur | FocusEvent | 焦点离开整个面板 |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
| node | { node: CascaderPanelNode, checked: boolean } | 替换节点文字,原始业务数据在 node.rawOption |
| empty | 无 | 替换空列提示 |
方法
| 方法 | 说明 |
|---|---|
| focus() | 聚焦根入口;禁用或卸载后不操作 |
| blur() | 仅让本面板内的活动元素失焦 |
| clearCheck() | 请求清空并收起展开列;禁用时不操作 |
类型
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
// FieldNames从CollectionFieldNames派生,取value/label/children/disabled。
// 每项支持字段名或(item: CascaderPanelOption) => unknown。