跳到主要内容
版本:3.x

Input

输入框。该组件是原生组件,使用时请注意相关限制

支持情况:

参考文档

类型

ComponentType<InputProps>

示例代码

class App extends Component {
render () {
return (
<View className='example-body'>
<Text>可以自动聚焦的 input</Text>
<Input type='text' placeholder='将会获取焦点' focus/>
<Text>控制最大输入长度的 input</Text>
<Input type='text' placeholder='最大输入长度为 10' maxLength='10'/>
<Text>数字输入的 input</Text>
<Input type='number' placeholder='这是一个数字输入框'/>
<Text>密码输入的 input</Text>
<Input type='password' password placeholder='这是一个密码输入框'/>
<Text>带小数点的 input</Text>
<Input type='digit' placeholder='带小数点的数字键盘'/>
<Text>身份证输入的 input</Text>
<Input type='idcard' placeholder='身份证输入键盘'/>
<Text>控制占位符颜色的 input</Text>
<Input type='text' placeholder='占位符字体是红色的' placeholderStyle='color:red'/>
</View>
)
}
}

InputProps

参数类型默认值必填说明
valuestring输入框的初始内容
typekeyof Type"text"input 的类型
passwordbooleanfalse是否是密码类型
placeholderstring输入框为空时占位符
placeholderStylestring指定 placeholder 的样式
placeholderClassstring"input-placeholder"指定 placeholder 的样式类
placeholderTextColorstring指定 placeholder 的文本颜色
disabledbooleanfalse是否禁用
maxlengthnumber140最大输入长度,设置为 -1 的时候不限制最大长度
cursorSpacingnumber0指定光标与键盘的距离,单位 px 。取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离
autoFocusbooleanfalse(即将废弃,请直接使用 focus )自动聚焦,拉起键盘
不推荐使用
focusbooleanfalse获取焦点
confirmTypekeyof ConfirmTypedone设置键盘右下角按钮的文字,仅在type='text'时生效
confirmHoldbooleanfalse点击键盘右下角按钮时是否保持键盘不收起
cursornumber指定focus时的光标位置
selectionStartnumber-1光标起始位置,自动聚集时有效,需与selection-end搭配使用
selectionEndnumber-1光标结束位置,自动聚集时有效,需与selection-start搭配使用
adjustPositionbooleantrue键盘弹起时,是否自动上推页面
holdKeyboardbooleanfalsefocus 时,点击页面的时候不收起键盘
alwaysEmbedbooleanfalse强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效)
safePasswordCertPathstring安全键盘加密公钥的路径,只支持包内路径
safePasswordLengthnumber安全键盘输入密码长度
safePasswordTimeStampnumber安全键盘加密时间戳
safePasswordNoncestring安全键盘加密盐值
safePasswordSaltstring安全键盘计算hash盐值,若指定custom-hash 则无效
safePasswordCustomHashstring安全键盘计算hash的算法表达式,如 md5(sha1('foo' + sha256(sm3(password + 'bar'))))
randomNumberbooleanfalse当 type 为 number, digit, idcard 数字键盘是否随机排列
controlledbooleanfalse是否为受控组件。为 true 时,value 内容会完全受 setData 控制。

建议当 type 值为 text 时不要将 controlled 设置为 true,详见 Bugs & Tips
nativePropsRecord<string, unknown>用于透传 WebComponents 上的属性到内部 H5 标签上
namestring组件名字,用于表单提交获取数据。
alwaysSystembooleanfalse是否强制使用系统键盘和 Web-view 创建的 input 元素。为 true 时,confirm-type、confirm-hold 可能失效。
ariaLabelstring无障碍访问,(属性)元素的额外描述
clueTypenumber0用于分发目的。取值:0 和 1,其中 0 表示默认,1 表示手机号,需要和留资分发配置一起使用,详情见留资分发配置。
onInputCommonEventFunction<inputEventDetail>当键盘输入时,触发input事件,event.detail = {value, cursor, keyCode},处理函数可以直接 return 一个字符串,将替换输入框的内容。
onFocusCommonEventFunction<inputForceEventDetail>输入框聚焦时触发,event.detail = { value, height },height 为键盘高度
onBlurCommonEventFunction<inputValueEventDetail>输入框失去焦点时触发
onConfirmCommonEventFunction<inputValueEventDetail>点击完成按钮时触发
onKeyboardHeightChangeCommonEventFunction<onKeyboardHeightChangeEventDetail>键盘高度发生变化的时候触发此事件
onNickNameReviewCommonEventFunction用户昵称审核完毕后触发,仅在 type 为 "nickname" 时有效,event.detail = { pass, timeout }

API 支持度

API微信小程序百度小程序支付宝小程序抖音小程序QQ 小程序京东小程序H5React NativeHarmony
InputProps.value✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.type✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.password✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.placeholder✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.placeholderStyle✔️✔️✔️✔️✔️✔️✔️
InputProps.placeholderClass✔️✔️✔️✔️✔️✔️
InputProps.placeholderTextColor✔️
InputProps.disabled✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.maxlength✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.cursorSpacing✔️✔️✔️✔️✔️
InputProps.autoFocus✔️✔️✔️✔️
InputProps.focus✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.confirmType✔️✔️✔️(confirm-type 与 enableNative 属性冲突,若希望 confirm-type 生效,enableNative 不能设定为 false,而且不能设定 always-system)✔️✔️✔️✔️
InputProps.confirmHold✔️✔️✔️✔️✔️✔️
InputProps.cursor✔️✔️✔️✔️✔️✔️✔️
InputProps.selectionStart✔️✔️✔️✔️✔️✔️✔️
InputProps.selectionEnd✔️✔️✔️✔️✔️✔️✔️
InputProps.adjustPosition✔️✔️✔️✔️✔️
InputProps.holdKeyboard✔️✔️
InputProps.alwaysEmbed✔️
InputProps.safePasswordCertPath✔️
InputProps.safePasswordLength✔️
InputProps.safePasswordTimeStamp✔️
InputProps.safePasswordNonce✔️
InputProps.safePasswordSalt✔️
InputProps.safePasswordCustomHash✔️
InputProps.randomNumber✔️
InputProps.controlled✔️
InputProps.nativeProps✔️
InputProps.name✔️
InputProps.alwaysSystem✔️
InputProps.ariaLabel✔️
InputProps.clueType✔️
InputProps.onInput✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.onFocus✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.onBlur✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.onConfirm✔️✔️✔️✔️✔️✔️✔️✔️
InputProps.onKeyboardHeightChange✔️✔️✔️
InputProps.onNickNameReview✔️

Type

Input 类型

参数说明
text文本输入键盘
number数字输入键盘
idcard身份证输入键盘
digit带小数点的数字键盘
safe-password密码安全输入键盘指引
nickname昵称输入键盘
numberpad数字输入键盘
digitpad带小数点的数字键盘
idcardpad身份证输入键盘

API 支持度

API微信小程序支付宝小程序H5React NativeHarmony
Type.text✔️✔️✔️✔️
Type.number✔️✔️✔️✔️
Type.idcard✔️✔️✔️
Type.digit✔️✔️✔️✔️
Type.safe-password✔️✔️
Type.nickname✔️✔️
Type.numberpad✔️
Type.digitpad✔️
Type.idcardpad✔️

ConfirmType

Confirm 类型

参数说明
send右下角按钮为“发送”
search右下角按钮为“搜索”
next右下角按钮为“下一个”
go右下角按钮为“前往”
done右下角按钮为“完成”

inputEventDetail

注意:React-Native 端 inputEventDetail 仅实现参数 value,若需实时获取光标位置则可通过 onSelectionChange 实现。

参数类型说明
valuestring输入值
cursornumber光标位置
keyCodenumber键值

inputForceEventDetail

参数类型说明
valuestring输入值
heightnumber键盘高度

inputValueEventDetail

参数类型说明
valuestring输入值

onKeyboardHeightChangeEventDetail

参数类型说明
heightnumber键盘高度
durationnumber持续时间