Input(单行输入)
用于表单和用户数据录入的文本输入组件,内置样式和无障碍功能。
loading
安装
bash
npx shadcn@latest add input1
1. 将以下代码复制并粘贴到项目中。
tsx
import * as React from "react"
import { Input as InputPrimitive } from "@base-ui/react/input"
import { cn } from "cn"
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
return (
<InputPrimitive
type={type}
data-slot="input"
className={cn(
"h-9 w-full min-w-0 rounded-4xl border border-input bg-transparent dark:bg-input/30 px-3 py-1 type-body transition-colors outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:type-body file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
className
)}
{...props}
/>
)
}
export { Input }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2. 根据项目配置调整导入路径。
用法
tsx
import { Input } from "@/components/ui/input"1
tsx
<Input />1
基础
loading
表单字段
使用 Field、FieldLabel 和 FieldDescription 创建带 标签和描述的输入框。
loading
字段组
使用 FieldGroup 展示多个 Field 区块并构建表单。
loading
禁用
使用 disabled 属性禁用输入框。要设置禁用状态的样式,可在 Field 组件上添加 data-disabled 属性。
loading
无效状态
使用 aria-invalid 属性标记输入无效。要设置无效状态的样式,可在 Field 组件上添加 data-invalid 属性。
loading
文件
使用 type="file" 属性创建文件输入框。
loading
行内排列
在 Field 上使用 orientation="horizontal" 创建行内输入框。 搭配 Button 可创建带按钮的搜索输入框。
loading
网格
使用网格布局并排放置多个输入框。
loading
必填
使用 required 属性标记必填输入框。
loading
徽标
在标签中使用 Badge 突出显示推荐字段。
loading
输入组
使用 InputGroup 组件在输入框内添加图标、文本或按钮。更多示例请参阅输入组组件。
loading
按钮组
使用 ButtonGroup 组件为输入框添加按钮。更多示例请参阅按钮组组件。
loading
表单
一个完整的表单示例,包含多个输入框、一个选择器和一个按钮。
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading