antd-mobile
默认的是输入框错误处理方式为在输入框右侧显示错误图标,点击图标触发绑定的onErrorClick
事件,这样显得不够直观,现想利用现有组件参数对其进行封装,达到在右侧不显示错误图标,将错误信息显示到输入框下侧。
Antd Mobile Design --> InputItem
props参数javascript
onErrorClick
:原来绑定的是一个点击事件,为了能够不添加多余的属性,且充分利用该属性(由于右侧错误图标不须要后,该事件便无效了
),如今变为传入一个错误数组
或者字符串
,用于显示错误提示信息tipStyle
:自定义错误提示语的样式,能够不传入,会使用封装内默认的提示样式,传入则增长或覆盖已有样式错误提示文字的显示位置java
InputItem
的组件样式注意组件内使用InputItem
时将error={false}
放在后面,这样就能避免显示出右侧的错误图标,原理不用多说你们应该也懂的^_^ ^_^
import React, { Component } from 'react'; import { InputItem } from 'antd-mobile'; const cls = 'trust-mobile-input'; class MobileInputItem extends Component { render() { const {type = 'text', labelNumber = 5, error = false, children = '', onErrorClick = '', tipStyle = {}, ...other} = this.props; const errorTipStyle = { color: '#f5222d', padding: '5px 0px', textAlign: 'left', position: 'relative', fontSize: 12, ...tipStyle } return ( <div className={cls}> <InputItem type={type} {...other} error={false}>{children}</InputItem> { error && <div className="am-list-item" style={{height: 'auto', minHeight: 0}}> { children && <div className={`am-input-label am-input-label-${labelNumber}`}></div> } <div className="am-input-control"> <div style={{...errorTipStyle}}> {onErrorClick} </div> </div> </div> } </div> ) } } export default MobileInputItem;
错误的显示隐藏过渡效果
,错误时输入框文字颜色
等等