组件Controls
Input
@mox-hub/ui 组件 · Controls · canonical
input@mox-hub/uiControlscanonicalsrc/components/input.tsx变体演示input
Props API
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
| className* | string | — | — |
| type* | HTMLInputTypeAttribute | — | — |
| controlSize* | "default" | "sm" | "lg" | "default" | — |
源码
import * as React from "react"
import { Input as InputPrimitive } from "@base-ui/react/input"
import { cn } from "../lib/utils"
function Input({
className,
type,
controlSize = "default",
...props
}: React.ComponentProps<"input"> & {
/** 控件高度档;`default` = 36px。不要与原生 input 的 `size`(字符宽度)混淆。 */
controlSize?: "sm" | "default" | "lg"
}) {
const sizeClass =
controlSize === "sm"
? "h-control-sm"
: controlSize === "lg"
? "h-control-lg"
: "h-control-md"
return (
<InputPrimitive
type={type}
data-slot="input"
data-control-size={controlSize}
className={cn(
"w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] duration-fast outline-none file:inline-flex file:h-control-2xs file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 read-only:cursor-default read-only:bg-muted/50 read-only:text-muted-foreground aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
sizeClass,
className
)}
{...props}
/>
)
}
// 项目扩展:密码框(显隐切换),历史 API 保留
import { Eye, EyeOff } from "lucide-react"
function PasswordInput({
className,
placeholder = "••••••••",
disabled,
labels,
...props
}: React.InputHTMLAttributes<HTMLInputElement> & {
/** 显隐按钮的无障碍名;不传时用内置 zh-CN 文案 */
labels?: { show?: string; hide?: string }
}) {
const [showPassword, setShowPassword] = React.useState(false)
return (
<div className="relative">
{/*
* `{...props}` 必须在内部 `type` 之前:调用方传 `type` 时不能覆盖显隐切换的
* 受控 type(此前展开顺序相反,传 type="text" 会让切换器永久失效)。
*/}
<Input
{...props}
type={showPassword ? "text" : "password"}
className={cn("pe-10", className)}
placeholder={placeholder}
disabled={disabled}
data-ai-component={props["data-ai-component"] ?? "ui.password-input"}
data-ai-role={props["data-ai-role"] ?? "password-input"}
/>
<button
type="button"
onClick={() => setShowPassword((visible) => !visible)}
disabled={disabled}
aria-label={
showPassword
? (labels?.hide ?? "隐藏密码")
: (labels?.show ?? "显示密码")
}
aria-pressed={showPassword}
className="absolute end-2 top-1/2 inline-flex size-control-xs -translate-y-1/2 items-center justify-center rounded-sm text-muted-foreground transition-[color,background-color] duration-fast outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50"
>
{showPassword ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
</button>
</div>
)
}
export { Input, PasswordInput }
可访问性与 RTL
- 键盘:交互件保留
:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。 - RTL:间距、对齐与方向性图标使用逻辑属性 (
ms-*/text-start/rtl:-scale-x-100);通过ThemeProvider或I18nProvider的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。 - 文案:优先用组件
labels/translate/translatorprops,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。 - 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。
用法说明
单行文本输入,透传原生 input 属性;前后缀与按钮组合用 input-group。