Skip to content

Input 输入框

通过鼠标或键盘输入字符

ts
import { createApp } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'

const app = createApp()
app.use(ElInput)

基础用法

<template>
  <el-input v-model="input" style="width: 240px" placeholder="Please input" />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


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

禁用状态

<template>
  <el-input
    v-model="input"
    style="width: 240px"
    disabled
    placeholder="Please input"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


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

一键清空

<template>
  <div class="input-group">
    <el-input
      v-model="input"
      style="width: 240px"
      placeholder="Please input"
      clearable
    />
    <el-input
      v-model="textareaInput"
      style="width: 240px"
      placeholder="Please input"
      type="textarea"
      clearable
    />
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


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

<style scoped>
.input-group {
  display: flex;
  align-items: center;
  gap: 1em;
}
</style>

自定义清除图标 (2.11.0)

<template>
  <div class="input-group">
    <el-input
      v-model="input"
      clearable
      :clear-icon="CloseBold"
      placeholder="Custom clear icon"
    />
    <el-input
      v-model="textareaInput"
      clearable
      :clear-icon="CloseBold"
      placeholder="Custom clear icon"
      type="textarea"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { CloseBold } from '@szhn/dh-design-pc/icons'
import { Input as ElInput } from '@szhn/dh-design-pc'


const input = ref('Custom clear icon')
const textareaInput = ref('Custom clear icon')
</script>

<style scoped>
.input-group {
  display: flex;
  flex-direction: column;
  gap: 1em;
}
</style>

格式化

formatter的情况下显示值,我们通常同时使用 parser

<template>
  <el-input
    v-model="input"
    style="width: 240px"
    placeholder="Please input"
    :formatter="format"
    :parser="parse"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


const input = ref('')

const format = (value: string | number) => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
const parse = (value: string) => value.replace(/\$\s?|(,*)/g, '')
</script>

密码框

<template>
  <div class="input-group">
    <el-input
      v-model="input"
      style="width: 240px"
      type="password"
      placeholder="Please input password"
      show-password
    />
    <el-input
      v-model="input"
      style="width: 240px"
      type="password"
      placeholder="Please input password"
      show-password
    >
      <template #password-icon="{ visible }">
        <el-icon :size="16">
          <Unlock v-if="visible" />
          <Lock v-else />
        </el-icon>
      </template>
    </el-input>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Lock, Unlock } from '@szhn/dh-design-pc/icons'
import { Icon as ElIcon, Input as ElInput } from '@szhn/dh-design-pc'


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

<style scoped>
.input-group {
  display: flex;
  align-items: center;
  gap: 1em;
}
</style>

带图标的输入框

带有图标标记输入类型

<template>
  <div class="demo-input-with-icon">
    <div class="input-group">
      <span class="label">Using attributes</span>
      <div class="input-container">
        <el-input
          v-model="input1"
          class="responsive-input"
          placeholder="Pick a date"
          :suffix-icon="Calendar"
        />
        <el-input
          v-model="input2"
          class="responsive-input"
          placeholder="Type something"
          :prefix-icon="Search"
        />
      </div>
    </div>
    <div class="input-group">
      <span class="label">Using slots</span>
      <div class="input-container">
        <el-input
          v-model="input3"
          class="responsive-input"
          placeholder="Pick a date"
        >
          <template #suffix>
            <el-icon class="el-input__icon"><calendar /></el-icon>
          </template>
        </el-input>
        <el-input
          v-model="input4"
          class="responsive-input"
          placeholder="Type something"
        >
          <template #prefix>
            <el-icon class="el-input__icon"><search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Calendar, Search } from '@szhn/dh-design-pc/icons'
import { Icon as ElIcon, Input as ElInput } from '@szhn/dh-design-pc'


const input1 = ref('')
const input2 = ref('')
const input3 = ref('')
const input4 = ref('')
</script>

<style scoped>
.demo-input-with-icon {
  width: 100%;
}

.input-group {
  margin-bottom: 1.5rem;
}

.label {
  display: block;
  margin-bottom: 1rem;
  color: var(--el-text-color-regular);
}

