Cascader
Hierarchical selection from static or asynchronous data, using tabs or wheel columns. Stale descendant requests are ignored.
Import
With uni-app easycom, use hui-cascader。 Standalone Vue imports @nscode/handy-uni/web HuiCascader or installs the preview plugin.
Examples
Interaction and event results
Interact with the control. The latest event and its full payload are shown below. Popup selectors commit only after confirmation; cancellation keeps the previous value.
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
const value = ref(null)
const options = [{ value: 'a', label: '甲 / A', children: [{ value: 'a1', label: '甲一 / A1' }] }]
function event0(arg0: unknown) {
result.value = 'update:modelValue' + ': ' + JSON.stringify([arg0])
}
function event1(arg0: unknown, arg1: unknown) {
result.value = 'change' + ': ' + JSON.stringify([arg0, arg1])
}
function event2(arg0: unknown, arg1: unknown) {
result.value = 'select' + ': ' + JSON.stringify([arg0, arg1])
}
function event3(arg0: unknown) {
result.value = 'load-error' + ': ' + JSON.stringify([arg0])
}
function event4() {
result.value = 'clear' + ': ' + JSON.stringify([])
}
function event5(arg0: unknown) {
result.value = 'update:visible' + ': ' + JSON.stringify([arg0])
}
function event6() {
result.value = 'open' + ': ' + JSON.stringify([])
}
function event7() {
result.value = 'opened' + ': ' + JSON.stringify([])
}
function event8(arg0: unknown) {
result.value = 'close' + ': ' + JSON.stringify([arg0])
}
function event9() {
result.value = 'closed' + ': ' + JSON.stringify([])
}
</script>
<template>
<view>
<hui-cascader
v-model="value"
:options="options"
clearable
@update:model-value="event0"
@change="event1"
@select="event2"
@load-error="event3"
@clear="event4"
@update:visible="event5"
@open="event6"
@opened="event7"
@close="event8"
@closed="event9"
></hui-cascader>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Clear through a component ref
clearable controls the visible clear action; clear() can also be called independently. Disabled, readonly and confirmation-pending controls ignore it. Clearing resets the panel draft and emits update:modelValue, clear, then change. Controlled trigger text waits for the parent update.
<script setup lang="ts">
import { ref } from 'vue'
import type { CascaderExpose } from '@nscode/handy-uni'
const control = ref<CascaderExpose>()
const value = ref<string[] | null>(['a'])
const options = [{ value: 'a', label: 'A', isLeaf: true }]
</script>
<template>
<view>
<hui-cascader ref="control" v-model="value" :options="options" />
<hui-button @click="control?.clear()">Clear</hui-button>
<text>{{ value }}</text>
</view>
</template>
Props
| Property | Type | Default | Description |
|---|---|---|---|
model-value | CascadeKey[] | null | undefined | undefined | Controlled committed value; display remains unchanged until the parent writes back. |
default-value | CascadeKey[] | null | undefined | null | Initial uncontrolled value. |
options | CascadeOption[] | undefined | — | Candidate options or a hierarchical option tree. |
lazy | boolean | undefined | false | Load descendants asynchronously. |
load | ((parent: CascadeOption | null, level: number) => Promise<CascadeOption[]>) | undefined | — | Load children from the current parent; failed requests can be retried and stale results are ignored. |
props | CascaderFieldNames | undefined | — | Field mapping; omitted fields use standard label/value/children/disabled names. |
check-strictly | boolean | undefined | false | Disable linked checking; allow non-leaf selection in single tree or cascader selection. |
labels | string[] | undefined | — | Fallback labels while source records are unavailable, in model order. |
show-all-levels | boolean | undefined | true | Show the full path instead of its final label. |
display-format | ((selectedOptions: CascadeOption[]) => string) | undefined | — | Format trigger text from selected source records and paths. |
before-confirm | ((value: CascadeKey[] | null, context: CascaderChangeContext) => unknown) | undefined | — | Confirmation guard; false, thrown errors and rejected promises preserve the draft. |
mode | "cascader" | "wheel" | undefined | 'cascader' | Tabbed cascader lists or wheel columns. |
max-level | number | undefined | — | Maximum cascade depth; nodes at this level are leaves. |
level-titles | string[] | undefined | — | Placeholder title for each level. |
visible | boolean | undefined | undefined | Controlled popup visibility. |
default-visible | boolean | undefined | false | Initial uncontrolled visibility. |
title | string | undefined | — | Popup title. |
confirm-text | string | undefined | — | Localized confirm-button text. |
cancel-text | string | undefined | — | Localized cancel-button text. |
placeholder | string | undefined | — | Placeholder for an empty value. |
trigger | boolean | undefined | true | Render the trigger; false leaves only the popup. |
separator | string | undefined | — | Separator between trigger labels. |
clearable | boolean | undefined | false | Show clear action and emit model/change updates. |
disabled | boolean | undefined | false | Disable user interaction, including inherited form state. |
readonly | boolean | undefined | undefined | Readonly; inherit form context when omitted. |
size | ComponentSize | undefined | — | Size priority: prop → FormItem → Form → ConfigProvider. |
close-on-click-overlay | boolean | undefined | true | Request closing on overlay click. |
z-index | number | undefined | — | Explicit layer index; otherwise assigned by the page layer stack. |
root-portal | boolean | undefined | false | Use platform root-portal support on uni. |
safe-area-inset-bottom | boolean | undefined | true | Add bottom safe-area spacing. |
custom-class | string | undefined | '' | Additional root classes. |
custom-style | string | CSSProperties | undefined | — | Additional root styles. |
Events
| Event | Parameters | Description |
|---|---|---|
update:modelValue | (value: CascadeKey[] | null) | Requests a controlled value update. |
change | (value: CascadeKey[] | null, context: CascaderChangeContext) | Reports change with the arguments shown in the example. |
select | (value: CascadeKey[], context: { selectedOptions: CascadeOption[]; level: number; }) | Reports select with the arguments shown in the example. |
load-error | (info: { level: number; error: unknown; }) | Reports load-error with the arguments shown in the example. |
clear | () | Reports clear with the arguments shown in the example. |
update:visible | (visible: boolean) | Requests a controlled value update. |
open | () | Reports open with the arguments shown in the example. |
opened | () | Reports opened with the arguments shown in the example. |
close | (reason: string) | Reports close with the arguments shown in the example. |
closed | () | Reports closed with the arguments shown in the example. |
Slots
| Slot | Description |
|---|---|
trigger | Trigger scope: {text,value,open}. |
title | Title content. |
empty | Empty-state content. |
option | Option content with the original option and selection state. |
Methods
| Method | Signature |
|---|---|
open | () => void |
close | () => void |
clear | () => void |
Types
CascaderProps、CascaderEmits、CascaderExpose。 Public types are exported from the package entry.
Platform notes
| Scope | Behavior and validation limits |
|---|---|
| Shared implementation | Columns use data and explicit field mappings. Async options use cancellation and request revisions. |
| Device checks pending | Check column scrolling, retries, rapid parent changes and confirmation callbacks; do not depend on object reference equality in mini programs. |
See examples/uni-demo/smoke/Cascader.md for the checklist. Unit tests do not replace device acceptance.