Skip to content

Input 输入框 ​

输入框封装,配合z-form风味更佳。

基础用法 ​

基础的z-input用法。

<script lang="ts" setup>
import { ref } from 'vue'

const val = ref('')

function handleInputChange(val: string) {
  // eslint-disable-next-line no-console
  console.log(val)
}
</script>

<template>
  <z-input v-model="val" clearable placeholder="Please enter" @change="handleInputChange" />
</template>

带图标的输入框 ​

可以简单地使用 prefix-icon 和 suffix-icon 属性。 另外, 使用prefix 和 suffix 传递函数或者插槽也能达到相同效果。

<script lang="ts" setup>
import { h, ref, resolveComponent } from 'vue'

const val = ref('')
const value = ref('')

const setPrefixIcon = () => {
  return h(resolveComponent('el-icon'), {}, () => h(resolveComponent('i-search')))
}

const setSuffixIcon = () => {
  return h(resolveComponent('el-icon'), {}, () => h(resolveComponent('i-calendar')))
}
</script>

<template>
  <z-input v-model="val" clearable placeholder="Please enter" prefix-icon="i-search" suffix-icon="i-calendar" class="mb-2" />
  <z-input v-model="value" clearable placeholder="Please enter" :prefix="setPrefixIcon" :suffix="setSuffixIcon" class="mb-2" />
  <z-input v-model="value" clearable placeholder="Please enter">
    <template #prefix>
      <el-icon>
        <i-search />
      </el-icon>
    </template>
    <template #suffix>
      <el-icon>
        <i-calendar />
      </el-icon>
    </template>
  </z-input>
</template>

复合型输入框 ​

可以简单地使用 append 和 prepend 属性。 另外, 使用append 和 prepend 传递函数或者插槽也能达到相同效果。

<script lang="ts" setup>
import { h, ref, resolveComponent } from 'vue'

const val = ref('')
const value = ref('')

const setAppend = () => {
  return h(resolveComponent('el-icon'), {}, () => h(resolveComponent('i-search')))
}

const setPrepend = () => {
  return h(resolveComponent('el-icon'), {}, () => h(resolveComponent('i-calendar')))
}
</script>

<template>
  <z-input v-model="val" clearable placeholder="Please enter" prepend="Http://" append=".com" class="mb-2" />
  <z-input v-model="value" clearable placeholder="Please enter" :append="setAppend" :prepend="setPrepend" class="mb-2" />
  <z-input v-model="value" clearable placeholder="Please enter">
    <template #append>
      <span>.com</span>
    </template>
    <template #prepend>
      <span>Http://</span>
    </template>
  </z-input>
</template>

z-input 属性 ​

属性名说明类型默认值
type类型string native input typestext
model-value / v-model绑定值string / number—
prepend输入框前置内容string / () => VNode—
append输入框后置内容string / () => VNode—
prefix输入框头部内容string / () => VNode—
suffix输入框尾部内容string / ()=> VNode—
maxlength最大输入长度string / number—
minlength原生属性,最小输入长度number—
show-word-limit是否显示统计字数, 只在 type 为 'text' 或 'textarea' 的时候生效booleanboolean
placeholder输入框占位文本string—
clearable是否显示清除按钮,只有当 type 不是 textarea时生效booleanfalse
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' 时有效numbernumber
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—
tabindex输入框的 tabindexstring / number—
validate-event输入时是否触发表单的校验booleantrue
input-styleinput 元素或 textarea 元素的 stylestring / object{}

z-input 事件 ​

事件名说明类型
blur当选择器的输入框失去焦点时触发Function
focus当选择器的输入框获得焦点时触发Function
change仅当 modelValue 改变时,当输入框失去焦点或用户按Enter时触发Function
input在 Input 值改变时触发Function
clear在点击由 clearable 属性生成的清空按钮时触发Function

z-input 方法 ​

名称说明类型
blur使 input 失去焦点Function
clear清除 input 值Function
focus使 input 获取焦点Function
resizeTextarea改变 textarea 大小Function
select选中 input 中的文字Function

z-input 插槽 ​

插槽名说明子标签
prepend输入框前置内容—
append输入框后置内容—
prefix输入框头部内容—
suffix输入框尾部内容—

Released under the MIT License.