Skip to content

Picker Component ​

This picker is used for single-column, multi-column, and multi-column linkage selection scenarios.

📌 Platform Differences ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

⚠️ Notes ​

Notes

  • When hasInput is true, the picker opens by clicking the input box, so there is no need to set the show property
  • modelValue is a string or number in single-column mode, and an array in multi-column mode
  • Multi-column linkage requires calling the setColumnValues method in the change event to update the data of subsequent columns
  • The columns parameter supports a one-dimensional array (single column) or a two-dimensional array (multiple columns)
  • popupMode currently only supports two modes: bottom and top

🏯 Basic Usage Examples ​

Basic Usage (Single Column Mode) ​

html
<template>
    <hy-picker :show="show" :columns="columns" @confirm="onConfirm"></hy-picker>
    <hy-cell clickable @click="show = true">
        <hy-cell-item title="Open Picker" :value="value"></hy-cell-item>
    </hy-cell>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const show = ref(false);
    const value = ref('');
    const columns = reactive([['China', 'USA', 'Japan', 'Korea']]);

    const onConfirm = (e) => {
        value.value = e.value.join('');
        show.value = false;
    };
</script>

Open via Input Box ​

html
<template>
    <hy-picker
        v-model="value"
        has-input
        :columns="columns"
        :input="{ placeholder: 'Please select a country' }"
        @confirm="onConfirm"
    ></hy-picker>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const value = ref('');
    const columns = reactive([['China', 'USA', 'Japan']]);

    const onConfirm = (e) => {
        console.log('Selected:', e.value);
    };
</script>

Multi-Column Mode ​

html
<template>
    <hy-picker
        v-model="value"
        has-input
        :columns="columns"
        separator="-"
        @confirm="onConfirm"
    ></hy-picker>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const value = ref([]);
    const columns = reactive([
        ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
        ['Morning', 'Afternoon', 'Evening'],
        ['9:00', '10:00', '11:00', '14:00', '15:00', '16:00'],
    ]);

    const onConfirm = (e) => {
        console.log('Selected:', e.value);
    };
</script>

Multi-Column Linkage ​

html
<template>
    <hy-picker
        v-model="value"
        ref="pickerRef"
        has-input
        :columns="columns"
        @change="onChange"
        @confirm="onConfirm"
    ></hy-picker>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const pickerRef = ref(null);
    const value = ref([]);

    const columns = reactive([
        ['China', 'USA'],
        ['Beijing', 'Shanghai', 'Guangzhou'],
    ]);

    const cityData = reactive({
        China: ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen'],
        USA: ['New York', 'Los Angeles', 'Chicago', 'Houston'],
    });

    const onChange = (e) => {
        const { columnIndex, value } = e;
        if (columnIndex === 0) {
            const selectedCountry = value[0];
            pickerRef.value.setColumnValues(1, cityData[selectedCountry]);
        }
    };

    const onConfirm = (e) => {
        console.log('Linked selection:', e.value);
    };
</script>

Object Data Format ​

html
<template>
    <hy-picker
        v-model="value"
        has-input
        :columns="columns"
        label-key="label"
        value-key="value"
        @confirm="onConfirm"
    ></hy-picker>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const value = ref('');
    const columns = reactive([
        [
            { label: 'Snowy Moon Night', value: 2021 },
            { label: 'Cold Night Rain', value: 804 },
            { label: 'Gentle Breeze', value: 305 },
        ],
    ]);

    const onConfirm = (e) => {
        console.log('Selected:', e.value);
    };
</script>

Custom Popup Position ​

html
<template>
    <hy-picker :show="show" :columns="columns" popup-mode="top" @confirm="onConfirm"></hy-picker>
    <hy-cell clickable @click="show = true">
        <hy-cell-item title="Top Popup" :value="value"></hy-cell-item>
    </hy-cell>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const show = ref(false);
    const value = ref('');
    const columns = reactive([['Option 1', 'Option 2', 'Option 3']]);

    const onConfirm = (e) => {
        value.value = e.value.join('');
        show.value = false;
    };
