/*! For license information please see 6517.ccd9b797.js.LICENSE.txt */ (self.webpackChunk=self.webpackChunk||[]).push([["6517"],{51562:function(s,n,a){"use strict";a.r(n);var t=a("80681");let l=["innerHTML"];n.default={setup:()=>({html:""}),render:()=>((0,t.wg)(),(0,t.iD)("div",{class:"van-doc-markdown-body",innerHTML:'

Picker

\n

Intro

\n

The picker component is usually used with Popup Component.

\n

Install

\n

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

\n
import { createApp } from 'vue';\nimport { Picker } from 'vant';\n\nconst app = createApp();\napp.use(Picker);\n
\n

Usage

\n

Basic Usage

\n
<van-picker\n  title="Title"\n  :columns="columns"\n  @confirm="onConfirm"\n  @cancel="onCancel"\n  @change="onChange"\n/>\n
\n
import { showToast } from 'vant';\n\nexport default {\n  setup() {\n    const columns = [\n      { text: 'Delaware', value: 'Delaware' },\n      { text: 'Florida', value: 'Florida' },\n      { text: 'Wenzhou', value: 'Wenzhou' },\n      { text: 'Indiana', value: 'Indiana' },\n      { text: 'Maine', value: 'Maine' },\n    ];\n    const onConfirm = ({ selectedValues }) => {\n      showToast(`Value: ${selectedValues.join(',')}`);\n    };\n    const onChange = ({ selectedValues }) => {\n      showToast(`Value: ${selectedValues.join(',')}`);\n    };\n    const onCancel = () => showToast('Cancel');\n\n    return {\n      columns,\n      onChange,\n      onCancel,\n      onConfirm,\n    };\n  },\n};\n
\n

With Popup

\n
<van-field\n  v-model="fieldValue"\n  is-link\n  readonly\n  label="City"\n  placeholder="Choose City"\n  @click="showPicker = true"\n/>\n<van-popup v-model:show="showPicker" round position="bottom">\n  <van-picker\n    title="Title"\n    :columns="columns"\n    @cancel="showPicker = false"\n    @confirm="onConfirm"\n  />\n</van-popup>\n
\n
import { ref } from 'vue';\n\nexport default {\n  setup() {\n    const columns = [\n      { text: 'Delaware', value: 'Delaware' },\n      { text: 'Florida', value: 'Florida' },\n      { text: 'Wenzhou', value: 'Wenzhou' },\n      { text: 'Indiana', value: 'Indiana' },\n      { text: 'Maine', value: 'Maine' },\n    ];\n    const fieldValue = ref('');\n    const showPicker = ref(false);\n\n    const onConfirm = ({ selectedOptions }) => {\n      showPicker.value = false;\n      fieldValue.value = selectedOptions[0].text;\n    };\n\n    return {\n      columns,\n      onConfirm,\n      fieldValue,\n      showPicker,\n    };\n  },\n};\n
\n

v-model

\n

Using v-model to bind selected values.

\n
<van-picker v-model="selectedValues" title="Title" :columns="columns" />\n
\n
import { showToast } from 'vant';\n\nexport default {\n  setup() {\n    const columns = [\n      { text: 'Delaware', value: 'Delaware' },\n      { text: 'Florida', value: 'Florida' },\n      { text: 'Wenzhou', value: 'Wenzhou' },\n      { text: 'Indiana', value: 'Indiana' },\n      { text: 'Maine', value: 'Maine' },\n    ];\n    const selectedValues = ref(['Wenzhou']);\n\n    return {\n      columns,\n      selectedValues,\n    };\n  },\n};\n
\n

Multiple Columns

\n
<van-picker title="Title" :columns="columns" />\n
\n
export default {\n  setup() {\n    const columns = [\n      [\n        { text: 'Monday', value: 'Monday' },\n        { text: 'Tuesday', value: 'Tuesday' },\n        { text: 'Wednesday', value: 'Wednesday' },\n        { text: 'Thursday', value: 'Thursday' },\n        { text: 'Friday', value: 'Friday' },\n      ],\n      [\n        { text: 'Morning', value: 'Morning' },\n        { text: 'Afternoon', value: 'Afternoon' },\n        { text: 'Evening', value: 'Evening' },\n      ],\n    ];\n\n    return { columns };\n  },\n};\n
\n

Cascade

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

Disable option

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

Loading

\n

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

\n
<van-picker title="Title" :columns="columns" :loading="loading" />\n
\n
import { ref } from 'vue';\n\nexport default {\n  setup() {\n    const columns = ref([]);\n    const loading = ref(true);\n\n    setTimeout(() => {\n      columns.value = [{ text: 'Option', value: 'option' }];\n      loading.value = false;\n    }, 1000);\n\n    return { columns, loading };\n  },\n};\n
\n

Custom Columns Field

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

API

\n

Props

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionTypeDefault
v-modelvalues of chosen optionnumber[] | string[]-
columnsColumns dataPickerOption[] | PickerOption[][][]
columns-field-namescustom columns fieldobject{ text: \'text\', value: \'value\', children: \'children\' }
titleToolbar titlestring-
confirm-button-textText of confirm button, setting it as an empty string can hide the buttonstringConfirm
cancel-button-textText of cancel button, setting it as an empty string can hide the buttonstringCancel
toolbar-positionToolbar position, cat be set to bottomstringtop
loadingWhether to show loading promptbooleanfalse
readonlyWhether to be readonlybooleanfalse
show-toolbarWhether to show toolbarbooleantrue
allow-htmlWhether to allow HTML in option textbooleanfalse
option-heightOption height, supports px vw vh rem unit, default pxnumber | string44
visible-option-numCount of visible columnsnumber | string6
swipe-durationDuration of the momentum animation, unit msnumber | string1000
\n

Events

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
EventDescriptionArguments
confirmEmitted when the confirm button is clicked{ selectedValues, selectedOptions, selectedIndexes }
cancelEmitted when the cancel button is clicked{ selectedValues, selectedOptions, selectedIndexes }
changeEmitted when current selected option is changed{ selectedValues, selectedOptions,selectedIndexes, columnIndex }
click-optionEmitted when an option is clicked{ currentOption, selectedValues, selectedOptions, selectedIndexes, columnIndex }
scroll-into v4.2.1Emitted when an option is scrolled into the middle selection area by clicking or dragging{ currentOption, columnIndex }
\n

Slots

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionSlotProps
toolbarCustom toolbar content-
titleCustom title-
confirmCustom confirm button text-
cancelCustom cancel button text-
optionCustom option contentoption: PickerOption, index: number
columns-topCustom content above columns-
columns-bottomCustom content below columns-
\n

Data Structure of PickerOption

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
KeyDescriptionType
textTextstring | number
valueValue of optionstring | number
disabledWhether to disable optionboolean
childrenCascade children optionsPickerOption[]
classNameClassName for this optionstring | Array | object
\n

Methods

\n

Use ref to get Picker instance and call instance methods.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionAttributeReturn value
confirmStop scrolling and emit confirm event--
getSelectedOptionsGet current selected options-(PickerOption | undefined)[]
\n

Types

\n

The component exports the following type definitions:

\n
import type {\n  PickerProps,\n  PickerColumn,\n  PickerOption,\n  PickerInstance,\n  PickerFieldNames,\n  PickerToolbarPosition,\n  PickerCancelEventParams,\n  PickerChangeEventParams,\n  PickerConfirmEventParams,\n} from 'vant';\n
\n

PickerInstance is the type of component instance:

\n
import { ref } from 'vue';\nimport type { PickerInstance } from 'vant';\n\nconst pickerRef = ref<PickerInstance>();\n\npickerRef.value?.confirm();\n
\n

Theming

\n

CSS Variables

\n

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

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDefault ValueDescription
--van-picker-backgroundvar(--van-background-2)-
--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-primary-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)-
\n
'},null,8,l))}}}]);