Skip to content
Nacre
Color palette
中文

Cascader

stableCascader · TouchCascader · TouchCascaderSheetOpen in playground
Cascader
Runs the real component in your browser.

Drills down a category tree level by level and only selects leaves; the popover has a search field at the top, and the result is the full path. Use it for fixed-depth data such as case and Notes categories.

Status: stable · Components: 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: 'Account', children: [{ value: 'open', label: 'Account opening', children: [] }, { value: 'close', label: 'Account closure', children: [] }] },
{ value: 'fund', label: 'Funds', children: [{ value: 'in', label: 'Deposit', children: [] }, { value: 'out', label: 'Withdrawal', children: [] }] },
]
export function CascaderUsage() {
const path = signal<string[]>([])
return (
<View class="w-[240px] max-w-full">
<Cascader testID="demo-cascader" label="Category" tree={categories()} value={path()} onChange={(next) => path.set(next)} placeholder="Choose a category" clearable />
</View>
)
}

TouchCascader provides a trigger. Use TouchCascaderSheet with open and onClose for a separate trigger. Selecting a leaf commits the full path.

import { View, signal } from 'nsh'
import { TouchCascader } from 'nacre'
export function CascaderTouchPicker() {
const value = signal<string[]>([])
return <View><TouchCascader testID="demo-touch-tree" label="Issue category"
tree={[
{ value: 'fund', label: 'Funds', children: [
{ value: 'in', label: 'Deposit', children: [] },
{ value: 'out', label: 'Withdrawal', children: [] },
] },
{ value: 'account', label: 'Account', children: [] },
]} value={value()} onChange={(path) => value.set(path)} clearable /></View>
}

Invalid and disabled; pair invalid with Field for the error text. When disabled it doesn’t open, and still shows the full path.

import { View } from 'nsh'
import { Cascader } from 'nacre'
import type { CascaderNode } from 'nacre'
const categories = (): CascaderNode[] => [
{ value: 'acct', label: 'Account', children: [{ value: 'open', label: 'Account opening', children: [] }, { value: 'close', label: 'Account closure', children: [] }] },
{ value: 'fund', label: 'Funds', children: [{ value: 'in', label: 'Deposit', children: [] }, { value: 'out', label: 'Withdrawal', children: [] }] },
]
export function CascaderStates() {
return (
<View class="w-[240px] max-w-full gap-2">
<Cascader testID="demo-cascader-invalid" label="Category" tree={categories()} value={[]} onChange={() => {}} placeholder="Required" invalid />
<Cascader testID="demo-cascader-disabled" label="Category" tree={categories()} value={['fund', 'in']} onChange={() => {}} disabled />
</View>
)
}

After drilling down, a breadcrumb “‹ Category / Trading / HK stocks” appears at the top; click any level to go back. Reopening lands on the level of the selected leaf.

import { View, signal } from 'nsh'
import { Cascader } from 'nacre'
import type { CascaderNode } from 'nacre'
const trade = (): CascaderNode[] => [
{
value: 'trade',
label: 'Trading',
children: [
{ value: 'hk', label: 'HK stocks', children: [{ value: 'order-failed', label: 'Order failed', children: [] }, { value: 'cancel', label: 'Cancellation failed', children: [] }] },
{ value: 'us', label: 'US stocks', children: [{ value: 'premarket', label: 'Pre-market trading', children: [] }, { value: 'fractional', label: 'Fractional shares', children: [] }] },
],
},
{ value: 'quote', label: 'Quotes', children: [{ value: 'delay', label: 'Delayed quotes', children: [] }, { value: 'level2', label: 'Level 2 access', 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="Issue type" tree={trade()} value={path()} onChange={(next) => path.set(next)} clearable />
</View>
)
}

searchText holds the names in three languages (newline-separated); search matches a leaf in any of them, and results show the full path.

import { View, signal } from 'nsh'
import { Cascader } from 'nacre'
import type { CascaderNode } from 'nacre'
const trilingual = (): CascaderNode[] => [
{
value: 'security',
label: 'Account security',
searchText: '账户安全\n帳戶安全\nAccount security',
children: [
{ value: 'password', label: 'Reset password', searchText: '重置密码\n重設密碼\nReset password', children: [] },
{ value: '2fa', label: 'Two-factor authentication', 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="Inquiry category"
tree={trilingual()}
value={path()}
onChange={(next) => path.set(next)}
placeholder="Try searching 密码"
searchPlaceholder="Search in Simplified Chinese, Traditional Chinese or English"
/>
</View>
)
}
Key Action
Space / Return Open from the trigger
↑ ↓ Move between options on the current level, wrapping at the ends
→ Go into the next level of the highlighted option (while the search field is empty)
← Go back one level, highlighting the option you came from (while the search field is empty)
Return Drill down when there are children; select a leaf, close, and return focus to the trigger
Esc Close
  • label is required: it names the trigger and list for screen readers, and is the root of the breadcrumb.
  • When clearable, a “Clear” button appears, read as “Clear”.

Do

  • Keep it to 3 levels or fewer
  • Show the full path once selected
  • Provide searchText in multilingual setups

Don’t

  • Use a cascader for flat options (use a select or combobox)
  • Allow selecting intermediate nodes

Property descriptions follow the Chinese source comments.

Prop Type Default Description
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 —

* required

Prop Type Default Description
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 —

* required

Prop Type Default Description
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 —

* required