Skip to content

Input 输入框

新粗野主义风格的文本输入框,支持变体、尺寸和 v-model 双向绑定。

预览

Preview

变体

尺寸

状态

图标与装饰

https://
.com

辅助能力

0 / 24

v-model 绑定

值:

安装

pnpm dlx brutx-vue@latest add input

用法

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const value = ref('')
</script>

<template>
    <Input v-model="value" placeholder="Enter your name..." />
</template>

搭配 Label

vue
<script setup>
import { ref } from 'vue'
import { Input, Label } from 'brutx-ui-vue'

const email = ref('')
</script>

<template>
    <div class="space-y-2">
        <Label for="email">Email</Label>
        <Input id="email" v-model="email" type="email" placeholder="you@example.com" />
    </div>
</template>

禁用状态

vue
<script setup>
import { Input } from 'brutx-ui-vue'
</script>

<template>
    <Input disabled placeholder="Disabled input" />
</template>

只读状态

通过 readonly 属性设置只读输入框。只读状态下输入框不可编辑但可聚焦选择文本,光标样式为 cursor-default,不会降低透明度(区别于 disabled)。

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const value = ref('只读内容,可选中复制但不可编辑')
</script>

<template>
    <Input v-model="value" readonly />
</template>

错误消息

通过 variant="error"errorMessage 属性显示错误消息。错误消息使用 role="alert" 确保屏幕阅读器自动播报。

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const email = ref('')
</script>

<template>
    <Input
        v-model="email"
        type="email"
        variant="error"
        error-message="请输入有效的邮箱地址"
        placeholder="you@example.com"
    />
</template>

可清除

设置 clearable 属性后,鼠标悬停时会显示清除按钮。

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const value = ref('点击清除')
</script>

<template>
    <Input v-model="value" clearable @clear="value = ''" />
</template>

密码切换

设置 show-password 属性后,密码输入框会显示可见性切换按钮。

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const password = ref('')
</script>

<template>
    <Input v-model="password" type="password" show-password placeholder="请输入密码" />
</template>

字数统计

同时设置 show-word-limitmaxlength 属性后,会显示字数统计。

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const value = ref('')
</script>

<template>
    <Input v-model="value" maxlength="100" show-word-limit placeholder="最多 100 个字符" />
</template>

图标与前后缀

优先使用 Input 自身的 prefixIconsuffixIcon props 和 prependappend 插槽来实现输入框装饰。

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'
import { Search } from '@lucide/vue'

const url = ref('')
</script>

<template>
    <Input :prefix-icon="Search" placeholder="搜索项目" />

    <Input v-model="url" placeholder="请输入网址">
        <template #prepend>https://</template>
        <template #append>.com</template>
    </Input>
</template>

变体

变体说明
default标准边框
error错误边框,聚焦时使用 Primary 阴影
success成功边框,聚焦时使用 Secondary 阴影

尺寸

尺寸高度内边距字体大小
smh-9px-3 py-1text-sm
defaulth-11px-4 py-2text-base
lgh-14px-5 py-3text-lg

Props

属性类型默认值说明
typeHTMLInputType ¹'text'输入框类型
modelValuestringv-model 绑定值
variant'default' | 'error' | 'success''default'输入框变体
size'sm' | 'default' | 'lg''default'输入框尺寸
disabledbooleanfalse是否禁用
readonlybooleanfalse是否只读
placeholderstring占位符文本
maxlengthnumber最大输入长度
clearablebooleanfalse悬停时显示清除按钮
showPasswordbooleanfalse显示密码切换按钮(仅 type="password" 有效)
showWordLimitbooleanfalse显示字数统计(需配合 maxlength
prefixIconComponent前缀图标
suffixIconComponent后缀图标
errorMessagestring错误消息文本,仅在 variant="error" 时显示
ariaLabelstringARIA 标签
ariaLabelledbystringARIA 标签引用 ID
ariaDescribedbystringARIA 描述引用 ID
ariaInvalidbooleanARIA 无效状态
ariaRequiredbooleanARIA 必填状态
classstring自定义 CSS 类名

¹ HTMLInputTypeHTMLInputElement.type 支持的所有标准类型联合:'button' | 'checkbox' | 'color' | 'date' | 'datetime-local' | 'email' | 'file' | 'hidden' | 'image' | 'month' | 'number' | 'password' | 'radio' | 'range' | 'reset' | 'search' | 'submit' | 'tel' | 'text' | 'time' | 'url' | 'week'

事件

事件参数说明
update:modelValuestring值更新时触发
clear点击清除按钮时触发

插槽

插槽说明
prepend输入框前置内容(如 URL 协议)
append输入框后置内容(如域名)

方法(defineExpose)

通过 ref 访问组件实例后可调用以下方法:

方法说明
focus()聚焦输入框
blur()移除焦点
select()选中输入框中的文本
vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const inputRef = ref(null)

function handleFocus() {
    inputRef.value?.focus()
}
</script>

<template>
    <Input ref="inputRef" placeholder="Click button to focus" />
    <button @click="handleFocus">Focus Input</button>
</template>

可访问性

通过 ARIA 属性增强输入框的无障碍可访问性,便于辅助技术(如屏幕阅读器)正确朗读:

vue
<script setup>
import { ref } from 'vue'
import { Input } from 'brutx-ui-vue'

const email = ref('')
</script>

<template>
    <Input
        v-model="email"
        type="email"
        aria-label="邮箱地址"
        aria-required="true"
        aria-invalid="false"
        placeholder="you@example.com"
    />
</template>

蛮力铸就。