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。