Files
heroui-vue/apps/docs/components/input-group.md
T

300 lines
11 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: InputGroup 输入框组
description: 将相关输入控件与前后缀元素组合,以增强表单字段
component: input-group
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
InputGroup,
InputGroupInput,
InputGroupPrefix,
Label,
TextField,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="input-group-default" />
## 组件结构
```vue
<script setup lang="ts">
import {
InputGroup,
InputGroupInput,
InputGroupPrefix,
InputGroupSuffix,
Label,
TextField,
} from "@heroui-vue/components";
</script>
<template>
<TextField>
<Label />
<InputGroup>
<InputGroupPrefix />
<InputGroupInput />
<!-- 多行请使用 InputGroupTextArea -->
<InputGroupSuffix />
</InputGroup>
</TextField>
</template>
```
> **InputGroup** 使用可选的前缀与后缀包裹输入框,形成视觉上统一的组合。通常放在 **TextField** 内,用于在输入前后添加图标、文字、按钮等元素。单行输入请使用 **InputGroupInput**,多行输入请使用 **InputGroupTextArea**。
命名导出为 `InputGroup` / `InputGroupInput` / `InputGroupTextArea` / `InputGroupPrefix` / `InputGroupSuffix`,不使用 `InputGroup.Input` 对象式子部件。值、校验、ARIA、reset 与表单提交由既有 TextField / `useTextControl` 统一管理。
## 示例
### 变体
InputGroup 组件支持两种视觉变体:
- **`primary`**(默认)- 标准样式带阴影,适用于大多数场景
- **`secondary`** - 低强调变体无阴影,适用于 Surface 组件内
<ComponentPreview name="input-group-variants" />
### 表面样式
在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。
<ComponentPreview name="input-group-on-surface" />
### 后缀加载状态
在后缀显示加载 spinner 以表示正在处理。
<ComponentPreview name="input-group-with-loading-suffix" />
### 必填字段
InputGroup 会遵循父级 TextField 的必填状态。
<ComponentPreview name="input-group-required" />
### 禁用状态
InputGroup 会遵循父级 TextField 的禁用状态。
<ComponentPreview name="input-group-disabled" />
### 宽度充满
<ComponentPreview name="input-group-full-width" />
### 文字前缀
使用文字作为前缀,例如货币符号或协议前缀。
<ComponentPreview name="input-group-with-text-prefix" />
### 文字后缀
使用文字作为后缀,例如域名后缀或单位。
<ComponentPreview name="input-group-with-text-suffix" />
### 图标前缀与文字后缀
组合图标前缀与文字后缀。
<ComponentPreview name="input-group-with-icon-prefix-and-text-suffix" />
### 复制按钮后缀
在后缀中加入按钮,例如复制图标按钮。本例仅展示布局,复制逻辑由使用方接入。嵌入按钮使用 `type="button"`(本库 Button 默认),避免误提交表单。
<ComponentPreview name="input-group-with-copy-suffix" />
### 图标前缀与复制按钮
组合图标前缀与交互式后缀按钮。
<ComponentPreview name="input-group-with-icon-prefix-and-copy-suffix" />
### 密码可见性切换
在后缀中使用按钮切换密码可见性。
<ComponentPreview name="input-group-password-with-toggle" />
### 键盘快捷键
使用 [Kbd](/components/kbd) 组件展示键盘快捷键。
<ComponentPreview name="input-group-with-keyboard-shortcut" />
### 徽章后缀
在后缀中加入徽章或 chip,用于展示状态或标签。
<ComponentPreview name="input-group-with-badge-suffix" />
### 表单校验
InputGroup 会自动反映父级 TextField 的无效状态。
<ComponentPreview name="input-group-invalid" />
### 前缀图标
在输入框前添加图标。
<ComponentPreview name="input-group-with-prefix-icon" />
### 后缀图标
在输入框后添加图标。
<ComponentPreview name="input-group-with-suffix-icon" />
### 前缀与后缀
同时组合前缀与后缀。
<ComponentPreview name="input-group-with-prefix-and-suffix" />
### 文本域
多行输入请使用 **InputGroupTextArea**,并搭配前缀与后缀。当存在 textarea 时,容器高度会自动适应内容,并将前缀/后缀与顶部对齐。
上游示例中的 Tooltip 由原生 `title` 与 `aria-label` 适配(Tooltip 组件尚未迁移)。
<ComponentPreview name="input-group-with-textarea" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="input-group-custom-styles" />
### 全局 CSS
InputGroup 使用可自定义的 CSS 类。覆盖组件类以匹配设计系统。
```css
@layer components {
.hv-input-group {
@apply bg-field text-field-foreground shadow-field rounded-field inline-flex min-h-9 items-center overflow-hidden border text-sm outline-none;
}
.hv-input-group__input {
@apply flex-1 rounded-none border-0 bg-transparent px-3 py-2 shadow-none outline-none;
}
.hv-input-group__prefix {
@apply text-field-placeholder rounded-l-field flex h-full items-center justify-center rounded-r-none bg-transparent px-3;
}
.hv-input-group__suffix {
@apply text-field-placeholder rounded-r-field flex h-full items-center justify-center rounded-l-none bg-transparent px-3;
}
/* Secondary variant */
.hv-input-group--secondary {
@apply shadow-none;
background-color: var(--color-default);
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
#### 基础类 [!toc]
- `.hv-input-group` – 根容器:带边框、背景与 flex 布局。默认 `min-h-9` 与 `items-center`;存在 textarea 时切换为 `items-start`
- `.hv-input-group__input` – 透明背景、无边框的输入元素。textarea 也使用该基础类
- `.hv-input-group__prefix` – 左侧圆角的前缀容器。与 textarea 搭配时与顶部对齐
- `.hv-input-group__suffix` – 右侧圆角的后缀容器。与 textarea 搭配时与顶部对齐
#### 变体类 [!toc]
- `.hv-input-group--primary` – 带阴影的主变体(默认)
- `.hv-input-group--secondary` – 无阴影的次变体,适用于 Surface 内
**Note:** 使用 `InputGroupTextArea` 时,容器从 `items-center` 切换为 `items-start`,并使用 `height: auto` 替代固定高度。前缀与后缀与顶部对齐,并增加内边距以匹配 textarea 的垂直内边距。textarea 使用相同的 `.hv-input-group__input` 基础类,并通过 `[data-slot="input-group-textarea"]` 选择器应用 textarea 专用样式(最小高度与纵向 resize)。
### 交互状态
InputGroup 会根据状态自动管理以下 data 属性:
- **Hover**:`[data-hovered]` - 悬停在组合上时应用
- **Focus Within**:`[data-focus-within]` - 输入聚焦时应用
- **Invalid**:`[data-invalid]` - 父级 TextField 无效时应用
- **Disabled**:`[data-disabled]` 或 `[aria-disabled]` - 父级 TextField 禁用时应用
## API 参考
### InputGroup
| Prop | 类型 | 默认值 | 描述 |
| ----------- | --------------------------------------- | -------------------------------- | ----------------------------------------- |
| 默认插槽 | `Component` | - | 子部件(Input、TextArea、Prefix、Suffix) |
| `variant` | `'primary' \| 'secondary'` | 继承 TextField,否则 `'primary'` | 视觉变体。显式值优先于 TextField 继承 |
| `fullWidth` | `boolean` | `false` | 输入组是否占满容器宽度 |
| `role` | `'group' \| 'region' \| 'presentation'` | `'group'` | 分组的无障碍角色 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
点击组合空白区域会聚焦内部真实 input/textarea(按钮等可交互子元素除外)。`aria-label`、`aria-labelledby`、`aria-describedby` 等 ARIA 属性透传到根节点。
### InputGroupInput
真实 `input` 元素,复用 `useTextControl` 与 TextField 上下文。
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------------------------- | -------- | ------------------------------------- |
| `modelValue` | `string` | - | 当前值(受控;也可由 TextField 管理) |
| `defaultValue` | `string` | - | 默认值(非受控) |
| `type` | `string` | `'text'` | 输入类型(text、password、email 等) |
| `invalid` | `boolean` | - | 标记无效状态(与父字段错误合并) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
`name`、`id`、`disabled`、`readonly`、`required`、原生约束与 ARIA 落在真实 `input` 上;从 TextField 继承;控件可额外启用 disabled/readonly/required,不能通过 false 解除父字段状态。
### InputGroupTextArea
真实 `textarea` 元素,复用同一套值/reset/ARIA 协议。
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------------------------- | ------ | -------------------------------- |
| `modelValue` | `string` | - | 当前值(受控) |
| `defaultValue` | `string` | - | 默认值(非受控) |
| `rows` | `number` | - | 可见文本行数 |
| `invalid` | `boolean` | - | 标记无效状态(与父字段错误合并) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### InputGroupPrefix
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ----------------------------- |
| 默认插槽 | `Component` | - | 前缀内容(图标、文字等) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
前后缀是展示容器,不是第二个表单字段,不会出现在 FormData 中。
### InputGroupSuffix
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ------------------------------ |
| 默认插槽 | `Component` | - | 后缀内容(图标、按钮、徽章等) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
`class` 遵循 Vue 的 class 绑定,可接收字符串、数组和对象并与基础类合并;不要使用 React 的 `className`。
## 相关组件
[Input 输入框](./input) · [Surface 表面](./surface) · [Button 按钮](./button)