Skip to content

Input 输入框

用户可以在文本框内输入或编辑文字。

基础用法

自定义类型

禁用输入框

显示图标

错误提示

插入按钮

格式化输入内容

高度自适应

显示字数统计

输入框内容对齐

输入框文本位置

API

Props

参数说明类型默认值
v-model当前输入的值number / string-
label输入框左侧文本string-
name名称,作为提交表单时的标识符string-
id输入框 id,同时会设置 label 的 for 属性stringr-input-n-input
type输入框类型, 支持原生 input 标签的所有 type 属性,额外支持了 digit 类型InputTypetext
size大小,可选值为 largestring-
maxlength输入的最大字符数number / string-
min输入框类型为 numberdigit 类型时设置可允许的最小值number-
max输入框类型为 numberdigit 类型时设置可允许的最大值number-
placeholder输入框占位提示文字string-
input-border输入框是否显示边框booleantrue
border是否显示内边框booleantrue
disabled是否禁用输入框booleanfalse
readonly是否为只读状态,只读状态下无法输入内容booleanfalse
colon是否在 label 后面添加冒号booleanfalse
required是否显示表单必填星号boolean | 'auto'null
center是否使内容垂直居中booleanfalse
clearable是否启用清除图标,点击清除图标后会清空输入框booleanfalse
clear-icon清除图标名称或图片链接,等同于 Icon 组件的 name 属性stringclear
clear-trigger显示清除图标的时机,always 表示输入框不为空时展示,
focus 表示输入框聚焦且不为空时展示
InputClearTriggerfocus
clickable是否开启点击反馈booleanfalse
is-link是否展示右侧箭头并开启点击反馈booleanfalse
autofocus是否自动聚焦,iOS 系统不支持该属性booleanfalse
show-word-limit是否显示字数统计,需要设置 maxlength 属性booleanfalse
error是否将输入内容标红booleanfalse
error-message底部错误提示文案,为空时不展示string-
error-message-align错误提示文案对齐方式,可选值为 center rightInputTextAlignleft
formatter输入内容格式化函数(val: string) => string-
format-trigger格式化函数触发的时机,可选值为 onBlurInputFormatTriggeronChange
arrow-direction箭头方向,可选值为 left up downstringright
label-class左侧文本额外类名string / Array / object-
label-width左侧文本宽度,默认单位为 pxnumber / string6.2em
label-align左侧文本对齐方式,可选值为 center right topInputTextAlignleft
input-align输入框对齐方式,可选值为 center rightInputTextAlignleft
autosize是否自适应内容高度,只对 textarea 有效,
可传入对象,如 { maxHeight: 100, minHeight: 50 },
单位为px
boolean / InputAutosizeConfigfalse
left-icon左侧图标名称或图片链接,等同于 Icon 组件的 name 属性string-
right-icon右侧图标名称或图片链接,等同于 Icon 组件的 name 属性string-
icon-prefix图标类名前缀,等同于 Icon 组件的 class-prefix 属性stringr-icon
rules表单校验规则,详见 Form 组件InputRule[]-
autocompleteHTML 原生属性,用于控制自动完成功能,详见 MDN - autocompletestringoff
autocapitalize v1.10.1HTML 原生属性,用于控制文本输入时是否自动大写,此 API 仅在部分浏览器支持,详见 MDN - autocapitalizestring-
enterkeyhintHTML 原生属性,用于控制回车键样式,此 API 仅在部分浏览器支持,详见 MDN - enterkeyhint
string-
spellcheck v1.10.1HTML 原生属性,用于检查元素的拼写错误,此 API 仅在部分浏览器支持,详见 MDN - spellcheck
boolean-
autocorrect v1.10.1HTML 原生属性,仅 Safari 适用,用于自动更正输入的文本,详见 MDN - autocorrect
string-
inputmode v1.13.11HTML 原生属性,用于指定输入框的输入模式,详见 MDN - inputmodestring根据 type 属性自动设置

Events

事件名说明回调参数
update:model-value输入框内容变化时触发value: string (当前输入的值)
focus输入框获得焦点时触发event: Event
blur输入框失去焦点时触发event: Event
clear点击清除按钮时触发event: MouseEvent
click点击组件时触发event: MouseEvent
click-input点击输入区域时触发event: MouseEvent
click-left-icon点击左侧图标时触发event: MouseEvent
click-right-icon点击右侧图标时触发event: MouseEvent
start-validate开始表单校验时触发-
end-validate结束表单校验时触发{ status: string, message: string }

方法

通过 ref 可以获取到 Input 实例并调用实例方法,详见组件实例方法

方法名说明参数返回值
focus获取输入框焦点--
blur取消输入框焦点--

类型定义

组件导出以下类型定义:

ts
import type {
  InputType,
  InputRule,
  InputProps,
  InputInstance,
  InputTextAlign,
  InputRuleMessage,
  InputClearTrigger,
  InputFormatTrigger,
  InputRuleValidator,
  InputRuleFormatter,
  InputValidateError,
  InputAutosizeConfig,
  InputValidateTrigger,
  InputValidationStatus
} from 'ryxon'

InputInstance 是组件实例的类型,用法如下:

ts
import { ref } from 'vue'
import type { InputInstance } from 'ryxon'

const inputRef = ref<InputInstance>()

inputRef.value?.focus()

Slots

名称说明参数
label自定义输入框左侧文本-
input自定义输入框,使用此插槽后,与输入框相关的属性和事件将失效-
left-icon自定义输入框头部图标-
right-icon自定义输入框尾部图标-
button自定义输入框尾部按钮-
error-message自定义底部错误提示文案{ message: string }
extra自定义输入框最右侧的额外内容-

主题定制

样式变量

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

名称默认值描述
--r-input-label-width6.2em-
--r-input-label-colorvar(--r-text-color)-
--r-input-label-margin-rightvar(--r-padding-sm)-
--r-input-text-colorvar(--r-text-color)-
--r-input-error-text-colorvar(--r-danger-color)-
--r-input-placeholder-text-colorvar(--r-text-color-3)-
--r-input-icon-size16px-
--r-input-clear-icon-size16px-
--r-input-clear-icon-colorvar(--r-gray-5)-
--r-input-right-icon-colorvar(--r-info-color)-
--r-input-error-message-colorvar(--r-danger-color)-
--r-input-error-message-font-size12px-
--r-input-text-area-min-height60px-
--r-input-word-limit-colorvar(--r-gray-7)-
--r-input-word-limit-font-sizevar(--r-font-size-sm)-
--r-input-word-limit-line-height16px-
--r-input-disabled-text-colorvar(--r-text-color-3)-
--r-input-required-mark-colorvar(--r-danger-color)-
--r-input-hover-border-colorvar(--r-text-color-disabled)-
--r-input-focus-border-colorvar(--r-primary-color)-

常见问题

设置 type 为 number 后,为什么 input 标签的类型仍为 text?

HTML 原生的 type="number" 属性在 iOS 和 Android 系统上都存在一定问题,比如 maxlength 属性不生效、无法获取到完整的输入内容等。因此设置 type 为 number 时,Input 不会使用原生的 type="number" 属性,而是用现代浏览器支持的 inputmode 属性来控制输入键盘的类型。

为什么 v-model 绑定的值被更新为 string 类型?

Input 组件内部会将传入的 v-model 格式化为 string 类型,便于组件内部进行处理。

如果你希望在 v-model 上绑定 number 类型,可以使用 Vue 提供的 .number 修饰符

html
<r-input v-model.number="value" type="tel" />

Released under the MIT License.