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

Cascader

Intro

The cascader component is used for the selection of multi-level data. The typical scene is the selection of provinces and cities.

Install

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

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

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

Usage

Basic Usage

<van-field
  v-model="fieldValue"
  is-link
  readonly
  label="Area"
  placeholder="Select Area"
  @click="show = true"
/>
<van-popup v-model="show" round position="bottom">
  <van-cascader
    v-model="cascaderValue"
    title="Select Area"
    :options="options"
    @close="show = false"
    @finish="onFinish"
  />
</van-popup>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const fieldValue = ref('');
    const cascaderValue = ref('');
    const options = [
      {
        text: 'Zhejiang',
        value: '330000',
        children: [{ text: 'Hangzhou', value: '330100' }],
      },
      {
        text: 'Jiangsu',
        value: '320000',
        children: [{ text: 'Nanjing', value: '320100' }],
      },
    ];
    const onFinish = ({ selectedOptions }) => {
      show.value = false;
      fieldValue.value = selectedOptions.map((option) => option.text).join('/');
    };

    return {
      show,
      options,
      onFinish,
      fieldValue,
      cascaderValue,
    };
  },
};

Custom Color

<van-cascader
  v-model="cascaderValue"
  title="Select Area"
  :options="options"
  active-color="#1989fa"
  @close="show = false"
  @finish="onFinish"
/>

Async Options

<van-field
  v-model="fieldValue"
  is-link
  readonly
  label="Area"
  placeholder="Select Area"
  @click="show = true"
/>
<van-popup v-model="show" round position="bottom">
  <van-cascader
    v-model="cascaderValue"
    title="Select Area"
    :options="options"
    @close="show = false"
    @change="onChange"
    @finish="onFinish"
  />
</van-popup>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const fieldValue = ref('');
    const cascaderValue = ref('');
    const options = ref([
      {
        text: 'Zhejiang',
        value: '330000',
        children: [],
      },
    ]);
    const onChange = ({ value }) => {
      if (value === options.value[0].value) {
        setTimeout(() => {
          options.value[0].children = [
            { text: 'Hangzhou', value: '330100' },
            { text: 'Ningbo', value: '330200' },
          ];
        }, 500);
      }
    };
    const onFinish = ({ selectedOptions }) => {
      show.value = false;
      fieldValue.value = selectedOptions.map((option) => option.text).join('/');
    };

    return {
      show,
      options,
      onFinish,
      fieldValue,
      cascaderValue,
    };
  },
};

Custom Field Names

<van-cascader
  v-model="code"
  title="Select Area"
  :options="options"
  :field-names="fieldNames"
/>
import { ref } from 'vue';

export default {
  setup() {
    const code = ref('');
    const fieldNames = {
      text: 'name',
      value: 'code',
      children: 'items',
    };
    const options = [
      {
        name: 'Zhejiang',
        code: '330000',
        items: [{ name: 'Hangzhou', code: '330100' }],
      },
      {
        name: 'Jiangsu',
        code: '320000',
        items: [{ name: 'Nanjing', code: '320100' }],
      },
    ];

    return {
      code,
      options,
      fieldNames,
    };
  },
};

Custom Content

<van-cascader v-model="code" title="Select Area" :options="options">
  <template #options-top="{ tabIndex }">
    <div class="current-level">Current level is {{ tabIndex }}</div>
  </template>
</van-cascader>

<style>
  .current-level {
    padding: 10px 16px 0;
  }
</style>
import { ref } from 'vue';

export default {
  setup() {
    const code = ref('');
    const options = [
      {
        name: 'Zhejiang',
        code: '330000',
        items: [{ name: 'Hangzhou', code: '330100' }],
      },
      {
        name: 'Jiangsu',
        code: '320000',
        items: [{ name: 'Nanjing', code: '320100' }],
      },
    ];

    return {
      code,
      options,
    };
  },
};

API

Props

AttributeDescriptionTypeDefault
titleTitlestring-
valueValue of selected optionstring | number-
optionsOptionsCascaderOption[][]
placeholderPlaceholder of unselected tabstringSelect
active-colorActive colorstring#ee0a24
swipeable v3.0.11Whether to enable gestures to slide left and rightbooleanfalse
closeableWhether to show close iconbooleantrue
show-header v3.4.2Whether to show headerbooleantrue
close-icon v3.0.10Close icon namestringcross
field-names v3.0.4Custom the fields of optionsobject{ text: 'text', value: 'value', children: 'children' }

Data Structure of CascaderOption

KeyDescriptionType
textOption textstring
valueOption valuestring | number
color v3.1.0Text colorstring
childrenCascade childrenCascaderOption[]
disabled v3.1.2Whether to disable optionboolean
className v3.1.0className for the optionstring | Array | object

Events

EventDescriptionArguments
changeEmitted when active option changed{ value: string | number, selectedOptions: CascaderOption[], tabIndex: number }
finishEmitted when all options is selected{ value: string | number, selectedOptions: CascaderOption[], tabIndex: number }
closeEmitted when the close icon is clicked-
click-tabEmitted when a tab is clickedactiveTab: number, title: string

Slots

NameDescriptionSlotProps
titleCustom title-
option v3.1.4Custom option text{ option: CascaderOption, selected: boolean }
options-top v3.2.7Custom the content above the options{ tabIndex: number }
options-bottom v3.2.8Custom the content below the options{ tabIndex: number }

Types

The component exports the following type definitions:

import type { CascaderProps, CascaderOption, CascaderFieldNames } 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-cascader-header-height48px-
--van-cascader-header-padding0 var(--van-padding-md)-
--van-cascader-title-font-sizevar(--van-font-size-lg)-
--van-cascader-title-line-height20px-
--van-cascader-close-icon-size22px-
--van-cascader-close-icon-colorvar(--van-gray-5)-
--van-cascader-selected-icon-size18px-
--van-cascader-tabs-height48px-
--van-cascader-active-colorvar(--van-danger-color)-
--van-cascader-options-height384px-
--van-cascader-tab-colorvar(--van-text-color)-
--van-cascader-unselected-tab-colorvar(--van-text-color-2)-
`,17),p=[e],i={__name:"README",setup(c,{expose:s}){return s({frontmatter:{}}),(d,r)=>(a(),n("div",l,p))}};export{i as default};