Select 选择器
从下拉列表中选择一项或多项。
基础用法
使用 v-model 绑定选中项的 value,通过 options 提供选项。
查看代码收起代码
vue
<script setup lang="ts">
import { ZSelect } from '@codex-proxy/ui'
import { ref } from 'vue'
const group = ref<string>()
const groups = [
{ value: 'production', label: '生产环境' },
{ value: 'testing', label: '测试环境' },
{ value: 'development', label: '开发环境' },
{ value: 'paused', label: '暂停使用', disabled: true },
]
</script>
<template>
<ZSelect v-model="group" :options="groups" aria-label="账号分组" class="w-60 max-w-full" />
</template>筛选选项
设置 filterable 后,可直接在选择框内输入关键词,按名称或说明筛选选项。用方向键定位,按回车选中。
查看代码收起代码
vue
<script setup lang="ts">
import { ZSelect } from '@codex-proxy/ui'
import { ref } from 'vue'
const group = ref('production')
const groups = [
{ value: 'production', label: '生产环境' },
{ value: 'testing', label: '测试环境' },
{ value: 'development', label: '开发环境' },
{ value: 'paused', label: '暂停使用', disabled: true },
]
</script>
<template>
<ZSelect v-model="group" :options="groups" aria-label="账号分组" class="w-60 max-w-full" filterable />
</template>多选
设置 multiple 可选择多项,绑定值为数组。
查看代码收起代码
vue
<script setup lang="ts">
import { ZSelect } from '@codex-proxy/ui'
import { ref } from 'vue'
const group = ref(['production', 'testing'])
const groups = [
{ value: 'production', label: '生产环境' },
{ value: 'testing', label: '测试环境' },
{ value: 'development', label: '开发环境' },
{ value: 'paused', label: '暂停使用', disabled: true },
]
</script>
<template>
<ZSelect v-model="group" :options="groups" aria-label="账号分组" class="w-60 max-w-full" multiple />
</template>折叠标签
设置 collapse-tags 折叠已选项,搭配 collapse-tags-tooltip 可查看隐藏标签。
查看代码收起代码
vue
<script setup lang="ts">
import { ZSelect } from '@codex-proxy/ui'
import { ref } from 'vue'
const group = ref(['production', 'testing', 'development'])
const groups = [
{ value: 'production', label: '生产环境' },
{ value: 'testing', label: '测试环境' },
{ value: 'development', label: '开发环境' },
{ value: 'paused', label: '暂停使用', disabled: true },
]
</script>
<template>
<ZSelect v-model="group" :options="groups" aria-label="账号分组" class="w-60 max-w-full" multiple collapse-tags collapse-tags-tooltip />
</template>Select API
属性
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
validate-event | 随控件事件触发表单校验 | boolean | true |
multiple-limit | 多选可选数量上限,0 表示不限 | number | 0 |
no-match-text | 搜索无匹配时的文案 | string | 语言包 |
loading-text | 加载时的文案 | string | 语言包 |
v-model | Value 为 string 或 number;单选空值 undefined,多选空值 [],0 是有效值 | Value | undefined 或 Value[] | 必填 |
options | label、value;可选 description、disabled | SelectOption<Value>[] | 必填 |
size | 组件尺寸 | 'small' | 'default' | 'large' | 'default' |
multiple | 开启多选 | boolean | false |
disabled / loading / filterable / clearable / remote | 禁用、加载、搜索、清空、外部筛选 | boolean | false |
remote-method | 远程搜索回调,接收最后一次输入的关键词 | (query: string) => void | — |
debounce | 远程搜索的防抖时间(毫秒),0 表示立即调用 | number | 300 |
placeholder / empty-text | 占位和空状态 | string | '请选择' / '暂无选项' |
collapse-tags / collapse-tags-tooltip | 折叠标签与溢出提示 | boolean | false |
max-collapse-tags | 至少显示一个标签 | number | 1 |
remote 模式下,query-change 随输入立即发出,可用于取消旧请求。remote-method 在停止输入后调用,关闭菜单会取消等待中的回调。分页由 end-reached 触发。通过 ZFormItem 或 aria-label 为控件命名
事件
| 事件 | 参数 | 说明 |
|---|---|---|
update:modelValue / change | 当前模型值 | 模型变化 / 用户选择、移除或清空 |
clear | 无 | 清空选择 |
remove-tag | Value | 用户移除一个标签 |
visible-change | boolean | 菜单显隐变化 |
query-change | string | 搜索输入完成 |
popup-scroll | { scrollTop, scrollLeft } | 下拉浮层的滚动坐标 |
end-reached | top / bottom / left / right | 滚动到达的边界方向 |
focus / blur | FocusEvent | 触发控件焦点变化 |
远程分页时监听 end-reached,方向为 bottom、有下一页且请求空闲时才加载。将新选项追加到原列表会保留滚动位置与键盘焦点,loading 用于替换整份结果时的加载提示。
插槽
option({ option, index, selected, disabled }):自定义选项内容empty({ search }):无匹配选项
实例
focus()、blur()、clear()、open()、close()