Files
heroui-vue/apps/docs/components/fieldset.md
T
2026-10-22 08:46:02 +08:00

131 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Fieldset 字段组
description: 将相关表单控件与图例、描述和操作组合在一起
component: fieldset
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Fieldset,
FieldsetLegend,
FieldGroup,
FieldsetActions,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="fieldset-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
Fieldset,
FieldsetLegend,
FieldGroup,
FieldsetActions,
} from "@heroui-vue/components";
</script>
<template>
<Fieldset>
<FieldsetLegend />
<FieldGroup><!-- 表单字段 --></FieldGroup>
<FieldsetActions><!-- 操作按钮 --></FieldsetActions>
</Fieldset>
</template>
```
## 示例
### 表面样式
在 [Surface](/components/surface) 内使用时,请在表单控件(Input、TextArea 等)上使用 `variant="secondary"`,以应用适合 Surface 背景的低强调变体。
<ComponentPreview name="fieldset-on-surface" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="fieldset-custom-styles" />
### 全局 CSS
使用 `@layer components` 指令定位 Fieldset 的 [BEM](https://getbem.com/) 风格类。
```css
@layer components {
.hv-fieldset {
@apply gap-5 rounded-xl border border-border/60 bg-surface p-6 shadow-field;
}
.hv-fieldset__legend {
@apply text-lg font-semibold;
}
.hv-fieldset__field-group {
@apply gap-3 md:grid md:grid-cols-2;
}
.hv-fieldset__actions {
@apply flex justify-end gap-2 pt-2;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Fieldset 复合组件暴露以下 CSS 选择器:
#### 基础类 [!toc]
- `.hv-fieldset` – 根容器
- `.hv-fieldset__legend` – 图例元素
- `.hv-fieldset__field-group` – 分组字段包裹层
- `.hv-fieldset__actions` – 字段下方的操作栏
## 禁用继承
原生 `disabled` 同步到本库文本输入、SearchField、Switch、Button、CloseButton 与 Link 的状态、事件和表单值。内层字段组不能解除祖先禁用;首个**直接** Legend 内的控件保留原生例外,仍受外层字段组与自身禁用约束。原生 legend 也支持,第三方自定义包装内部的 legend 不作静态识别,请直接使用 FieldsetLegend。
下面是本库新增的协议验证例,不属于上游三项基线示例。
<ComponentPreview name="fieldset-disabled" />
## API 参考
### Fieldset
| 属性 | 类型 | 默认值 | 描述 |
| --------------- | --------------------------- | ------- | ------------------------------ |
| `disabled` | `boolean` | `false` | 原生禁用并下发组件禁用上下文 |
| `name` / `form` | `string` | - | 原生 fieldset 属性,透传根节点 |
| `class` | `string \| Array \| Object` | - | 合并基础类 |
| 默认插槽 | 内容 | - | Legend、字段和操作区 |
### FieldsetLegend / FieldGroup / FieldsetActions
| 部件 | 根元素 | 属性与插槽 |
| --------------- | -------- | ----------------------------------------- |
| FieldsetLegend | `legend` | 原生属性、class/style、DOM 事件、默认插槽 |
| FieldGroup | `div` | 原生属性、class/style、DOM 事件、默认插槽 |
| FieldsetActions | `div` | 原生属性、class/style、DOM 事件、默认插槽 |
三个部件使用对应 BEM 基础类,class 接受字符串、数组和对象。命名导出替代 React 对象式子部件;原生属性直接传递,不使用 nativeProps 对象。根元素固定为语义化原生元素,暂不提供 React render 根元素替换。
Fieldset 不维护字段值或校验队列。请使用 Form + TextField / SearchField,提交与 reset 沿用原生表单;示例展示实际 FormData 与成功反馈,不执行后台保存。
## 相关组件
[TextField](./text-field) · [SearchField](./search-field) · [Input](./input)