TreePicker
A tree selector with result strategies, filtering and selected-path labels.
Import
With uni-app easycom, use hui-tree-picker。 Standalone Vue imports @nscode/handy-uni/web HuiTreePicker 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 data = [{ id: 1, label: '根 / Root', children: [{ id: 2, label: '子项 / Child' }] }]
const value = ref(null)
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 = 'search' + ': ' + 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-tree-picker
v-model="value"
:data="data"
filterable
clearable
@update:model-value="event0"
@change="event1"
@select="event2"
@search="event3"
@clear="event4"
@update:visible="event5"
@open="event6"
@opened="event7"
@close="event8"
@closed="event9"
></hui-tree-picker>
<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 { TreePickerExpose } from '@nscode/handy-uni'
const control = ref<TreePickerExpose>()
const value = ref<string | null>('a')
const data = [{ id: 'a', label: 'A' }]
</script>
<template>
<view>
<hui-tree-picker ref="control" v-model="value" :data="data" />
<hui-button @click="control?.clear()">Clear</hui-button>
<text>{{ value }}</text>
</view>
</template>
Props
| Property | Type | Default | Description |
|---|---|---|---|
model-value | TreePickerValue | undefined | undefined | Controlled committed value; display remains unchanged until the parent writes back. |
default-value | TreePickerValue | undefined | null | Initial uncontrolled value. |
multiple | boolean | undefined | false | Allow multiple selections. |
checked-strategy | TreePickerCheckedStrategy | undefined | 'all' | Commit all checked nodes, highest checked parents, or leaf children. |
filterable | boolean | undefined | false | Show a search input. |
filter-method | ((keyword: string, node: TreeNode) => boolean) | undefined | — | Custom filter; return true to keep an option. |
confirm-on-select | boolean | undefined | undefined | Single selection confirms immediately by default; multiple selection waits for confirmation. |
display-format | ((nodes: TreeNode[], paths: TreeNode[][]) => string) | undefined | — | Format trigger text from selected source records and paths. |
labels | string[] | undefined | — | Fallback labels while source records are unavailable, in model order. |
before-confirm | ((value: TreePickerValue, context: TreePickerChangeContext) => unknown) | undefined | — | Confirmation guard; false, thrown errors and rejected promises preserve the draft. |
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. |
data | TreeNode[] | undefined | [] | Full tree records supplied by the application. |
props | TreeFieldNames | undefined | — | Field mapping; omitted fields use standard label/value/children/disabled names. |
node-key | string | undefined | 'id' | Unique key field; numeric and string keys remain distinct. |
expanded-keys | TreeNodeKey[] | undefined | undefined | Controlled expanded keys. |
default-expanded-keys | TreeNodeKey[] | undefined | [] | Initial expanded keys. |
default-expand-all | boolean | undefined | false | Initially expand all nodes, combined with explicit initial keys. |
default-expand-level | number | undefined | 0 | Initially expanded level count; zero adds no expansion. |
current-key | TreeNodeKey | null | undefined | undefined | Current selected node key; null clears selection. |
show-checkbox | boolean | undefined | false | Show checkboxes and enable multiple selection. |
checked-keys | TreeNodeKey[] | undefined | undefined | Controlled checked keys. |
default-checked-keys | TreeNodeKey[] | undefined | [] | Initial checked keys. |
check-strictly | boolean | undefined | false | Disable linked checking; allow non-leaf selection in single tree or cascader selection. |
accordion | boolean | undefined | false | Allow one expanded sibling at a time. |
expand-on-click-node | boolean | undefined | true | Toggle non-leaf expansion on row click. |
lazy | boolean | undefined | false | Load descendants asynchronously. |
load | TreeLoad | undefined | — | Load children from the current parent; failed requests can be retried and stale results are ignored. |
indent | number | undefined | 16 | Indent per level in pixels. |
empty-text | string | undefined | — | Empty-state text, defaulting to localized common.noData. |
Events
| Event | Parameters | Description |
|---|---|---|
update:modelValue | (value: TreePickerValue) | Requests a controlled value update. |
change | (value: TreePickerValue, context: TreePickerChangeContext) | Reports change with the arguments shown in the example. |
select | (value: TreePickerValue, context: { nodes: TreeNode[]; }) | Reports select with the arguments shown in the example. |
search | (keyword: string) | Reports search 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. |
node | Tree scope: {node,level,expanded,checked,indeterminate,current,loading}. |
empty | Empty-state content. |
footer | Confirmation and cancellation area. |
Methods
| Method | Signature |
|---|---|
open | () => void |
close | () => void |
clear | () => void |
Types
TreePickerProps、TreePickerEmits、TreePickerExpose。 Public types are exported from the package entry.
Platform notes
| Scope | Behavior and validation limits |
|---|---|
| Shared implementation | Combines Tree and Popup and maintains selection drafts with shared confirm/cancel behavior. |
| Device checks pending | Check scrolling, async loading, disabled nodes and cancel rollback inside the popup; verify back keys and native stacking on devices. |
See examples/uni-demo/smoke/TreePicker.md for the checklist. Unit tests do not replace device acceptance.