翻譯進行中
本頁尚未翻譯為繁體中文,以下顯示簡體中文原文。範例的預設元件文字仍使用繁體中文。
Picker 滚轮选择
在弹层内滚动选择,确认后才提交草稿。
引入
uni-app 配置 easycom 后使用 <hui-picker>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<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('尚无事件')
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>
弹层内先改草稿,确认后才发 model 更新;取消不提交。滚动 pending 时确认要等待滚轮停下。native 模式的多列能力以平台说明为准。
程序清空
clearable 控制界面清空入口;也可独立调用 clear()。禁用、只读或确认钩子等待时忽略。清空同步重置面板草稿,事件顺序为 update:modelValue → clear → change;受控父级不回写则触发区保持原值。
vue
<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()">清空</hui-button>
<text>{{ value }}</text>
</view>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | undefined | v-model:visible |
defaultVisible | boolean | false | v-model:visible |
title | string | — | 标题。 |
confirmText | string | — | 默认 t('common.confirm')、t('common.cancel') |
cancelText | string | — | 默认 t('common.confirm')、t('common.cancel') |
placeholder | string | — | 触发区占位,默认 t('common.selectPlaceholder') |
trigger | boolean | true | 显示内置触发区(像输入框的一行);false 时只用 v-model:visible 或 open() |
separator | string | ' / ' | 多列、联动在触发区的文字连接符 |
displayFormat | ((selected: Record<string, unknown>[]) => string) | — | 自定义触发区文字 |
clearable | boolean | false | 显示清空入口。 |
disabled | boolean | false | 跟随 FormItem |
readonly | boolean | undefined | 跟随 FormItem |
beforeConfirm | ((value: PickerValue | PickerValue[] | null, context: PickerChangeContext) => unknown) | — | exclusive;等待期间确认按钮加载中 |
closeOnClickOverlay | boolean | true | 点遮罩等于取消 |
native | boolean | false | 用原生 picker 弹窗(见各端差异) |
zIndex | number | — | 弹层层号;未传时由本页层栈分配。 |
rootPortal | boolean | false | 尝试挂到页面根层;能力依平台而异。 |
safeAreaInsetBottom | boolean | true | 预留底部安全区。 |
modelValue | PickerValue | PickerValue[] | null | undefined | 清空为 null(单列)或 [](多列、联动) |
defaultValue | PickerValue | PickerValue[] | null | undefined | 非受控初值;受控时忽略。 |
options | Record<string, unknown>[] | — | 只能给一个,给多个时开发环境警告,按 data > columns > options 取 |
columns | Record<string, unknown>[][] | — | 只能给一个,给多个时开发环境警告,按 data > columns > options 取 |
data | Record<string, unknown>[] | — | 只能给一个,给多个时开发环境警告,按 data > columns > options 取 |
props | PickerFieldNames | — | 字段映射;函数写法已在 H5、微信开发工具验证,App 与微信真机待验(总体设计 9.6) |
itemHeight | number | 44 | px |
visibleItemCount | number | 5 | 滚轮可见项数。 |
loading | boolean | false | 列数据加载中:滚轮区显示加载,确认按钮 aria-disabled |
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:modelValue | (value: PickerValue | PickerValue[] | null) | 确认通过钩子后 |
change | (value: PickerValue | PickerValue[] | null, context: PickerChangeContext) | 同上;选同一个值也发(总体设计 9.2 弹出确认类) |
pick | (value: PickerValue | PickerValue[] | null, context: PickerChangeContext & { column: number }) | 滚动中草稿变化;不改 modelValue |
cancel | () | 点取消或遮罩 |
clear | () | update:modelValue(null/[]) → clear → change |
update:visible | (visible: boolean) | close 的原因多了 'confirm'、'cancel' |
open | () | close 的原因多了 'confirm'、'cancel' |
opened | () | close 的原因多了 'confirm'、'cancel' |
close | (reason: PickerCloseReason) | close 的原因多了 'confirm'、'cancel' |
closed | () | close 的原因多了 'confirm'、'cancel' |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
trigger | { text, value, open } | 自定义 trigger 区域,覆盖默认内容。 |
toolbar | — | 自定义 toolbar 区域,覆盖默认内容。 |
title | — | 自定义 title 区域,覆盖默认内容。 |
option | { option, column, index, active } | 自定义 option 区域,覆盖默认内容。 |
empty | — | 自定义 empty 区域,覆盖默认内容。 |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
open | (): void | 打开非受控面板;受控时发更新请求。 |
close | (reason?: PickerCloseReason | undefined): void | 经前置钩子请求关闭。 |
confirm | (): Promise<void> | 通过组件 ref 调用。 |
clear | () => void | 请求清空;禁用、只读或确认等待时忽略。 |
类型
PickerProps、PickerExpose、PickerEmits 可从 @nscode/handy-uni 导入。
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
| native 模式 | 按原生能力处理 | 原生 picker | 原生 picker | 单列使用 select,多列/联动回退自绘并提示 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。