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

99 lines
3.6 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: Input 输入框
description: 接受标准 HTML 属性的原生单行文本输入组件
component: input
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="input-basic" />
::: info 字段组合
校验、标签与错误消息由 TextField 体系负责。TextField 尚未迁移,当前请使用原生 label、aria-describedby 和 aria-invalid 关联。
:::
## 示例
### 变体
Input 组件支持两种视觉变体:
- **primary**(默认):标准样式带阴影,适用于大多数场景。
- **secondary**:低强调变体无阴影,适用于 Surface 组件内。
<ComponentPreview name="input-variants" />
### 表面样式
在表面容器内使用 `variant="secondary"` 以应用适合背景的低强调变体。
<ComponentPreview name="input-on-surface" />
### 宽度充满
<ComponentPreview name="input-full-width" />
### 输入类型
<ComponentPreview name="input-types" />
### 受控组件
<ComponentPreview name="input-controlled" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="input-custom-styles" />
### 全局 CSS
```css
@layer components {
.hv-input {
border-radius: 8px;
}
}
```
## 样式参考
基础类 `.hv-input`;变体 `.hv-input--primary` / `.hv-input--secondary`;宽度修饰符 `.hv-input--full-width`。保留原生 hover / focus / disabled 和 `data-invalid` 状态。
## API 参考
### Input
Input 接受标准 HTML input 属性,以及以下 props:
| Prop | 类型 | 默认值 | 描述 |
| --------------------------------------------------- | -------------------------- | ----------- | --------------------------------- |
| `modelValue` | `string` | — | 受控值,使用 v-model |
| `defaultValue` | `string` | `''` | 非受控初始值 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体 |
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
| `type` | `string` | `'text'` | 原生输入类型 |
| `invalid` | `boolean` | `false` | 设置 aria-invalid 和 data-invalid |
| `disabled` / `readonly` / `required` | `boolean` | `false` | 原生表单状态 |
| `name` / `placeholder` / `autocomplete` / `pattern` | `string` | — | 原生输入属性 |
| `maxlength` / `minlength` | `number` | — | 长度限制 |
| `min` / `max` / `step` | `string / number` | — | 数值与日期约束 |
### 事件与方法
`update:modelValue` 传递字符串;`@input` / `@change` 保留原生事件。`focus()` 可通过模板引用调用。原生表单 reset 还原非受控初始值;受控值由父组件管理。
不把 React 的 `value` + `onChange` 直接复制为 Vue 状态 API。`className` 改为 `class`。TextField 上下文的自动继承尚未实现。
## 相关组件
[Button 按钮](./button) · [Card 卡片](./card)