Skip to content

Input 输入框

输入框用于通过鼠标或键盘输入字符。VISTA B 直接使用 Element Plus 的 el-input,样式沿用组件库,适合表单录入、筛选条件、备注说明、搜索、金额输入、密码输入等场景。

基础用法

使用 v-model 绑定输入值,通过 placeholder 设置占位文本。

vue
<script setup>
import { ref } from 'vue'

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

<template>
  <el-input v-model="input" style="width: 240px" placeholder="请输入项目名称" />
</template>

禁用状态

设置 disabled 后,输入框不可输入。

vue
<el-input v-model="input" disabled placeholder="不可编辑" />

一键清空

设置 clearable 后,输入框会显示清除按钮。Element Plus 2.13.4 起,textarea 类型也支持 clearable

vue
<el-input v-model="input" clearable placeholder="请输入项目名称" />

<el-input
  v-model="textarea"
  type="textarea"
  clearable
  placeholder="请输入备注"
/>

自定义清除图标

通过 clear-icon 可以替换清除按钮图标。

vue
<script setup>
import { CloseBold } from '@element-plus/icons-vue'
</script>

<template>
  <el-input
    v-model="input"
    clearable
    :clear-icon="CloseBold"
    placeholder="自定义清除图标"
  />
</template>

格式化

通过 formatter 控制展示值,通过 parser 把展示值还原为绑定值,常用于金额、编号、电话等格式化输入。

vue
<el-input
  v-model="input"
  placeholder="请输入预算"
  :formatter="(value) => `¥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
  :parser="(value) => value.replace(/\s?|(,*)/g, '')"
/>

密码框

设置 type="password"show-password 后,可以切换密码显示状态。password-icon 插槽可自定义显示和隐藏图标。

vue
<el-input
  v-model="password"
  type="password"
  placeholder="请输入密码"
  show-password
/>

<el-input v-model="password" type="password" show-password>
  <template #password-icon="{ visible }">
    <el-icon :size="16">
      <Unlock v-if="visible" />
      <Lock v-else />
    </el-icon>
  </template>
</el-input>

带图标的输入框

通过 prefix-iconsuffix-iconprefixsuffix 插槽,可以在输入框内添加图标。

vue
<el-input v-model="date" :suffix-icon="Calendar" placeholder="选择日期" />
<el-input v-model="keyword" :prefix-icon="Search" placeholder="搜索项目" />

<el-input v-model="keyword" placeholder="前缀插槽">
  <template #prefix>
    <el-icon class="el-input__icon"><Search /></el-icon>
  </template>
</el-input>

文本域

设置 type="textarea" 后,输入框会变为多行文本域。可通过 rows 控制默认行数。

vue
<el-input
  v-model="textarea"
  :rows="3"
  type="textarea"
  placeholder="请输入项目备注"
/>

自适应文本域

设置 autosize 后,文本域高度会根据内容自动调整。也可以传入对象,限制最小和最大行数。

vue
<el-input v-model="textarea1" autosize type="textarea" />

<el-input
  v-model="textarea2"
  :autosize="{ minRows: 2, maxRows: 4 }"
  type="textarea"
/>

复合型输入框

通过 prependappend 插槽,可以在输入框前后添加标签、选择器或按钮。

https://
.com
vue
<el-input v-model="url" placeholder="请输入链接">
  <template #prepend>https://</template>
</el-input>

<el-input v-model="keyword" placeholder="请输入关键词">
  <template #prepend>
    <el-select v-model="type" placeholder="类型" style="width: 120px">
      <el-option label="项目" value="project" />
      <el-option label="客户" value="customer" />
    </el-select>
  </template>
  <template #append>
    <el-button :icon="Search" />
  </template>
</el-input>

调整尺寸

使用 size 设置输入框尺寸,支持 large、默认、smalltextarea 不支持 size

vue
<el-input v-model="input" size="large" placeholder="Large" />
<el-input v-model="input" placeholder="Default" />
<el-input v-model="input" size="small" placeholder="Small" />

输入长度限制

通过 maxlengthminlength 限制输入长度。设置 show-word-limit 后,会显示字数统计;word-limit-position="outside" 可将统计放到输入框外。

