Picker Picker
Choose wheel values in a popup and commit the draft on confirmation.
Import
Use <hui-picker> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
<template>
<view>
<hui-picker
ref="control"
v-model="value"
:options="options"
placeholder="选择城市 / Choose city"
title="城市 / City"
@update:model-value="event0"
@change="event1"
@pick="event2"
@cancel="event3"
@clear="event4"
@update:visible="event5"
@open="event6"
@opened="event7"
@close="event8"
@closed="event9"
/>
<text>{{ value }}</text>
<hui-button @click="() => control?.open()">open()</hui-button>
<hui-button @click="() => control?.close('api')">close()</hui-button>
<hui-button @click="() => control?.confirm()">confirm()</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { type PickerValue } from '@nscode/handy-uni'
const lastEvent = ref('No event yet')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const control = ref()
const value = ref<PickerValue | PickerValue[] | null>('sh')
const options = [
{ label: '上海 / Shanghai', value: 'sh' },
{ label: '北京 / Beijing', value: 'bj' },
{ label: '不可选 / Disabled', value: 'none', disabled: true }
]
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:modelValue' + ': ' + [describe(arg0)].join(', ')
}
const event1 = (arg0: unknown, arg1: unknown) => {
lastEvent.value = 'change' + ': ' + [describe(arg0), describe(arg1)].join(', ')
}
const event2 = (arg0: unknown, arg1: unknown) => {
lastEvent.value = 'pick' + ': ' + [describe(arg0), describe(arg1)].join(', ')
}
const event3 = () => {
lastEvent.value = 'cancel'
}
const event4 = () => {
lastEvent.value = 'clear'
}
const event5 = (arg0: unknown) => {
lastEvent.value = 'update:visible' + ': ' + [describe(arg0)].join(', ')
}
const event6 = () => {
lastEvent.value = 'open'
}
const event7 = () => {
lastEvent.value = 'opened'
}
const event8 = (arg0: unknown) => {
lastEvent.value = 'close' + ': ' + [describe(arg0)].join(', ')
}
const event9 = () => {
lastEvent.value = 'closed'
}
</script>
Popup selection edits a draft until confirmation; cancellation does not commit. Confirmation waits for pending wheels to settle. Native multi-column behavior depends on the platform.
Clear through a component ref
clearable controls the visible clear action; clear() can also be called independently. Disabled, readonly and confirmation-pending controls ignore it. Clearing resets the panel draft and emits update:modelValue, clear, then change. Controlled trigger text waits for the parent update.
<script setup lang="ts">
import { ref } from 'vue'
import type { PickerExpose } from '@nscode/handy-uni'
const control = ref<PickerExpose>()
const value = ref<string | null>('a')
const options = [{ value: 'a', label: 'A' }]
</script>
<template>
<view>
<hui-picker ref="control" v-model="value" :options="options" />
<hui-button @click="control?.clear()">Clear</hui-button>
<text>{{ value }}</text>
</view>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
visible | boolean | undefined | Controlled visibility; undefined is omitted and explicit false stays closed. |
defaultVisible | boolean | false | Initial uncontrolled visibility. |
title | string | — | Title. |
confirmText | string | — | Confirmation action text. |
cancelText | string | — | Cancel action text; an empty string remains an explicit override. |
placeholder | string | — | Placeholder when no value is selected or entered. |
trigger | boolean | true | Render the picker trigger. |
separator | string | ' / ' | Separator between selected option labels. |
displayFormat | ((selected: Record<string, unknown>[]) => string) | — | Format selected raw options for the trigger. |
clearable | boolean | false | Show the clear action. |
disabled | boolean | false | Disable interaction. |
readonly | boolean | undefined | Read-only presentation without value changes. |
beforeConfirm | ((value: PickerValue | PickerValue[] | null, context: PickerChangeContext) => unknown) | — | Confirmation guard; false or rejection retains the current content. |
closeOnClickOverlay | boolean | true | Request guarded closing when the overlay is clicked. |
native | boolean | false | Try a native picker; unsupported modes fall back to custom wheels. |
zIndex | number | — | Layer z-index; allocated from the current page stack when omitted. |
rootPortal | boolean | false | Try to mount at the page root; support varies by platform. |
safeAreaInsetBottom | boolean | true | Reserve the bottom safe area. |
modelValue | PickerValue | PickerValue[] | null | undefined | Controlled value; undefined means omitted. |
defaultValue | PickerValue | PickerValue[] | null | undefined | Initial uncontrolled value; ignored in controlled mode. |
options | Record<string, unknown>[] | — | Option data; takes precedence over the default slot. |
columns | Record<string, unknown>[][] | — | Wheel column data; children defines linked options. |
data | Record<string, unknown>[] | — | Linked options organized by children. |
props | PickerFieldNames | — | Data field-name mapping. |
itemHeight | number | 44 | Height of each wheel item in px. |
visibleItemCount | number | 5 | Number of visible wheel items. |
loading | boolean | false | Loading state. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
update:modelValue | (value: PickerValue | PickerValue[] | null) | Request a controlled value update. |
change | (value: PickerValue | PickerValue[] | null, context: PickerChangeContext) | Emitted after the value changes. |
pick | (value: PickerValue | PickerValue[] | null, context: PickerChangeContext & { column: number }) | Reports the corresponding interaction result. |
cancel | () | Cancellation action. |
clear | () | Emitted after clearing. |
update:visible | (visible: boolean) | Request a visibility update. |
open | () | Opening started. |
opened | () | Enter animation finished. |
close | (reason: PickerCloseReason) | Closing started after guards passed. |
closed | () | Leave animation finished. |
Slots
| Slot | Parameters | Description |
|---|---|---|
trigger | { text, value, open } | Custom trigger region; replaces its fallback content. |
toolbar | — | Custom toolbar region; replaces its fallback content. |
title | — | Custom title region; replaces its fallback content. |
option | { option, column, index, active } | Custom option region; replaces its fallback content. |
empty | — | Custom empty region; replaces its fallback content. |
Methods
| Method | Signature | Description |
|---|---|---|
open | (): void | Open the uncontrolled panel or request a controlled update. |
close | (reason?: PickerCloseReason | undefined): void | Request closing through configured guards. |
confirm | (): Promise<void> | Call through the component ref. |
clear | () => void | Request clearing; ignored while disabled, readonly or confirmation-pending. |
Types
PickerProps、PickerExpose、PickerEmits are available from @nscode/handy-uni.
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
| Native mode | Uses platform picker support | Native picker | Native picker | Single columns use select; multiple/linked columns fall back to custom wheels with a warning |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.