选择器
带组合框触发器和列表框浮层的无障碍自绘选择器。
@zeus-web/select数据录入zweb add select自动生成
交互演示
源码
请选择与你的应用框架匹配的入口。
html
<script type="module">
import '@zeus-web/select/wc/auto'
</script>
<zw-select aria-label="Environment">
<option value="preview">Preview</option>
<option value="production">Production</option>
</zw-select>tsx
import { Select } from '@zeus-web/select/react'
export function SelectExample() {
return (
<Select aria-label="Environment">
<option value="preview">Preview</option>
<option value="production">Production</option>
</Select>
)
}vue
<script setup lang="ts">
import { Select } from '@zeus-web/select/vue'
</script>
<template>
<Select aria-label="Environment">
<option value="preview">Preview</option>
<option value="production">Production</option>
</Select>
</template>添加
bash
zweb add select导入
样式化 Registry 组件:
tsx
import { Select } from '@/components/ui/select'基础组件 React 包装器:
tsx
import { Select } from '@zeus-web/select/react'Web Component 入口:
ts
import '@zeus-web/select/wc/auto'属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
id | string | - | 用于派生内部触发器 ID 的基础 ID;列表框继续使用自动生成的 ID。 |
value | string | - | 当前选中值。 |
defaultValue | string | - | 初始选中值。 |
values | string[] | - | 多选模式下的当前选中值;请通过 JavaScript property 传入。 |
defaultValues | string[] | - | 多选模式下的初始选中值;请通过 JavaScript property 传入。 |
placeholder | string | - | 未选择选项时显示的文本。 |
size | SelectSize可选值: sm, md, lg | md | 选择器控件尺寸。 |
multiple | boolean | - | 启用多选。 |
disabled | boolean | - | 禁用用户交互。 |
required | boolean | - | 将选择器标记为表单必填项。 |
invalid | boolean | - | 将选择器标记为无效。 |
name | string | - | 表单提交时使用的字段名称。 |
ariaLabel | string | - | 无可见标签选择器的无障碍标签。 |
ariaLabelledby | string | - | 可见选择器标签的 ID 引用。 |
ariaDescribedby | string | - | 额外无障碍说明的 ID 引用。 |
ariaErrormessage | string | - | 无障碍错误消息的 ID 引用。 |
事件
| 事件 | React 属性 | 详情 | 说明 |
|---|---|---|---|
value-change | onValueChange | value: string, values: string[], nativeEvent: Event | 选中值变化时触发。 |
focus-change | onFocusChange | focused: boolean, nativeEvent: FocusEvent | 焦点进入或离开选择器触发器时触发。 |
插槽
| 插槽 | 说明 |
|---|---|
default | 作为选择器数据源的 option 子元素。 |
样式
使用 Tailwind: 是
主题变量:
border-inputbg-popovertext-popover-foregroundbg-accentring-ring
内部选择器:
[data-slot=select-trigger][data-slot=select-content][data-slot=select-option][data-slot=select-option-indicator][data-slot=select-message]
Registry
Registry 源码:可用。
添加命令: zweb add select
类型: component
依赖:
@zeus-web/select
Registry 依赖:
cnglobals
文件:
templates/react/select.tsx→components/ui/select.tsxtemplates/vue/select.ts→components/ui/select.ts
AI 使用规则
建议:
- 需要键盘导航的单选或多选列表使用 Select。
- 字段没有初始选中项时使用 placeholder。
- values 和 defaultValues 应通过 JavaScript property 设置,不要写成 HTML attribute。
避免:
- 不要用 Select 处理自由文本输入或远程自动补全。
示例
React 样式化用法
tsx
import { Select } from '@/components/ui/select'
export function Example() {
return (
<Select defaultValue="apple">
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</Select>
)
}