Components/TreeSelect
Universal Component

TreeSelect

Tree-backed select control with single and multiple selection, search, clear, and Popover placement controls.

This page was migrated by AI, please review carefully

Migration is complete, but please validate against source code and manual review.

TreeSelect

TxTreeSelect wraps TxTree in a Popover-backed combobox. It supports single-value selection, multiple checked keys, searchable tree filtering, clear actions, custom node rendering, and imperative open/focus helpers.

Basic Usage

TreeSelect

Demo will load when visible.
<script setup lang="ts">
import { computed, ref } from 'vue'

const multiple = ref(false)
const value = ref<string | number | Array<string | number> | undefined>(undefined)

const nodes = [
  {
    key: 'general',
    label: 'General',
    children: [
      { key: 'appearance', label: 'Appearance' },
      { key: 'language', label: 'Language' },
    ],
  },
  {
    key: 'account',
    label: 'Account',
    children: [
      { key: 'profile', label: 'Profile' },
      { key: 'billing', label: 'Billing' },
    ],
  },
  {
    key: 'danger',
    label: 'Danger Zone',
    disabled: true,
    children: [
      { key: 'delete', label: 'Delete account', disabled: true },
    ],
  },
]

const placeholder = computed(() => (multiple.value ? 'Select multiple' : 'Select one'))
</script>

<template>
  <div style="display: flex; flex-direction: column; gap: 12px; width: 420px;">
    <div style="display: flex; gap: 8px; align-items: center;">
      <TxButton size="small" variant="secondary" @click="multiple = !multiple">
        Toggle multiple
      </TxButton>
      <TxButton size="small" variant="secondary" @click="value = multiple ? [] : undefined">
        Clear
      </TxButton>
    </div>

    <TxTreeSelect
      v-model="value"
      :nodes="nodes"
      :multiple="multiple"
      :placeholder="placeholder"
      :dropdown-max-height="260"
    />

    <div style="color: var(--tx-text-color-secondary); font-size: 12px;">
      value: {{ value }}
    </div>
  </div>
</template>

Notes

  • When multiple is true, modelValue must be an array; clearing returns an empty array.
  • When multiple is false, clearing returns undefined.

API

TxTreeSelect Props

PropTypeDefaultDescription
modelValuestring | number | Array<string | number> | undefined-Selected key in single mode, or selected key array in multiple mode.
nodesTreeSelectNode[][]Tree nodes displayed in the dropdown.
multiplebooleanfalseEnables checkbox-style multi-select and tag display in the trigger.
disabledbooleanfalseDisables the combobox trigger, tree interaction, and clear action.
placeholderstring'请选择'Placeholder shown when nothing is selected.
searchablebooleantrueShows a search input and passes its text to TxTree filtering.
clearablebooleantrueShows a clear button when there is a selected key.
placementPopoverPlacement'bottom-start'Popover placement for the dropdown panel.
dropdownOffsetnumber6Popover offset from the trigger.
dropdownWidthnumber0Explicit Popover panel width; 0 leaves width resolution to the Popover.
dropdownMaxWidthnumber480Maximum Popover panel width.
dropdownMaxHeightnumber320Maximum tree panel height in pixels.
defaultExpandedKeysArray<string | number>[]Tree node keys expanded on initial render.

TreeSelectNode

FieldTypeDescription
keystring | numberStable node key used as the selected value.
labelstringVisible node label and trigger display text.
disabledbooleanDisables selecting this node.
childrenTreeSelectNode[]Optional nested child nodes.

Events

EventParamsDescription
update:modelValue(v: TreeSelectValue)v-model update emitted after tree selection or clear.
change(v: TreeSelectValue)Emitted with the same value after selection or clear.
open-Emitted when the Popover dropdown opens.
close-Emitted when the Popover dropdown closes.

Slots

NameDescription
nodeCustom node label content. Receives { node, level, expanded, selected }.

Expose

NameTypeDescription
open()() => voidOpens the dropdown.
close()() => voidCloses the dropdown.
toggle()() => voidToggles the dropdown open state.
focus()() => voidFocuses the combobox trigger.
blur()() => voidBlurs the combobox trigger.
clear()() => voidClears to undefined in single mode or [] in multiple mode.
setValue(v)(v: TreeSelectValue) => voidEmits the supplied value through update:modelValue and change.
getValue()() => TreeSelectValueReturns the current modelValue.
getCheckedKeys()() => Array<string | number>Returns the currently selected keys as an array.