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

Picker

Intro

The picker component is usually used with Popup Component.

Install

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

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

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

Usage

Basic Usage

<van-picker
  title="Title"
  :columns="columns"
  @confirm="onConfirm"
  @cancel="onCancel"
  @change="onChange"
/>
import { Toast } from 'vant';

export default {
  setup() {
    const columns = ['Delaware', 'Florida', 'Georqia', 'Indiana', 'Maine'];

    const onConfirm = (value, index) => {
      Toast(\`Value: \${value}, Index: \${index}\`);
    };
    const onChange = (value, index) => {
      Toast(\`Value: \${value}, Index: \${index}\`);
    };
    const onCancel = () => Toast('Cancel');

    return {
      columns,
      onChange,
      onCancel,
      onConfirm,
    };
  },
};

Default Index

<van-picker title="Title" :columns="columns" :default-index="2" />

Multiple Columns

<van-picker title="Title" :columns="columns" />
export default {
  setup() {
    const columns = [
      {
        values: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
        defaultIndex: 2,
      },
      {
        values: ['Morning', 'Afternoon', 'Evening'],
        defaultIndex: 1,
      },
    ];

    return { columns };
  },
};

Cascade

<van-picker title="Title" :columns="columns" />
export default {
  setup() {
    const columns = [
      {
        text: 'Zhejiang',
        children: [
          {
            text: 'Hangzhou',
            children: [{ text: 'Xihu' }, { text: 'Yuhang' }],
          },
          {
            text: 'Wenzhou',
            children: [{ text: 'Lucheng' }, { text: 'Ouhai' }],
          },
        ],
      },
      {
        text: 'Fujian',
        children: [
          {
            text: 'Fuzhou',
            children: [{ text: 'Gulou' }, { text: 'Taijiang' }],
          },
          {
            text: 'Xiamen',
            children: [{ text: 'Siming' }, { text: 'Haicang' }],
          },
        ],
      },
    ];

    return { columns };
  },
};

Disable option

<van-picker :columns="columns" />
export default {
  setup() {
    const columns = [
      { text: 'Delaware', disabled: true },
      { text: 'Florida' },
      { text: 'Georqia' },
    ];

    return { columns };
  },
};

Set Column Values

<van-picker ref="picker" title="Title" :columns="columns" @change="onChange" />
import { ref } from 'vue';

export default {
  setup() {
    const picker = ref(null);

    const states = {
      Group1: ['Delaware', 'Florida', 'Georqia', 'Indiana', 'Maine'],
      Group2: ['Alabama', 'Kansas', 'Louisiana', 'Texas'],
    };
    const columns = [
      { values: Object.keys(states) },
      { values: states.Group1 },
    ];

    const onChange = (values) => {
      picker.value.setColumnValues(1, states[values[0]]);
    };

    return {
      picker,
      columns,
      onChange,
    };
  },
};

Loading

When Picker columns data is acquired asynchronously, use loading prop to show loading prompt.

<van-picker title="Title" :columns="columns" :loading="loading" />
import { ref } from 'vue';

export default {
  setup() {
    const columns = ref([]);
    const loading = ref(true);

    setTimeout(() => {
      columns.value = ['Option'];
      loading.value = false;
    }, 1000);

    return { columns, loading };
  },
};

With Popup

<van-field
  v-model="result"
  is-link
  readonly
  label="City"
  placeholder="Choose City"
  @click="showPicker = true"
/>
<van-popup v-model:show="showPicker" round position="bottom">
  <van-picker
    title="Title"
    :columns="columns"
    @cancel="showPicker = false"
    @confirm="onConfirm"
  />
</van-popup>
import { ref } from 'vue';

export default {
  setup() {
    const columns = ['Delaware', 'Florida', 'Georqia', 'Indiana', 'Maine'];
    const result = ref('');
    const showPicker = ref(false);

    const onConfirm = (value) => {
      result.value = value;
      showPicker.value = false;
    };

    return {
      result,
      columns,
      onConfirm,
      showPicker,
    };
  },
};

Custom Columns Field

<van-picker
  :title="Title"
  :columns="columns"
  :columns-field-names="customFieldName"
/>
export default {
  setup() {
    const columns = [
      {
        cityName: 'Zhejiang',
        cities: [
          {
            cityName: 'Hangzhou',
            cities: [{ cityName: 'Xihu' }, { cityName: 'Yuhang' }],
          },
          {
            cityName: 'Wenzhou',
            cities: [{ cityName: 'Lucheng' }, { cityName: 'Ouhai' }],
          },
        ],
      },
      {
        cityName: 'Fujian',
        cities: [
          {
            cityName: 'Fuzhou',
            cities: [{ cityName: 'Gulou' }, { cityName: 'Taijiang' }],
          },
          {
            cityName: 'Xiamen',
            cities: [{ cityName: 'Siming' }, { cityName: 'Haicang' }],
          },
        ],
      },
    ];

    const customFieldName = {
      text: 'cityName',
      children: 'cities',
    };

    return {
      columns,
      customFieldName,
    };
  },
};

API

Props

AttributeDescriptionTypeDefault
columnsColumns dataColumn[][]
columns-field-namescustom columns fieldobject{ text: 'text', values: 'values', children: 'children' }
titleToolbar titlestring-
confirm-button-textText of confirm buttonstringConfirm
cancel-button-textText of cancel buttonstringCancel
toolbar-positionToolbar position, cat be set to bottomstringtop
loadingWhether to show loading promptbooleanfalse
show-toolbarWhether to show toolbarbooleantrue
allow-htmlWhether to allow HTML in option textbooleanfalse
default-indexDefault value index of single column pickernumber | string0
item-heightOption height, supports px vw vh rem unit, default pxnumber | string44
visible-item-countCount of visible columnsnumber | string6
swipe-durationDuration of the momentum animation, unit msnumber | string1000

Events

Picker events will pass different parameters according to the columns are single or multiple

EventDescriptionArguments
confirmEmitted when click confirm button.
Notice: the arguments return an array when the multiple columns mode.
currentValue: PickerOption | PickerOption[], currentIndex: number | number[]
cancelEmitted when click cancel button.
Notice: the arguments return an array when the multiple columns mode.
currentValue: PickerOption | PickerOption[], currentIndex: number | number[]
changeEmitted when current option changed.
Notice: the arguments return an array when the multiple columns mode.
currentValue: PickerOption | PickerOption[], currentIndex: number | number[]

Slots

NameDescriptionSlotProps
toolbar 3.1.2Custom toolbar content-
titleCustom title-
confirmCustom confirm button text-
cancelCustom cancel button text-
optionCustom option contentoption: string | object
columns-topCustom content above columns-
columns-bottomCustom content below columns-

Data Structure of Column

KeyDescriptionType
valuesValue of columnArray<string | number>
defaultIndexDefault value indexnumber
classNameClassName for this columnstring | Array | object
childrenCascade childrenColumn

Methods

Use ref to get Picker instance and call instance methods.

NameDescriptionAttributeReturn value
getValuesGet current values of all columns-values
setValuesSet current values of all columnsvalues-
getIndexesGet current indexes of all columns-indexes
setIndexesSet current indexes of all columnsindexes-
getColumnValueGet current value of the columncolumnIndexvalue
setColumnValueSet current value of the columncolumnIndex, value-
getColumnIndexGet current index of the columncolumnIndexoptionIndex
setColumnIndexSet current index of the columncolumnIndex, optionIndex-
getColumnValuesGet columns data of the columncolumnIndexvalues
setColumnValuesSet columns data of the columncolumnIndex, values-
confirmStop scrolling and emit confirm event--

Types

The component exports the following type definitions:

import type {
  PickerProps,
  PickerColumn,
  PickerOption,
  PickerInstance,
  PickerFieldNames,
  PickerObjectColumn,
  PickerObjectOption,
  PickerToolbarPosition,
} from 'vant';

PickerInstance is the type of component instance:

import { ref } from 'vue';
import type { PickerInstance } from 'vant';

const pickerRef = ref<PickerInstance>();

pickerRef.value?.confirm();

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-picker-background-colorvar(--van-background-color-light)-
--van-picker-toolbar-height44px-
--van-picker-title-font-sizevar(--van-font-size-lg)-
--van-picker-title-line-heightvar(--van-line-height-md)-
--van-picker-action-padding0 var(--van-padding-md)-
--van-picker-action-font-sizevar(--van-font-size-md)-
--van-picker-confirm-action-colorvar(--van-text-link-color)-
--van-picker-cancel-action-colorvar(--van-text-color-2)-
--van-picker-option-padding0 var(--van-padding-base)-
--van-picker-option-font-sizevar(--van-font-size-lg)-
--van-picker-option-text-colorvar(--van-text-color)-
--van-picker-option-disabled-opacity0.3-
--van-picker-mask-colorlinear-gradient-
--van-picker-loading-icon-colorvar(--van-primary-color)-
--van-picker-loading-mask-colorrgba(255, 255, 255, 0.9)-
`,22),p=[e],h={__name:"README",setup(c,{expose:s}){return s({frontmatter:{}}),(d,o)=>(a(),n("div",l,p))}};export{h as default};