.input-container {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.responsive-input {
  width: 240px;
}

@media (max-width: 768px) {
  .input-container {
    flex-direction: column;
    gap: 1rem;
  }

  .responsive-input {
    width: 100%;
  }
}
</style>

文本域

用于输入多行文本信息可缩放的输入框。 添加 type="textarea" 属性来将 input 元素转换为原生的 textarea 元素。

<template>
  <el-input
    v-model="textarea"
    style="width: 240px"
    :rows="2"
    type="textarea"
    placeholder="Please input"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


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

自适应文本域

设置文字输入类型的 autosize 属性使得根据内容自动调整的高度。 你可以给 autosize 提供一个包含有最大和最小高度的对象,让输入框自动调整。

<template>
  <el-input
    v-model="textarea1"
    style="width: 240px"
    autosize
    type="textarea"
    placeholder="Please input"
  />
  <div style="margin: 20px 0" />
  <el-input
    v-model="textarea2"
    style="width: 240px"
    :autosize="{ minRows: 2, maxRows: 4 }"
    type="textarea"
    placeholder="Please input"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


const textarea1 = ref('')
const textarea2 = ref('')
</script>

复合型输入框

可以在输入框中前置或后置一个元素,通常是标签或按钮。

<template>
  <div>
    <el-input
      v-model="input1"
      style="max-width: 600px"
      placeholder="Please input"
    >
      <template #prepend>Http://</template>
    </el-input>
  </div>
  <div class="mt-4">
    <el-input
      v-model="input2"
      style="max-width: 600px"
      placeholder="Please input"
    >
      <template #append>.com</template>
    </el-input>
  </div>
  <div class="mt-4">
    <el-input
      v-model="input3"
      style="max-width: 600px"
      placeholder="Please input"
      class="input-with-select"
    >
      <template #prepend>
        <el-select v-model="select" placeholder="Select" style="width: 115px">
          <el-option label="Restaurant" value="1" />
          <el-option label="Order No." value="2" />
          <el-option label="Tel" value="3" />
        </el-select>
      </template>
      <template #append>
        <el-button :icon="Search" />
      </template>
    </el-input>
  </div>
  <div class="mt-4">
    <el-input
      v-model="input3"
      style="max-width: 600px"
      placeholder="Please input"
      class="input-with-select"
    >
      <template #prepend>
        <el-button :icon="Search" />
      </template>
      <template #append>
        <el-select v-model="select" placeholder="Select" style="width: 115px">
          <el-option label="Restaurant" value="1" />
          <el-option label="Order No." value="2" />
          <el-option label="Tel" value="3" />
        </el-select>
      </template>
    </el-input>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Search } from '@szhn/dh-design-pc/icons'
import { Button as ElButton, Input as ElInput, Option as ElOption, Select as ElSelect } from '@szhn/dh-design-pc'


const input1 = ref('')
const input2 = ref('')
const input3 = ref('')
const select = ref('')
</script>

<style>
.input-with-select .el-input-group__prepend {
  background-color: var(--el-fill-color-blank);
}
</style>

尺寸

<template>
  <div class="flex gap-4 mb-4 items-center">
    <el-input
      v-model="input1"
      style="width: 240px"
      size="large"
      placeholder="Please Input"
    />
    <el-input
      v-model="input2"
      style="width: 240px"
      placeholder="Please Input"
    />
    <el-input
      v-model="input3"
      style="width: 240px"
      size="small"
      placeholder="Please Input"
    />
  </div>
  <div class="flex gap-4 mb-4 items-center">
    <el-input
      v-model="input1"
      style="width: 240px"
      size="large"
      placeholder="Please Input"
      :suffix-icon="Search"
    />
    <el-input
      v-model="input2"
      style="width: 240px"
      placeholder="Please Input"
      :suffix-icon="Search"
    />
    <el-input
      v-model="input3"
      style="width: 240px"
      size="small"
      placeholder="Please Input"
      :suffix-icon="Search"
    />
  </div>
  <div class="flex gap-4 items-center">
    <el-input
      v-model="input1"
      style="width: 240px"
      size="large"
      placeholder="Please Input"
      :prefix-icon="Search"
    />
    <el-input
      v-model="input2"
      style="width: 240px"
      placeholder="Please Input"
      :prefix-icon="Search"
    />
    <el-input
      v-model="input3"
      style="width: 240px"
      size="small"
      placeholder="Please Input"
      :prefix-icon="Search"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Search } from '@szhn/dh-design-pc/icons'
import { Input as ElInput } from '@szhn/dh-design-pc'


const input1 = ref('')
const input2 = ref('')
const input3 = ref('')
</script>

输入长度限制

<template>
  <el-input
    v-model="text"
    style="width: 240px"
    maxlength="10"
    placeholder="Please input"
    show-word-limit
    type="text"
  />
  <el-input
    v-model="text"
    class="ml-4"
    style="width: 240px"
    maxlength="10"
    placeholder="Please input"
    show-word-limit
    word-limit-position="outside"
    type="text"
  />
  <div style="margin: 20px 0" />
  <el-input
    v-model="textarea"
    maxlength="30"
    style="width: 240px"
    placeholder="Please input"
    show-word-limit
    type="textarea"
  />
  <el-input
    v-model="textarea"
    class="ml-4"
    maxlength="30"
    style="width: 240px"
    placeholder="Please input"
    show-word-limit
    word-limit-position="outside"
    type="textarea"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


const text = ref('')
const textarea = ref('')
</script>

计数图形 (2.13.7)

:::小技巧

浏览器支持 & 回退策略

当使用 count-graphemes prop时,组件使用以下方法:

在实现自己的 count-graphemes 函数时,如果需要对复杂的 unicode 字符提供强大的支持,请考虑使用 Intl.Segmenter

:::

