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
177 lines
6.7 KiB
Markdown
177 lines
6.7 KiB
Markdown
---
|
||
title: ToggleButtonGroup 切换按钮组
|
||
description: 将多个 ToggleButton 组合为统一控件,允许用户选择单个或多个选项。
|
||
component: toggle-button-group
|
||
group: 组件
|
||
---
|
||
|
||
## 用法
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import { ToggleButtonGroup, ToggleButton } from "@heroui-vue/components";
|
||
</script>
|
||
```
|
||
|
||
<ComponentPreview name="toggle-button-group-basic" />
|
||
|
||
## 组件结构
|
||
|
||
导入 ToggleButtonGroup 组件,并使用具名子组件组合布局。
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
ToggleButtonGroup,
|
||
ToggleButton,
|
||
ToggleButtonGroupSeparator,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
|
||
<template>
|
||
<ToggleButtonGroup selection-mode="multiple">
|
||
<ToggleButton id="first">First</ToggleButton>
|
||
<ToggleButton id="second">
|
||
<ToggleButtonGroupSeparator />
|
||
Second
|
||
</ToggleButton>
|
||
<ToggleButton id="third">
|
||
<ToggleButtonGroupSeparator />
|
||
Third
|
||
</ToggleButton>
|
||
</ToggleButtonGroup>
|
||
</template>
|
||
```
|
||
|
||
## 示例
|
||
|
||
### 尺寸
|
||
|
||
<ComponentPreview name="toggle-button-group-sizes" />
|
||
|
||
### 方向
|
||
|
||
<ComponentPreview name="toggle-button-group-orientation" />
|
||
|
||
### 宽度充满
|
||
|
||
<ComponentPreview name="toggle-button-group-full-width" />
|
||
|
||
### 禁用
|
||
|
||
<ComponentPreview name="toggle-button-group-disabled" />
|
||
|
||
### 无分隔线
|
||
|
||
省略按钮中的 `<ToggleButtonGroupSeparator />` 组件即可。
|
||
|
||
<ComponentPreview name="toggle-button-group-without-separator" />
|
||
|
||
### 分离模式
|
||
|
||
使用 `is-detached` 让按钮之间留出间隔,而不是彼此连接。
|
||
|
||
<ComponentPreview name="toggle-button-group-attached" />
|
||
|
||
### 选择模式
|
||
|
||
使用 `selection-mode="single"` 实现互斥选择,或使用 `selection-mode="multiple"` 实现独立切换。
|
||
|
||
<ComponentPreview name="toggle-button-group-selection-mode" />
|
||
|
||
### 受控组件
|
||
|
||
<ComponentPreview name="toggle-button-group-controlled" />
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="toggle-button-group-custom-styles" />
|
||
|
||
### 全局 CSS
|
||
|
||
若要自定义组件类,可使用 `@layer components` 指令。
|
||
|
||
```css
|
||
@layer components {
|
||
.hv-toggle-button-group {
|
||
@apply rounded-lg;
|
||
}
|
||
|
||
.hv-toggle-button-group__separator {
|
||
@apply opacity-25;
|
||
}
|
||
|
||
.hv-toggle-button-group--full-width {
|
||
@apply w-full;
|
||
}
|
||
}
|
||
```
|
||
|
||
## 样式参考
|
||
|
||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
||
|
||
### CSS 类
|
||
|
||
ToggleButtonGroup 组件使用以下 CSS 类:
|
||
|
||
#### 基础与布局类 [!toc]
|
||
|
||
- `.hv-toggle-button-group` - 容器基础样式
|
||
- `.hv-toggle-button-group--horizontal` - 水平方向
|
||
- `.hv-toggle-button-group--vertical` - 垂直方向
|
||
- `.hv-toggle-button-group--full-width` - 全宽修饰符
|
||
- `.hv-toggle-button-group__separator` - 按钮之间的分隔线元素
|
||
|
||
#### 修饰符类 [!toc]
|
||
|
||
- `.hv-toggle-button-group--detached` - 分离模式(按钮间有间隔)
|
||
|
||
## API 参考
|
||
|
||
### ToggleButtonGroup
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| -------------------------------------- | -------------------------------------------------- | -------------- | --------------------------------------------------- |
|
||
| `selectionMode` | `'single' \| 'multiple'` | `'single'` | 是否允许选中一个或多个按钮 |
|
||
| `v-model` / `modelValue` | `string \| number \| (string \| number)[] \| null` | - | 受控选中值;single 为单个 key,multiple 为 key 数组 |
|
||
| `defaultValue` | 同上 | - | 默认选中值(非受控) |
|
||
| `selectedKeys` / `defaultSelectedKeys` | 同上 | - | 与 `modelValue`/`defaultValue` 等价的别名 |
|
||
| `disallowEmptySelection` | `boolean` | `false` | 是否禁止清空所有选中 |
|
||
| `orientation` | `'horizontal' \| 'vertical'` | `'horizontal'` | 布局方向 |
|
||
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 传递给子 ToggleButton 的尺寸 |
|
||
| `isDetached` | `boolean` | `false` | 按钮是否以间隔分离显示 |
|
||
| `fullWidth` | `boolean` | `false` | 按钮组是否占满可用宽度 |
|
||
| `isDisabled` | `boolean` | `false` | 是否禁用组内全部按钮 |
|
||
| `as` | `string` | `'div'` | 渲染的根元素 |
|
||
|
||
### 事件
|
||
|
||
| 事件 | 参数 | 描述 |
|
||
| ------------------- | ---------------------- | --------------------------------------------- |
|
||
| `update:modelValue` | `key \| key[] \| null` | 选中变化时触发 |
|
||
| `selectionChange` | `key \| key[] \| null` | 选中变化时触发(与 `update:modelValue` 同时) |
|
||
|
||
### ToggleButtonGroupSeparator
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------- | -------- | ------ | ------------- |
|
||
| `class` | `string` | - | 额外的 CSS 类 |
|
||
|
||
## 说明
|
||
|
||
- ToggleButtonGroup 使用 provide/inject 将 `size` 传递给子 ToggleButton 组件
|
||
- 每个 ToggleButton 都必须有唯一 `id`(或 `value`)prop,并与选中 key 对应
|
||
- 组的 `isDisabled` 会禁用所有子 ToggleButton;子项的 `isDisabled` 可额外禁用单个按钮,不能用 `false` 解除组禁用
|
||
- 组件会自动处理按钮之间的圆角
|
||
- 在每个 ToggleButton(第一个除外)内添加 `<ToggleButtonGroupSeparator />`,可在按钮之间显示分隔线
|
||
- `disallowEmptySelection` 阻止用户清空已有选择;需要初始选中时,请同时提供 `defaultValue` 或受控值
|
||
- `selectionMode="single"` 时根节点为 `role="radiogroup"`,按钮为 `role="radio"`;`multiple` 时为 `role="toolbar"` 与 `role="button"`
|
||
- Vue 模型使用 `modelValue`/数组,对应上游 `selectedKeys`/`Set<Key>`;React Aria 集合 API 未直接移植
|
||
|
||
## 相关组件
|
||
|
||
[ToggleButton 切换按钮](./toggle-button) · [ButtonGroup 按钮组](./button-group) · [Button 按钮](./button)
|