Skip to content

Input 输入框

提示

0.2.0版本已将 Input 组件的 textarea 文本域功能迁移至 Textarea组件,所有API保持一致。

基本用法

可以通过 v-model 双向绑定输入框的值,通过 placeholder 设置占位提示文字。

typescript
const value = ref<string>('')
function handleChange(event) {
  console.log(event)
}
html
<wd-input type="text" v-model="value" placeholder="请输入用户名" @change="handleChange" />

禁用

设置 disabled 属性。

html
<wd-input v-model="value" disabled />

只读

设置 readonly 属性。

html
<wd-input v-model="value" readonly />

清空按钮

设置 clearable 属性。

html
<wd-input v-model="value" clearable @change="handleChange"/>

密码输入框

设置 show-password 属性。

html
<wd-input v-model="value" clearable show-password @change="handleChange"/>

前后icon

设置前置icon prefix-icon,设置后置icon suffix-icon,icon 为 icon 章节中的图标,如果没有你需要的图标,则使用 prefixsuffix 插槽进行自定义插入。

html
<wd-input
  v-model="value"
  prefix-icon="dong"
  suffix-icon="list"
  @change="handleChange"/>

限制字数输入

设置 maxlength 属性,如果要显示字数限制,设置 show-word-limit 属性。

html
<wd-input v-model="value" :maxlength="20" show-word-limit @change="handleChange"/>

设置label标题

设置 label 标题,可以和 cell-group 组合使用,形成 cell 展示类型。可以通过 label-width 设置标题宽度,默认为 '33%'。

html
<wd-input type="text" label="基本用法" v-model="value" placeholder="请输入..." />

必填样式

设置了 label 的情况下,设置 required 属性,展示必填样式。

html
<wd-input v-model="value" placeholder="请输入..." label="必填" required></wd-input>

输入框大小

通过设置 size 修改输入框大小,将 size 设置为 'large' 时字号为 16px。

html
<wd-input type="text" label="基本用法" size="large" v-model="value" placeholder="请输入..." />

错误状态

设置 error 属性,输入框的值显示为红色。

html
<wd-input type="text" v-model="value" placeholder="请输入用户名" error />

垂直居中

当设置 label 标题时,默认为顶部居中,设置 center 属性可以使标题和输入框垂直居中。

html
<wd-input type="text" label="基本用法" v-model="value" center />

Attributes

参数说明类型可选值默认值最低版本
type类型stringtext / number / digit / idcardtext-
v-model绑定值string / number---
placeholder占位文本string-请输入...-
clearable显示清空按钮boolean-false-
maxlength原生属性,最大长度number-支付宝小程序无默认值,其余平台默认为-1-
showPassword显示为密码框boolean-false-
disabled原生属性,禁用boolean-false-
readonly只读boolean-false-
prefixIcon前置图标,icon组件中的图标类名string---
suffixIcon后置图标,icon组件中的图标类名string---
showWordLimit显示字数限制,需要同时设置 maxlengthboolean-false-
confirm-type设置键盘右下角按钮的文字,仅在type='text'时生效stringdone / go / next / search / senddone-
confirm-hold点击键盘右下角按钮时是否保持键盘不收起Boolean-false-
always-embed微信小程序原生属性,强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效)Boolean-false-
placeholderStyle原生属性,指定 placeholder 的样式,目前仅支持color,font-size和font-weightstring---
placeholderClass原生属性,指定 placeholder 的样式类string---
focus原生属性,获取焦点boolean-false-
cursorSpacing原生属性,指定光标与键盘的距离。取 input 距离底部的距离和cursor-spacing指定的距离的最小值作为光标与键盘的距离number-0-
cursor原生属性,指定focus时的光标位置number--1-
selectionStart原生属性,光标起始位置,自动聚集时有效,需与selection-end搭配使用number--1-
selectionEnd原生属性,光标结束位置,自动聚集时有效,需与selection-start搭配使用number--1-
adjustPosition原生属性,键盘弹起时,是否自动上推页面boolean-true-
label设置左侧标题string---
size设置输入框大小string---
error设置输入框错误状态,错误状态时为红色boolean-false-
center当有label属性时,设置标题和输入框垂直居中,默认为顶部居中boolean-false-
label-width设置左侧标题宽度string-33%-
use-label-slot使用 label 插槽boolean-false-
use-suffix-slot使用 后置图标 插槽boolean-false-
use-prefix-slot使用 前置图标 插槽boolean-false-
requiredcell 类型下必填样式boolean-false-
no-border非 cell 类型下是否隐藏下划线boolean-false-
prop表单域 model 字段名,在使用表单校验功能的情况下,该属性是必填的string---
rules表单验证规则,结合wd-form组件使用FormItemRule []-[]-

FormItemRule 数据结构

键名说明类型
required是否为必选字段boolean
message错误提示文案string
validator通过函数进行校验,可以返回一个 Promise 来进行异步校验(value, rule) => boolean | Promise
pattern通过正则表达式进行校验,正则无法匹配表示校验不通过RegExp

Events

事件名称说明参数最低版本
input监听输入框input事件 {value, cursor, keyCode}-
focus监听输入框focus事件 { value, height }, height 为键盘高度-
blur监听输入框blur事件 { value }-
change监听输入框修改事件 { value }-
clear监听输入框清空按钮事件--
confirm点击完成时, 触发 confirm 事件 { value }-
keyboardheightchange键盘高度发生变化的时候触发此事件 { height, duration }-
clickprefixicon点击前置图标时触发--
clicksuffixicon点击后置图标时触发--

Slot

提示

使用插槽需要配置是否启用对应的插槽,分别对应use-label-slotuse-suffix-slotuse-prefix-slot

name说明最低版本
label左侧标题插槽-
prefix前置插槽-
suffix后置插槽-

外部样式类

类名说明最低版本
custom-class根节点样式-
custom-input-classinput 外部自定义样式-
custom-label-classlabel 外部自定义样式-

Released under the MIT License.

Released under the MIT License.