Skip to content

Input 输入框

用户可以在文本框中输入内容。

组件类型

基本用法

通过 v-model 绑定输入值,通过 placeholder 设置占位提示。

html
<wd-input v-model="value" type="text" placeholder="请输入用户名" @input="handleInput" />
typescript
import { ref } from 'vue'

const value = ref('')

function handleInput(event) {
  console.log(event)
}

数字类型

type 设置为 number 后,仅允许输入数字。

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

组件状态

禁用

设置 disabled 后不可输入。

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

只读

设置 readonly 后不可编辑,但仍会保留展示样式。

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

错误状态

设置 error 后,输入内容会展示为错误态。

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

组件变体

清空按钮

设置 clearable 后,在可清空条件满足时显示清空按钮。

html
<wd-input v-model="value" clearable @clear="handleClear" />

聚焦时显示清空按钮

通过 clear-trigger="focus" 控制仅在输入框聚焦且有值时展示清空按钮。

注意

支付宝小程序暂不支持 clear-trigger,且某些场景下清空按钮无法点击,可参考 issue

html
<wd-input v-model="value" clear-trigger="focus" clearable />

清空后不自动聚焦

通过 focus-when-clear 控制点击清空按钮后是否重新聚焦。

html
<wd-input v-model="value" :focus-when-clear="false" clearable />

密码输入框

设置 show-password 后,可切换密码显隐状态。

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

内容形态

前后图标

通过 prefix-iconsuffix-icon 设置前后图标,图标名称可参考 Icon

html
<wd-input
  v-model="value"
  prefix-icon="sound"
  suffix-icon="send"
  clearable
  @clickprefixicon="handlePrefixClick"
  @clicksuffixicon="handleSuffixClick"
/>

后缀插槽

通过 suffix 插槽自定义后缀内容。

html
<wd-input v-model="value" clearable placeholder="请输入">
  <template #suffix>
    <wd-button size="small">获取验证码</wd-button>
  </template>
</wd-input>

组件样式

字数限制

设置 maxlength 后,可以通过 show-word-limit 展示当前字数统计。

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

紧凑布局

设置 compact 后会移除输入框的默认内边距和背景,适合配合 wd-cellwd-form-item 使用。

html
<wd-input
  v-model="price"
  compact
  placeholder="请输入价格"
  custom-style="display: inline-block; width: 70px; vertical-align: middle;"
/>
<text style="margin-left: 8px;">元</text>

特殊用法

结合表单使用

当前表单场景推荐使用 wd-formwd-form-item 承载标题、必填态与校验提示,wd-input 仅负责输入能力。

html
<wd-form :model="formData" border title-width="98px">
  <wd-form-item title="基本用法" prop="basic">
    <wd-input v-model="formData.basic" placeholder="请输入" compact />
  </wd-form-item>

  <wd-form-item title="自定义插槽" prop="slot" center>
    <wd-input v-model="formData.slot" placeholder="请输入" clearable compact>
      <template #suffix>
        <wd-button size="small">获取验证码</wd-button>
      </template>
    </wd-input>
  </wd-form-item>
</wd-form>

Attributes

参数说明类型默认值
v-model输入框绑定值string | number''
type输入框类型,可选值为 textnumberdigitidcardsafe-passwordnicknametelInputTypetext
placeholder占位文本string请输入...
placeholder-styleplaceholder 样式,目前支持 colorfont-sizefont-weightstring-
placeholder-classplaceholder 样式类string''
maxlength最大输入长度number支付宝小程序无默认值,其余平台为 -1
disabled是否禁用booleanfalse
readonly是否只读booleanfalse
clearable是否显示清空按钮booleanfalse
clear-trigger v1.3.7清空按钮显示时机,可选值为 focusalwaysInputClearTriggeralways
focus-when-clear v1.3.7点击清空按钮后是否自动聚焦booleantrue
show-password是否显示密码切换按钮booleanfalse
prefix-icon前置图标名称string-
suffix-icon后置图标名称string-
show-word-limit是否显示字数统计,需要同时设置 maxlengthbooleanfalse
error是否展示错误状态booleanfalse
align-right输入内容是否右对齐booleanfalse
compact是否开启紧凑模式;未显式设置时,在 wd-form-item 中会自动启用boolean-
focus是否获取焦点booleanfalse
cursor-spacing光标与键盘的距离number0
cursor获取焦点时的光标位置number-1
selection-start光标起始位置,需与 selection-end 搭配使用number-1
selection-end光标结束位置,需与 selection-start 搭配使用number-1
adjust-position键盘弹起时是否自动上推页面booleantrue
hold-keyboard聚焦时点击页面是否保持键盘不收起booleanfalse
confirm-type键盘右下角按钮文字,可选值为 donegonextsearchsendInputConfirmTypedone
confirm-hold点击键盘右下角按钮时是否保持键盘不收起booleanfalse
always-embed是否强制 input 处于同层状态,仅微信小程序 iOS 生效booleanfalse
ignore-composition-event v1.3.11是否忽略文本合成系统事件处理;为 false 时会触发 composition 相关事件,且合成期间会触发 inputbooleantrue
inputmode v1.5.0输入模式提示,可选值为 nonetextdecimalnumerictelsearchemailurlInputModetext
enable-native v1.14.0支付宝小程序下是否启用原生输入框,设为 false 可避免弹起键盘后内容上移booleantrue
custom-input-class输入框自定义类名string''
custom-class根节点自定义类名string''
custom-style根节点自定义样式string''

InputMode 可选值

inputmode 为 HTML 规范后期扩展能力,在符合条件的高版本 WebView 中可用于 uni-app 的 Web 与 App-Vue 平台,详见 inputmode

说明
none不弹出虚拟键盘
text标准文本输入键盘
decimal小数输入键盘
numeric纯数字输入键盘
tel电话输入键盘
search搜索输入优化键盘
email邮箱输入优化键盘
urlURL 输入优化键盘

Events

事件名称说明参数
input输入时触发{ value, cursor, keyCode }
focus聚焦时触发{ value, height }
blur失焦时触发{ value }
clear点击清空按钮时触发-
confirm点击键盘完成按钮时触发{ value }
keyboardheightchange键盘高度变化时触发{ height, duration }
clickprefixicon点击前置图标时触发-
clicksuffixicon点击后置图标时触发-
click点击组件根节点时触发event: MouseEvent

Slots

名称说明
prefix自定义前置内容
suffix自定义后置内容

外部样式类

类名说明
custom-class根节点样式类
custom-input-class输入框样式类

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-input-padding$padding-loose $padding-loose输入框内边距
--wot-input-bg$filled-oppo输入框背景色
--wot-input-inner-height$n-20输入框内高度
--wot-input-inner-font-size$typography-body-size-main输入框内字号
--wot-input-inner-color$text-main输入框文字颜色
--wot-input-inner-placeholder-color$text-placeholder输入框占位符颜色
--wot-input-disabled-color$text-disabled输入框禁用颜色
--wot-input-icon-color$icon-secondary输入框图标颜色
--wot-input-icon-font-size$n-18输入框图标字号
--wot-input-icon-margin$spacing-tight输入框图标间距
--wot-input-count-margin$spacing-extra-loose输入框字数统计间距
--wot-input-count-color$text-auxiliary输入框字数统计颜色
--wot-input-count-font-size$typography-body-size-main输入框字数统计字号
--wot-input-count-line-height$typography-body-line--height-size-main输入框字数统计行高
--wot-input-error-color$danger-main输入框错误颜色

源代码

文档
组件

Released under the MIT License.

Released under the MIT License.