</script>

Custom Toolbar Buttons ​

html
<template>
    <hy-picker
        :show="show"
        :columns="columns"
        cancel-text="Cancel Selection"
        confirm-text="Confirm Selection"
        cancel-color="#999999"
        confirm-color="#4F8EF7"
        title="Custom Title"
        @confirm="onConfirm"
        @cancel="onCancel"
    ></hy-picker>
    <hy-cell clickable @click="show = true">
        <hy-cell-item title="Custom Buttons" :value="value"></hy-cell-item>
    </hy-cell>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const show = ref(false);
    const value = ref('');
    const columns = reactive([['Option A', 'Option B', 'Option C']]);

    const onConfirm = (e) => {
        value.value = e.value.join('');
        show.value = false;
    };

    const onCancel = () => {
        show.value = false;
    };
</script>

Custom Input Style ​

html
<template>
    <hy-picker
        v-model="value"
        has-input
        :columns="columns"
        :input="{
            placeholder: 'Please select',
            fontSize: 16,
            prefixIcon: 'calendar',
            border: false
        }"
    ></hy-picker>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const value = ref('');
    const columns = reactive([['Option 1', 'Option 2', 'Option 3']]);
</script>

Set Default Selection ​

html
<template>
    <hy-picker
        :show="show"
        :columns="columns"
        :defaultIndex="[1, 2]"
        @confirm="onConfirm"
    ></hy-picker>
    <hy-cell clickable @click="show = true">
        <hy-cell-item title="Default Selection" :value="value"></hy-cell-item>
    </hy-cell>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const show = ref(false);
    const value = ref('');
    const columns = reactive([
        ['A', 'B', 'C', 'D'],
        ['1', '2', '3', '4'],
    ]);

    const onConfirm = (e) => {
        value.value = e.value.join(' / ');
        show.value = false;
    };
</script>

Disable Close on Overlay Click ​

html
<template>
    <hy-picker
        :show="show"
        :columns="columns"
        :closeOnClickOverlay="false"
        title="Must click a button to close"
        @confirm="onConfirm"
    ></hy-picker>
    <hy-cell clickable @click="show = true">
        <hy-cell-item title="Disable Overlay Close" :value="value"></hy-cell-item>
    </hy-cell>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const show = ref(false);
    const value = ref('');
    const columns = reactive([['Option 1', 'Option 2', 'Option 3']]);

    const onConfirm = (e) => {
        value.value = e.value.join('');
        show.value = false;
    };
</script>

Custom Slot Content ​

html
<template>
    <hy-picker v-model="value" has-input :columns="columns" title="Custom Title" ref="pickerRef">
        <!-- Custom input content -->
        <template #default>
            <view class="custom-input">
                <text>Custom content: {{ value }}</text>
            </view>
        </template>

        <!-- Custom toolbar right side -->
        <template #toolbar-right>
            <hy-button text="Save" size="small" @click="handleSave"></hy-button>
        </template>

        <!-- Custom area below the toolbar -->
        <template #toolbar-bottom>
            <view class="toolbar-tip">
                <text>Please select your options</text>
            </view>
        </template>
    </hy-picker>
</template>

<script setup>
    import { ref, reactive } from 'vue';

    const pickerRef = ref(null);
    const value = ref('');
    const columns = reactive([['Option 1', 'Option 2', 'Option 3']]);

    const handleSave = () => {
        console.log('Save:', value.value);
        pickerRef.value.onConfirm();
    };
</script>

API ​

Picker Props ​

