import{o as a,a as n,y as t}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=t(`

TreeSelect

Intro

Used to select from a set of related data sets.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { TreeSelect } from 'vant';

const app = createApp();
app.use(TreeSelect);

Usage

Radio Mode

<van-tree-select
  v-model:active-id="activeId"
  v-model:main-active-index="activeIndex"
  :items="items"
/>
import { ref } from 'vue';

export default {
  setup() {
    const activeId = ref(1);
    const activeIndex = ref(0);
    const items = [
      {
        text: 'Group 1',
        children: [
          { text: 'Delaware', id: 1 },
          { text: 'Florida', id: 2 },
          { text: 'Georqia', id: 3, disabled: true },
        ],
      },
      {
        text: 'Group 2',
        children: [
          { text: 'Alabama', id: 4 },
          { text: 'Kansas', id: 5 },
          { text: 'Louisiana', id: 6 },
        ],
      },
      { text: 'Group 3', disabled: true },
    ];

    return {
      items,
      activeId,
      activeIndex,
    };
  },
};

Multiple Mode

<van-tree-select
  v-model:active-id="activeIds"
  v-model:main-active-index="activeIndex"
  :items="items"
/>
import { ref } from 'vue';

export default {
  setup() {
    const activeId = ref([1, 2]);
    const activeIndex = ref(0);
    const items = [
      {
        text: 'Group 1',
        children: [
          { text: 'Delaware', id: 1 },
          { text: 'Florida', id: 2 },
          { text: 'Georqia', id: 3, disabled: true },
        ],
      },
      {
        text: 'Group 2',
        children: [
          { text: 'Alabama', id: 4 },
          { text: 'Kansas', id: 5 },
          { text: 'Louisiana', id: 6 },
        ],
      },
      { text: 'Group 3', disabled: true },
    ];

    return {
      items,
      activeId,
      activeIndex,
    };
  },
};

Custom Content

<van-tree-select
  v-model:main-active-index="activeIndex"
  height="55vw"
  :items="items"
>
  <template #content>
    <van-image
      v-if="activeIndex === 0"
      src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg"
    />
    <van-image
      v-if="activeIndex === 1"
      src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg"
    />
  </template>
</van-tree-select>
import { ref } from 'vue';

export default {
  setup() {
    const activeIndex = ref(0);
    return {
      activeIndex,
      items: [{ text: 'Group 1' }, { text: 'Group 2' }],
    };
  },
};

Show Badge

<van-tree-select
  v-model:main-active-index="activeIndex"
  height="55vw"
  :items="items"
/>
import { ref } from 'vue';

export default {
  setup() {
    const activeIndex = ref(0);
    return {
      activeIndex,
      items: [
        {
          text: 'Group 1',
          children: [
            { text: 'Delaware', id: 1 },
            { text: 'Florida', id: 2 },
            { text: 'Georqia', id: 3, disabled: true },
          ],
          dot: true,
        },
        {
          text: 'Group 2',
          children: [
            { text: 'Alabama', id: 4 },
            { text: 'Kansas', id: 5 },
            { text: 'Louisiana', id: 6 },
          ],
          badge: 5,
        },
      ],
    };
  },
};

API

Props

AttributeDescriptionTypeDefault
itemsRequired datasets for the componentTreeSelectItem[][]
heightHeightnumber | string300
main-active-indexThe index of selected parent nodenumber | string0
active-idId of selected itemnumber | string |
(number | string)[]
0
maxMaximum number of selected itemsnumber | stringInfinity
selected-iconSelected iconstringsuccess

Events

EventDescriptionArguments
click-navEmitted when parent node is selectedindex: number
click-itemEmitted when item is selecteditem: TreeSelectChild

Slots

NameDescription
contentCustom right content

Data Structure of TreeSelectItem

TreeSelectItem should be an array contains specified tree objects.

In every tree object, text property defines id stands for the unique key while the children contains sub-tree objects.

[
  {
    // name of the parent node
    text: 'Group 1',
    // badge
    badge: 3,
    // Whether to show red dot
    dot: true,
    // ClassName of parent node
    className: 'my-class',
    // leaves of this parent node
    children: [
      {
        // name of the leaf node
        text: 'Washington',
        // id of the leaf node, component highlights leaf node by comparing the activeId with this.
        id: 1,
        // disable options
        disabled: true,
      },
      {
        text: 'Baltimore',
        id: 2,
      },
    ],
  },
];

Types

The component exports the following type definitions:

import type { TreeSelectItem, TreeSelectChild, TreeSelectProps } from 'vant';

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-tree-select-font-sizevar(--van-font-size-md)-
--van-tree-select-nav-background-colorvar(--van-background-color)-
--van-tree-select-content-background-colorvar(--van-background-color-light)-
--van-tree-select-nav-item-padding14px var(--van-padding-sm)-
--van-tree-select-item-height48px-
--van-tree-select-item-active-colorvar(--van-danger-color)-
--van-tree-select-item-disabled-colorvar(--van-gray-5)-
--van-tree-select-item-selected-size16px-
`,16),p=[l],o={__name:"README",setup(c,{expose:s}){return s({frontmatter:{}}),(d,h)=>(a(),n("div",e,p))}};export{o as default};