145a208687
HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
349 lines
20 KiB
Markdown
349 lines
20 KiB
Markdown
---
|
||
title: Select 选择器
|
||
description: Select 展示可折叠的选项列表,并允许用户从中选择一项。
|
||
component: select
|
||
group: 组件
|
||
---
|
||
|
||
## 用法
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
Select,
|
||
SelectTrigger,
|
||
SelectValue,
|
||
SelectIndicator,
|
||
SelectPopover,
|
||
ListBox,
|
||
ListBoxItem,
|
||
ListBoxItemIndicator,
|
||
Label,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
```
|
||
|
||
<ComponentPreview name="select-default" />
|
||
|
||
## 组件结构
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
Select,
|
||
SelectTrigger,
|
||
SelectValue,
|
||
SelectClearButton,
|
||
SelectIndicator,
|
||
SelectPopover,
|
||
Label,
|
||
Description,
|
||
Header,
|
||
ListBox,
|
||
ListBoxItem,
|
||
ListBoxItemIndicator,
|
||
ListBoxSection,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
<template>
|
||
<Select>
|
||
<Label>选项</Label>
|
||
<SelectTrigger>
|
||
<SelectValue />
|
||
<SelectClearButton />
|
||
<SelectIndicator />
|
||
</SelectTrigger>
|
||
<Description>请选择一项</Description>
|
||
<SelectPopover>
|
||
<ListBox>
|
||
<ListBoxItem id="first" text-value="第一项">
|
||
<div>
|
||
<Label>第一项</Label>
|
||
<Description>选项说明</Description>
|
||
</div>
|
||
<ListBoxItemIndicator />
|
||
</ListBoxItem>
|
||
<ListBoxSection>
|
||
<Header>分组</Header>
|
||
<ListBoxItem id="second" text-value="第二项">
|
||
<Label>第二项</Label>
|
||
</ListBoxItem>
|
||
</ListBoxSection>
|
||
</ListBox>
|
||
</SelectPopover>
|
||
</Select>
|
||
</template>
|
||
```
|
||
|
||
## 示例
|
||
|
||
### 变体
|
||
|
||
Select 组件支持两种视觉变体:
|
||
|
||
- **`primary`**(默认)— 带阴影的标准样式,适用于大多数场景
|
||
- **`secondary`** — 低强调、无阴影,适合在 Surface 等表面背景上使用
|
||
|
||
<ComponentPreview name="select-variants" />
|
||
|
||
### 宽度充满
|
||
|
||
<ComponentPreview name="select-full-width" />
|
||
|
||
### 带描述
|
||
|
||
<ComponentPreview name="select-with-description" />
|
||
|
||
### 必填
|
||
|
||
<ComponentPreview name="select-required" />
|
||
|
||
### 禁用
|
||
|
||
<ComponentPreview name="select-disabled" />
|
||
|
||
### 含禁用选项
|
||
|
||
<ComponentPreview name="select-with-disabled-options" />
|
||
|
||
### 多选
|
||
|
||
<ComponentPreview name="select-multiple-select" />
|
||
|
||
### 分组选项
|
||
|
||
<ComponentPreview name="select-with-sections" />
|
||
|
||
### 受控组件
|
||
|
||
<ComponentPreview name="select-controlled" />
|
||
|
||
### 受控多选
|
||
|
||
<ComponentPreview name="select-controlled-multiple" />
|
||
|
||
### 受控展开状态
|
||
|
||
<ComponentPreview name="select-controlled-open-state" />
|
||
|
||
### 异步加载
|
||
|
||
Vue 使用响应式数组、Fetch 与 VueUse 可见性观察加载 PokeAPI 分页;保留取消、加载中、错误重试和手动加载入口,不导出 React Collection / ListBoxLoadMoreItem。
|
||
|
||
<ComponentPreview name="select-asynchronous-loading" />
|
||
|
||
### 自定义指示器
|
||
|
||
<ComponentPreview name="select-custom-indicator" />
|
||
|
||
### 带清除按钮
|
||
|
||
`SelectTrigger` 本身是 button,因此不能再嵌套 `<button>` 来实现清除。请在触发器内组合 `SelectClearButton` — 它渲染为非 button 控件,且不会打开菜单。
|
||
|
||
<ComponentPreview name="select-with-clear-button" />
|
||
|
||
由于 ARIA 将 button 的子节点视为纯展示内容,清除控件无法获得焦点,因此它带有 `aria-hidden`,仅作为指针操作的可视入口。当组合了 `SelectClearButton` 时,触发器同时支持按 `Backspace` 或 `Delete` 清除,这是键盘与屏幕阅读器用户的清除方式。两种方式都会调用 `clear`。
|
||
|
||
### 自定义展示值
|
||
|
||
<ComponentPreview name="select-custom-value" />
|
||
|
||
### 渲染函数
|
||
|
||
通过 `as`、原生 attrs 和作用域插槽表达 Vue 渲染能力。
|
||
|
||
<ComponentPreview
|
||
name="select-render-function"
|
||
/>
|
||
|
||
### 表面样式
|
||
|
||
在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"`,以应用适合表面背景的低强调变体。
|
||
|
||
<ComponentPreview name="select-on-surface" />
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="select-custom-styles" />
|
||
|
||
### 全局 CSS
|
||
|
||
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||
|
||
```css
|
||
@layer components {
|
||
.hv-select {
|
||
@apply flex flex-col gap-1;
|
||
}
|
||
|
||
.hv-select__trigger {
|
||
@apply rounded-lg border border-border bg-surface p-2;
|
||
}
|
||
|
||
.hv-select__value {
|
||
@apply text-current;
|
||
}
|
||
|
||
.hv-select__indicator {
|
||
@apply text-muted;
|
||
}
|
||
|
||
.hv-select__popover {
|
||
@apply rounded-lg border border-border bg-surface p-2;
|
||
}
|
||
}
|
||
```
|
||
|
||
## 样式参考
|
||
|
||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
||
|
||
### CSS 类
|
||
|
||
Select 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/select.css)):
|
||
|
||
#### 基础类
|
||
|
||
- `.hv-select` - Select 根容器
|
||
- `.hv-select__trigger` - 打开下拉的触发按钮
|
||
- `.hv-select__value` - 当前显示的值或占位符
|
||
- `.hv-select__clear-button` - 触发器内的可选清除控件
|
||
- `.hv-select__indicator` - 下拉指示图标
|
||
- `.hv-select__popover` - 弹出层容器
|
||
|
||
#### 变体类
|
||
|
||
- `.hv-select--primary` - Primary 变体,带阴影(默认)
|
||
- `.hv-select--secondary` - Secondary 变体,无阴影,适合在 Surface 上使用
|
||
|
||
#### 状态类
|
||
|
||
- `.hv-select[data-invalid="true"]` - 无效状态
|
||
- `.hv-select__trigger[data-focus-visible="true"]` - 触发器聚焦状态
|
||
- `.hv-select__trigger[data-disabled="true"]` - 触发器禁用状态
|
||
- `.hv-select__value[data-placeholder="true"]` - 占位符状态
|
||
- `.hv-select__clear-button[data-empty="true"]` - 无选中项时隐藏清除控件
|
||
- `.hv-select__indicator[data-open="true"]` - 展开时的指示器状态
|
||
|
||
### 交互状态
|
||
|
||
该组件同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制:
|
||
|
||
- **悬停**:触发器上的 `:hover` 或 `[data-hovered="true"]`
|
||
- **聚焦**:触发器上的 `:focus-visible` 或 `[data-focus-visible="true"]`
|
||
- **禁用**:Select 上的 `:disabled` 或 `[data-disabled="true"]`
|
||
- **展开**:指示器上的 `[data-open="true"]`
|
||
|
||
## API 参考
|
||
|
||
### Select
|
||
|
||
`SelectRoot` 是 `Select` 的别名。Key 为字符串或有限数值,数字 `0` 和字符串 `"0"` 不相同,空字符串有效。单选清空为 `null`,多选为 `[]`。显式 `modelValue` 优先于 `value`,显式 `open` 优先于 `isOpen`,包括显式传入 `undefined`。
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------------------------------ | --------------------------------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
||
| `placeholder` | `string` | `"Select an item"` | 空选择的占位符。 |
|
||
| `selectionMode` | `"single" \| "multiple"` | `"single"` | 单选或多选。ListBox 的选择模式由 Select 统一管理。 |
|
||
| `modelValue` / `value` | `Key \| Key[] \| null` | — | 受控值,推荐 `v-model`。多选必须是数组。 |
|
||
| `defaultValue` | `Key \| Key[] \| null` | `null` / `[]` | 非受控初值,也是 reset 的目标值。 |
|
||
| `open` / `isOpen` | `boolean` | — | 受控展开状态,推荐 `v-model:open`。 |
|
||
| `defaultOpen` | `boolean` | `false` | 非受控展开初值。 |
|
||
| `disabledKeys` | `readonly Key[]` | — | 禁用选项 key;Vue 使用数组替代 Iterable。 |
|
||
| `items` | `readonly SelectItem[]` | — | `{ id, textValue, isDisabled? }` 完整元数据。用于不透明包装和异步数据,保证关闭态/SSR 的文字与提交正确;不负责渲染选项。 |
|
||
| `isDisabled` / `disabled` | `boolean` | `false` | 禁用交互与原生提交;继承 Fieldset 禁用。 |
|
||
| `isReadOnly` | `boolean` | `false` | 保留提交值,阻止用户改选与展开。 |
|
||
| `isRequired` | `boolean` | `false` | 必填,校验有效且未禁用的已选项。 |
|
||
| `isInvalid` | `boolean` | `false` | 外部无效状态。 |
|
||
| `validationBehavior` | `"native" \| "aria"` | 继承 Form,默认 `"native"` | 原生约束或仅 ARIA 校验。 |
|
||
| `validate` | `(value: SelectModelValue) => ValidateResult` | — | 本地自定义校验;同时消费 Form 服务端错误。 |
|
||
| `name` / `form` | `string` | — | 原生表单名称和外部 form ID。多选同名重复提交;空/不存在/禁用选项不提交。 |
|
||
| `autoComplete` | `string` | — | 传给唯一的原生 select 自动填充层;空选择时用稳定 ID 标识,首次自动填充是否出现由浏览器决定。 |
|
||
| `fullWidth` | `boolean` | `false` | 占满容器宽度。 |
|
||
| `variant` | `"primary" \| "secondary"` | `"primary"` | 主要/次要变体。 |
|
||
| `id` | `string` | 自动生成 | 触发器 DOM ID,关联 Label、选项列表与校验。 |
|
||
| `as` | `string` | `"div"` | 根 HTML 标签。 |
|
||
| `class` / `style` / 原生 attrs | 原生属性 | — | 传给根容器。根 `aria-label` 同时用于触发器;触发器可单独覆盖。 |
|
||
|
||
### 事件
|
||
|
||
| 事件 | 参数 | 描述 |
|
||
| ------------------------------ | ------------------ | -------------------------------------------------------------------------- |
|
||
| `update:modelValue` / `change` | `SelectModelValue` | 用户选择提案;父组件未接受时可见状态和 FormData 保持父值,相同提案可重试。 |
|
||
| `update:open` / `openChange` | `boolean` | 展开状态提案;父更新和 reset 不伪造值变化事件。 |
|
||
| `clear` | 无 | 指针或关闭态 Backspace/Delete 请求清空;同时触发值事件。 |
|
||
|
||
### SelectTrigger
|
||
|
||
| Prop / 事件 | 类型 | 默认值 | 描述 |
|
||
| ------------------------ | --------- | ---------- | ---------------------------------------------------------- |
|
||
| `as` | `string` | `"button"` | 触发器标签;保留正确键盘激活行为。 |
|
||
| `isDisabled` | `boolean` | `false` | 单独禁用触发器及其内部清空控件。 |
|
||
| `class` / `style` / ARIA | 原生属性 | — | 传到真正的交互节点;显式名称优先。 |
|
||
| 原生键盘/指针事件 | DOM Event | — | 用户处理器先运行,可通过 `preventDefault()` 取消内部行为。 |
|
||
|
||
### SelectValue
|
||
|
||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||
| ----------------- | ---------- | -------- | ------------------------ |
|
||
| `as` | `string` | `"span"` | 展示值标签。 |
|
||
| `class` / `style` | 原生属性 | — | 值区域样式。 |
|
||
| `default` | 作用域插槽 | 已选文字 | 自定义展示值,参数如下。 |
|
||
|
||
### SelectIndicator
|
||
|
||
| Prop / 插槽 | 类型 | 描述 |
|
||
| ----------------- | ------------- | -------------------------------------------------------------- |
|
||
| `class` / `style` | 原生属性 | 指示器样式。 |
|
||
| `default` | 单一 Vue 节点 | 自定义图标;状态与样式合并到该节点。默认使用移植自基线的图标。 |
|
||
|
||
### SelectClearButton
|
||
|
||
| Prop / 事件 / 插槽 | 类型 | 默认值 | 描述 |
|
||
| ------------------ | ------------ | ------------ | -------------------------------------- |
|
||
| `as` | `string` | `"span"` | 保持非 button 标签,避免嵌套交互按钮。 |
|
||
| `class` / `style` | 原生属性 | — | 清除控件样式。 |
|
||
| `click` | `MouseEvent` | — | 可取消清除;不会打开弹层。 |
|
||
| `default` | Vue 插槽 | 基线关闭图标 | 自定义内容。 |
|
||
|
||
### SelectPopover
|
||
|
||
| Prop / 事件 | 类型 | 默认值 | 描述 |
|
||
| ----------------------------------- | ----------------------- | ---------- | -------------------------------------------------------------------------------------------------------- |
|
||
| `placement` | `OverlayPlacement` | `"bottom"` | bottom/top/left/right/start/end 及对应 left/right/top/bottom/start/end 对齐组合,沿用 Popover 定位协议。 |
|
||
| `offset` | `number` | `8` | 与触发器的距离。 |
|
||
| `shouldFlip` | `boolean` | `true` | 空间不足时翻转。 |
|
||
| `containerPadding` | `number` | `12` | 视口碰撞边距。 |
|
||
| `to` | `string \| HTMLElement` | `body` | Teleport 目标;SSR 默认关闭态不会渲染弹层。 |
|
||
| `class` / `style` / attrs | 原生属性 | — | 弹层节点属性;选择器只承载一个 ListBox。 |
|
||
| `openAutoFocus` / `closeAutoFocus` | `Event` | — | 可取消默认焦点。默认进入已选/首项,关闭恢复触发器;外部交互保留外部焦点。 |
|
||
| `escapeKeyDown` / `interactOutside` | DOM / CustomEvent | — | 复用 Popover 的事件与取消协议。 |
|
||
|
||
### 作用域插槽状态
|
||
|
||
| 插槽 | 参数 | 描述 |
|
||
| --------------------- | ---------------------------------------------------------- | ------------------------------------------------------------------------- |
|
||
| Select `default` | `{ value, isOpen, selectedItems }` | 当前接受状态;初次 SSR 需要直接使用 selectedItems 时请提供 items 元数据。 |
|
||
| SelectValue `default` | `{ defaultChildren, isPlaceholder, selectedItems, state }` | 默认文字、占位状态与选中项。 |
|
||
| `selectedItems` | `{ key: Key; textValue: string }[]` | 已选元数据;自定义头像等通过业务数据查找。 |
|
||
| `state` | `{ selectedItems, isOpen }` | Vue 响应式状态快照,不暴露 React SelectState。 |
|
||
|
||
非受控 `selectionMode` 切换会适配标量/数组外观,切回单选取首项,不发编辑事件;受控切换应由父组件同时提供匹配模式的值。
|
||
|
||
通过组件 ref 调用 `focus()`、`open()`、`close()`,`element` 指向触发器。
|
||
|
||
## 无障碍与兼容差异
|
||
|
||
- 原生按钮承担触发、取消与禁用。触发器使用选择型 `combobox`,关联实际 `listbox`,选项名称与字段标签隔离。
|
||
- 方向键进入已选项(空选择时 ArrowDown 首项、ArrowUp 末项),列表复用 ListBox 导航、首字母搜索和禁用跳过。单选激活关闭,多选保持展开;Escape 关闭,Tab 离开弹层。
|
||
- 复用现有 Popover 非模态焦点与定位体系;与上游 RAC 的移动端原生选择菜单、模态隐藏外部内容不同。当前不提供移动端原生菜单切换。
|
||
- 关闭态从已知 ListBox/Section/Item 插槽预规划元数据;不执行任意包装组件的 setup/render。不透明包装请提供 Select 的 `items`,否则首次打开前未知选项只能显示 key,且无法提交未知选项。
|
||
- 唯一原生 select 提交层保留字符串边界,多选使用重复 name;类型区分只用于组件值与事件。浏览器自动填充通过选中 option 的索引恢复原始 key,不把内部编码放进 FormData。
|
||
- 支持原生必填、重置、Fieldset 与 Form 错误关联;跨浏览器自动填充与实际屏幕阅读器播报仍需专项验收,不声明等同 React Aria 全部能力。
|
||
- 样式及 19 个中文示例来自固定 HeroUI 基线;React 渲染函数改为 Vue 插槽,表单成功反馈与基线一致使用 `alert`,图标扩展示例使用 Tabler。
|
||
|
||
## 相关组件
|
||
|
||
[ListBox 列表框](./list-box) · [Popover 弹出框](./popover) · [Label 标签](./label)
|