ParameterDescriptionTypeDefault
modelValueThe value echoed to the input box (required when hasInput is true)string|number|array-
showWhether to show the picker (no need to set when hasInput is true)booleanfalse
popupModePopup display mode[1]stringbottom
separatorMulti-column separatorstring/
showToolbarWhether to show the top toolbarbooleantrue
titleTop titlestring-
columnsSets the data for each column; supports a one-dimensional array (single column) or a two-dimensional array (multiple columns)array[]
loadingWhether to show the loading statebooleanfalse
itemHeightHeight of a single option in each column (px)number44
cancelTextCancel button textstringCancel
confirmTextConfirm button textstringConfirm
cancelColorCancel button colorstring#909193
confirmColorConfirm button colorstring-
visibleItemCountNumber of visible options per columnnumber5
labelKeyKey name for the display text in option objectsstringlabel
valueKeyKey name for the value in option objectsstringvalue
closeOnClickOverlayWhether clicking the overlay is allowed to close the pickerbooleanfalse
defaultIndexDefault index for each columnarray[]
immediateChangeWhether to trigger the change event immediately when the finger is releasedbooleantrue
zIndexPopup z-indexnumber10076
hasInputWhether to show the input boxbooleanfalse
inputInput box configuration properties; takes effect when hasInput is true; see Input APIHyInputProps{}
toolbarRightSlotWhether to enable the toolbar right slot (must be used with slot="toolbar-right")booleanfalse

Events ​

Event NameDescriptionCallback Parameters
closeTriggered when the picker is closed-
confirmTriggered when the confirm button is clicked; returns the currently selected values{ indexs, value, values }
changeTriggered when the selected value changes{ columnIndex, index, indexs, value, values }
cancelTriggered when the cancel button is clicked-

change Event Parameters ​

ParameterDescriptionType
columnIndexIndex of the column that changednumber
indexIndex of the selected option in the current columnnumber
indexsArray of indices for all columnsarray
valueCurrently selected values (array)array
valuesData for all columns (two-dimensional array)array

confirm Event Parameters ​

ParameterDescriptionType
indexsArray of indices for all columnsarray
valueCurrently selected values (array)array
valuesData for all columns (two-dimensional array)array

Methods ​

Call the following methods via ref:

MethodDescriptionParameters
setColumnValuesSets the option data of the specified columncolumnIndex: number column index, values: array new data
onConfirmManually triggers confirm selection-
closeCloses the picker popup-

Slots ​

Slot NameDescriptionCallback Parameters
defaultCustom input box content (takes effect when hasInput is true)-
toolbar-rightToolbar right content; must also set toolbarRightSlot="true" for it to take effect (WeChat Mini Program limitation)-
toolbar-bottomCustom area below the toolbar-

typings ​

Type Definitions
ts
interface PickerColumnVo {
    /** Value (required) */
    value: string | number;
    /** Display text */
    label?: string;
    /** Custom attributes */
    [key: string]: any;
}

interface SelectValueVo {
    /** Currently selected values (array) */
    value: string[];
    /** Index of the selected option in the current column */
    index?: number;
    /** Array of indices for all columns */
    indexs?: number[];
    /** Data for all columns (two-dimensional array) */
    values?: Array<any>;
    /** Index of the column that changed */
    columnIndex?: number;
}

interface IPickerExpose {
    /** Set the values of a column */
    setColumnValues: (columnIndex: number, values: Array<string | PickerColumnVo>) => void;
    /** Manually trigger confirm selection */
    onConfirm: () => void;
    /** Close the picker popup */
    close: () => void;
}

CSS Variables ​

Variable NameDescriptionDefault
--hy-picker-item-disabled-opacityOpacity of disabled options0.35
--hy-picker-loading-z-indexLoading layer z-index1000
--hy-picker-title-font-sizeTop title font sizeFollows --hy-font-size-base (28rpx)
--hy-picker-title-colorTop title colorFollows --hy-text-color--grey
--hy-picker-item-colorOption text colorFollows --hy-text-color
12:33
battery

  1. bottom: pops up from the bottom; top: pops up from the top ↩︎