0 / 20
0 / 20
0 / 80
vue
<el-input
  v-model="text"
  maxlength="20"
  show-word-limit
  placeholder="最多 20 个字符"
/>

<el-input
  v-model="textarea"
  maxlength="80"
  type="textarea"
  show-word-limit
/>

计数字形

设置 count-graphemes 后,字数统计会使用自定义函数计算文本长度,适合需要正确处理 emoji、组合字符或复杂 Unicode 的输入场景。

3 / 10
5 / 30
vue
const countGraphemes = (value) => {
  if (typeof Intl !== 'undefined' && Intl.Segmenter) {
    return [...new Intl.Segmenter().segment(value)].length
  }
  return Array.from(value).length
}

<el-input
  v-model="text"
  maxlength="10"
  show-word-limit
  :count-graphemes="countGraphemes"
/>

API

Input 属性

属性说明类型默认值
type输入框类型,同原生 input typestringtext
model-value / v-model绑定值string / number
model-modifiersv-model 修饰符object
maxlength原生 maxlength 属性string / number
minlength原生 minlength 属性string / number
show-word-limit是否显示字数统计,仅 text / textarea 有效booleanfalse
word-limit-position字数统计位置inside / outsideinside
placeholder输入框占位文本string
clearable是否显示清除按钮booleanfalse
clear-icon自定义清除图标string / ComponentCircleClose
formatter指定输入值的展示格式,仅 text 有效(value: string | number) => string
parser从格式化展示中解析出绑定值,仅 text 有效(value: string) => string
show-password是否显示切换密码图标booleanfalse
disabled是否禁用booleanfalse
size输入框尺寸,textarea 不生效large / default / small
prefix-icon自定义前缀图标string / Component
suffix-icon自定义后缀图标string / Component
rows输入框行数,仅 textarea 有效number2
autosizetextarea 高度是否自适应boolean / { minRows?: number, maxRows?: number }false
autocomplete原生 autocomplete 属性stringoff
name原生 name 属性string
readonly是否只读booleanfalse
max原生 max 属性string / number
min原生 min 属性string / number
step原生 step 属性string / number
resize控制 textarea 是否可被缩放none / both / horizontal / vertical
autofocus是否自动获取焦点booleanfalse
form原生 form 属性string
aria-label原生 aria-label 属性string
tabindex输入框的 tabindexstring / number
validate-event输入时是否触发表单校验booleantrue
input-styleinput 或 textarea 元素的样式string / object{}
inputmode原生 inputmode 属性string
count-graphemes自定义字形计数函数(value: string) => number

Input 事件

事件名说明类型
blur输入框失去焦点时触发(event: FocusEvent) => void
focus输入框获得焦点时触发(event: FocusEvent) => void
change绑定值变化且失焦或按下 Enter 时触发(value: string | number) => void
input输入值变化时触发(value: string | number) => void
clear点击清除按钮时触发() => void
keydown按下键盘按键时触发(event: KeyboardEvent) => void
mouseleave鼠标离开输入框时触发(event: MouseEvent) => void
mouseenter鼠标进入输入框时触发(event: MouseEvent) => void
compositionstart输入法输入开始时触发(event: CompositionEvent) => void
compositionupdate输入法输入变化时触发(event: CompositionEvent) => void
compositionend输入法输入结束时触发(event: CompositionEvent) => void

Input 插槽

插槽说明
prefix输入框头部内容,仅非 textarea 有效
suffix输入框尾部内容,仅非 textarea 有效
prepend输入框前置内容,仅非 textarea 有效
append输入框后置内容,仅非 textarea 有效
password-icon密码图标内容,仅 show-passwordtrue 时生效,参数为 { visible: boolean }

Input 暴露

方法名说明类型
blur使 input 失去焦点() => void
clear清除 input 值() => void
focus使 input 获取焦点() => void
inputinput HTML 元素object
refinput 或 textarea HTML 元素object
resizeTextarea改变 textarea 大小() => void
select选中 input 中的文字() => void
textareatextarea HTML 元素object
textareaStyletextarea 的样式object
isComposing是否处于输入法组合输入状态object
passwordVisible密码是否可见object

HOMEVISTA 设计规范