树形分类逐级下钻,只能选到叶子;浮层顶部可搜索,结果是整条路径。用在客诉分类、小记分类这类固定层级的数据上。
状态:stable · 组件:Cascader、TouchCascader、TouchCascaderSheet
引入
定位到「引入」import { Cascader, TouchCascader, TouchCascaderSheet } from 'nacre'用法
定位到「用法」import { View, signal } from 'nsh'import { Cascader } from 'nacre'import type { CascaderNode } from 'nacre'
const categories = (): CascaderNode[] => [ { value: 'acct', label: '账户', children: [{ value: 'open', label: '开户', children: [] }, { value: 'close', label: '销户', children: [] }] }, { value: 'fund', label: '资金', children: [{ value: 'in', label: '入金', children: [] }, { value: 'out', label: '出金', children: [] }] },]
export function CascaderUsage() { const path = signal<string[]>([]) return ( <View class="w-[240px] max-w-full"> <Cascader testID="demo-cascader" label="分类" tree={categories()} value={path()} onChange={(next) => path.set(next)} placeholder="请选择分类" clearable /> </View> )}示例
定位到「示例」手机逐级选择
定位到「手机逐级选择」TouchCascader 自带触发器。已有入口的页面可用 TouchCascaderSheet 控制 open 和 onClose,点叶子提交完整路径。
import { View, signal } from 'nsh'import { TouchCascader } from 'nacre'
export function CascaderTouchPicker() { const value = signal<string[]>([]) return <View><TouchCascader testID="demo-touch-tree" label="问题分类" tree={[ { value: 'fund', label: '资金', children: [ { value: 'in', label: '入金', children: [] }, { value: 'out', label: '出金', children: [] }, ] }, { value: 'account', label: '账户', children: [] }, ]} value={value()} onChange={(path) => value.set(path)} clearable /></View>}状态
定位到「状态」出错(invalid)与禁用;出错时配 Field 给错误文字。禁用时不弹出,仍显示完整路径。
import { View } from 'nsh'import { Cascader } from 'nacre'import type { CascaderNode } from 'nacre'
const categories = (): CascaderNode[] => [ { value: 'acct', label: '账户', children: [{ value: 'open', label: '开户', children: [] }, { value: 'close', label: '销户', children: [] }] }, { value: 'fund', label: '资金', children: [{ value: 'in', label: '入金', children: [] }, { value: 'out', label: '出金', children: [] }] },]
export function CascaderStates() { return ( <View class="w-[240px] max-w-full gap-2"> <Cascader testID="demo-cascader-invalid" label="分类" tree={categories()} value={[]} onChange={() => {}} placeholder="必填未选" invalid /> <Cascader testID="demo-cascader-disabled" label="分类" tree={categories()} value={['fund', 'in']} onChange={() => {}} disabled /> </View> )}三级与面包屑
定位到「三级与面包屑」下钻后顶部出面包屑「‹ 分类 / 交易 / 港股」,点任一级回退;重新打开时停在已选叶子所在的那一级。
import { View, signal } from 'nsh'import { Cascader } from 'nacre'import type { CascaderNode } from 'nacre'
const trade = (): CascaderNode[] => [ { value: 'trade', label: '交易', children: [ { value: 'hk', label: '港股', children: [{ value: 'order-failed', label: '下单失败', children: [] }, { value: 'cancel', label: '撤单未成功', children: [] }] }, { value: 'us', label: '美股', children: [{ value: 'premarket', label: '盘前交易', children: [] }, { value: 'fractional', label: '碎股', children: [] }] }, ], }, { value: 'quote', label: '行情', children: [{ value: 'delay', label: '行情延迟', children: [] }, { value: 'level2', label: 'Level 2 权限', children: [] }] },]
export function CascaderDeep() { const path = signal<string[]>(['trade', 'hk', 'order-failed']) return ( <View class="w-[280px] max-w-full"> <Cascader testID="example-cascader-trade" label="问题类型" tree={trade()} value={path()} onChange={(next) => path.set(next)} clearable /> </View> )}多语搜索
定位到「多语搜索」searchText 放三语名称(换行分隔),搜索命中任一语言的叶子,结果显示整条路径。
import { View, signal } from 'nsh'import { Cascader } from 'nacre'import type { CascaderNode } from 'nacre'
const trilingual = (): CascaderNode[] => [ { value: 'security', label: '账户安全', searchText: '账户安全\n帳戶安全\nAccount security', children: [ { value: 'password', label: '重置密码', searchText: '重置密码\n重設密碼\nReset password', children: [] }, { value: '2fa', label: '两步验证', searchText: '两步验证\n兩步驗證\nTwo-factor authentication', children: [] }, ], },]
export function CascaderSearchText() { const path = signal<string[]>([]) return ( <View class="w-[280px] max-w-full"> <Cascader testID="example-cascader-trilingual" label="咨询分类" tree={trilingual()} value={path()} onChange={(next) => path.set(next)} placeholder="试试搜 password" searchPlaceholder="搜索中文、繁体或英文" /> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Space / Return | 在触发器上展开 |
| ↑ ↓ | 在当前层的选项间移动,到头回绕 |
| → | 进入高亮项的下一级(搜索框为空时) |
| ← | 返回上一级,高亮刚才进入的那一项(搜索框为空时) |
| Return | 有子级时下钻;叶子选中并收起,焦点回到触发器 |
| Esc | 收起 |
无障碍
定位到「无障碍」- label 必填:是触发器与列表的读屏名,也是面包屑的根。
- 可清空时出「清除」钮,读屏名是「清除」。
该用 / 不该用
定位到「该用 / 不该用」该用
- 层级 ≤ 3
- 选中后显示完整路径
- 多语环境给 searchText
不该用
- 平铺的选项用级联(用选择框或组合框)
- 允许选中间节点
API
定位到「API」Cascader
定位到「Cascader」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
testID * |
string |
— | |
label * |
string |
— | 字段名:无障碍名、面包屑根(「‹ 分类」) |
tree * |
readonly CascaderNode[] |
— | |
value * |
readonly string[] |
— | 已选叶子的完整路径(从根到叶的 value) |
onChange * |
(path: string[]) => void |
— | 只在选中叶子时回调;clearable 时清除回调 [] |
placeholder |
string |
— | |
searchPlaceholder |
string |
— | |
emptyText |
string |
— | 树为空时的提示 |
invalid |
boolean |
— | |
disabled |
boolean |
— | |
clearable |
boolean |
— |
* 必填
TouchCascader
定位到「TouchCascader」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
testID * |
string |
— | |
label * |
string |
— | 字段名:无障碍名、面包屑根(「‹ 分类」) |
tree * |
readonly CascaderNode[] |
— | |
value * |
readonly string[] |
— | 已选叶子的完整路径(从根到叶的 value) |
onChange * |
(path: string[]) => void |
— | 只在选中叶子时回调;clearable 时清除回调 [] |
placeholder |
string |
— | |
searchPlaceholder |
string |
— | |
emptyText |
string |
— | 树为空时的提示 |
invalid |
boolean |
— | |
disabled |
boolean |
— | |
clearable |
boolean |
— |
* 必填
TouchCascaderSheet
定位到「TouchCascaderSheet」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
testID * |
string |
— | |
label * |
string |
— | 字段名:无障碍名、面包屑根(「‹ 分类」) |
tree * |
readonly CascaderNode[] |
— | |
value * |
readonly string[] |
— | 已选叶子的完整路径(从根到叶的 value) |
onChange * |
(path: string[]) => void |
— | 只在选中叶子时回调;clearable 时清除回调 [] |
placeholder |
string |
— | |
searchPlaceholder |
string |
— | |
emptyText |
string |
— | 树为空时的提示 |
invalid |
boolean |
— | |
disabled |
boolean |
— | |
clearable |
boolean |
— | |
open * |
boolean |
— | |
onClose * |
() => void |
— |
* 必填