<template>
  <el-input
    v-model="text"
    maxlength="10"
    placeholder="Please input"
    show-word-limit
    :count-graphemes="calc"
    type="text"
  />
  <div style="margin: 20px 0" />
  <el-input
    v-model="textarea"
    maxlength="20"
    placeholder="Please input"
    show-word-limit
    :count-graphemes="calc"
    type="textarea"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Input as ElInput } from '@szhn/dh-design-pc'


const text = ref('😀😁')
const textarea = ref('😀😁')

/**
 * Count function for grapheme clustering.
 * Counts the number of graphemes (visual characters) in a string.
 *
 * Note: This example uses Array.from() for simplicity.
 * For production use, consider using Intl.Segmenter for better
 * handling of complex emoji and unicode characters.
 */
const calc = (value: string) => {
  // Simplified version: counts code points (not full grapheme clusters)
  // For production, use Intl.Segmenter like the component does
  return Array.from(value).length
}
</script>

常见问题

典型问题: #7287

PS: 由于ElInput 组件没有默认宽度,当显示 clearable 图标时, 组件的宽度将被撑开,可以通过设置固定宽度属性来解决。

API

Attributes

属性名说明类型默认值
type输入类型,更多信息请参见 MDNstringtext
model-value / v-model绑定值string / number
model-modifiers 2.11.5v-model 修饰符,参考 Vue 修饰符object
maxlength同原生 maxlength 属性string / number
minlength原生属性,最小输入长度string / number
show-word-limit是否显示统计字数, 只在 type 为 'text' 或 'textarea' 的时候生效booleanfalse
word-limit-position 2.11.5字数统计的位置,仅当 show-word-limittrue 时生效。enum"inside"
placeholder输入框占位文本string
clearable是否显示清除按钮,只有当 type 不是 textarea时生效booleanfalse
clear-icon 2.11.0自定义清除图标string / objectCircleClose
formatter指定输入值的格式。(只有当 type 是"text"时才能工作)Function
parser指定从格式化器输入中提取的值。(仅当 type 是"text"时才起作用)Function
show-password是否显示切换密码图标booleanfalse
disabled是否禁用booleanfalse
size输入框尺寸,只在 type 不为 'textarea' 时有效enum
prefix-icon自定义前缀图标string / Component
suffix-icon自定义后缀图标string / Component
rows输入框行数,仅 type 为 'textarea' 时有效number2
autosizetextarea 高度是否自适应,仅 type 为 'textarea' 时生效。 可以接受一个对象,比如: { minRows: 2, maxRows: 6 }boolean / objectfalse
autocomplete原生 autocomplete 属性stringoff
name等价于原生 input name 属性string
readonly原生 readonly 属性,是否只读booleanfalse
max原生 max 属性,设置最大值:::
min原生属性,设置最小值
step原生属性,设置输入字段的合法数字间隔:::
resize控制是否能被用户缩放enum
autofocus原生属性,自动获取焦点booleanfalse
form原生属性string
aria-label a11y 2.7.2等价于原生 input aria-label 属性string
tabindex输入框的 tabindexstring / number
validate-event输入时是否触发表单的校验booleantrue
input-styleinput 元素或 textarea 元素的 stylestring / object{}
label a11y deprecated等价于原生 input aria-label 属性string
inputmode 2.10.3等价于原生 input inputmode 属性string
count-graphemes 2.13.7自定义函数用于计算字形;设置后,将绕过原生的 maxlength/minlength 约束。 组件使用 Intl.Segmenter(Chrome 87+、Firefox 125+、Safari 14.1+)进行正确的字素聚类;旧版浏览器回退到使用 Array.from() 进行代码点迭代。Function:::

Events

事件名说明类型
blur当选择器的输入框失去焦点时触发Function
focus当选择器的输入框获得焦点时触发Function
change仅当 modelValue 改变时,当输入框失去焦点或用户按Enter时触发Function
input在 Input 值改变时触发Function
clear在点击由 clearable 属性生成的清空按钮时触发Function
keydown按下键时触发Function
mouseleave当鼠标离开输入元素时触发Function
mouseenter当鼠标进入输入元素时触发Function
compositionstart输入法输入开始时触发Function
compositionupdate输入法输入改变时触发Function
compositionend输入法输入完成时触发Function

Slots

插槽名说明
prefix输入框头部内容,只对非 type="textarea" 有效
suffix输入框尾部内容,只对非 type="textarea" 有效
prepend输入框前置内容,只对非 type="textarea" 有效
append输入框后置内容,只对非 type="textarea" 有效
password-icon 2.13.6作为输入密码图标的内容,仅在 show-password 为 true 时生效。 作用域变量为 { visible: boolean }

Exposes

名称说明类型
blur使 input 失去焦点Function
clear清除 input 值Function
focus使 input 获取焦点Function
inputInput HTML 元素object
refHTML元素 input 或 textareaobject
resizeTextarea改变 textarea 大小Function
select选中 input 中的文字Function
textareaHTML textarea 元素object
textareaStyletextarea 的样式object
isComposing 2.8.0是否是输入 composing 状态object
passwordVisible 2.13.7密码是否可见object