InputItem 文本输入
定义/Definition
文本框允许用户输入一行文本。通常可以使用文本框来收集用户的少量文本信息并执行一些直接作用,例如搜索操作。规则 / Rule
文本框的左侧一般用于表述文本框的含义,而右侧用于展示附加的功能,如书签。
输入内容超出视窗宽度时,文本内容向前缩进。
代码演示
输入框。
支持银行卡(format="bankCard"),手机号码(format="phone"),密码(format="password"),数字(format="number",尽量唤起数字键盘)四种类型的格式
import { List, InputItem } from 'antd-mobile';import { createForm } from 'rc-form';let BasicInput = React.createClass({onClick() {console.log(this.props.form.getFieldsValue());},render() {const { getFieldProps } = this.props.form;return (<div><List><List.Header>线型</List.Header><List.Body><InputItem{...getFieldProps('input1')}placeholder="内容"clearmaxLength={10}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>基本</InputItem><InputItem{...getFieldProps('input2', {initialValue: '标签可以是node',})}placeholder="标签可以是node"><div style={{ backgroundImage: 'url(https://os.alipayobjects.com/rmsportal/zumwvwrngNMGSWe.png)', backgroundSize: 'cover', height: '22px', width: '22px' }} /></InputItem><InputItemvalue="仅供展示:editable={false}"editable={false}>不可编辑</InputItem><InputItem{...getFieldProps('input3', {initialValue: '无标签',})}clearplaceholder="内容"/><InputItem{...getFieldProps('input4')}clearplaceholder="extra"extra="元">带注释</InputItem><InputItem{...getFieldProps('input5')}name="camera"placeholder="注释可以是node"clearextra={<img src="https://os.alipayobjects.com/rmsportal/mOoPurdIfmcuqtr.png" width="22" height="22" />}>带注释</InputItem><InputItem{...getFieldProps('input6', {initialValue: '输入框',})}clearplaceholder="clear">带清除</InputItem><InputItem{...getFieldProps('input7', {initialValue: '校验出错',})}errorerrorMsg="校验出错"clearplaceholder="内容">报错样式</InputItem><InputItem{...getFieldProps('input8', {initialValue: '',})}erroronErrorClick={() => { alert(111); }}placeholder="内容"clearextra={<img src="https://os.alipayobjects.com/rmsportal/mOoPurdIfmcuqtr.png" width="22" height="22" />}>报错样式</InputItem></List.Body></List><List><List.Header>两个字标签</List.Header><List.Body><InputItem{...getFieldProps('label3')}placeholder="内容"clearmaxLength={10}labelNumber={2}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>姓名</InputItem></List.Body></List><List><List.Header>三个字标签</List.Header><List.Body><InputItem{...getFieldProps('label3')}placeholder="内容"clearmaxLength={10}labelNumber={3}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>校验码</InputItem></List.Body></List><List><List.Header>四个字标签(默认)</List.Header><List.Body><InputItem{...getFieldProps('label4')}placeholder="内容"clearmaxLength={10}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>四字标签</InputItem></List.Body></List><List><List.Header>五个字标签</List.Header><List.Body><InputItem{...getFieldProps('label5')}placeholder="内容"clearmaxLength={10}labelNumber={5}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>五个字标签</InputItem></List.Body></List><List><List.Header>六个字标签</List.Header><List.Body><InputItem{...getFieldProps('label6')}placeholder="内容"clearmaxLength={10}labelNumber={6}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>六个字标签六</InputItem></List.Body></List><List><List.Header>七个字标签</List.Header><List.Body><InputItem{...getFieldProps('label6')}placeholder="内容"clearmaxLength={10}labelNumber={7}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>七个字标签七个</InputItem></List.Body></List><List><List.Header>有边框,本期不实现</List.Header><List.Body><InputItem{...getFieldProps('zzzz', {initialValue: '',valuePropName: 'value',})}placeholder="内容"clearmaxLength={10}onBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>五个字标签</InputItem></List.Body></List></div>);},});BasicInput = createForm()(BasicInput);ReactDOM.render(<BasicInput />, mountNode);
import { List, InputItem } from 'antd-mobile';import { createForm } from 'rc-form';let FormatExample = React.createClass({render() {const { getFieldProps } = this.props.form;return (<List><List.Header>格式</List.Header><List.Body><InputItem{...getFieldProps('bankCard', {initialValue: '8888 8888 8888 8888',})}format="bankCard"placeholder="8888 8888 8888 8888"clearonBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>银行卡</InputItem><InputItem{...getFieldProps('phone', {initialValue: '186 1234 1234',})}format="phone"placeholder="186 1234 1234"clearonBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>手机号码</InputItem><InputItem{...getFieldProps('password', {initialValue: '123131',})}format="password"placeholder="****"clearonBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>密码</InputItem><InputItem{...getFieldProps('number', {initialValue: '123131',})}format="number"placeholder="123131"clearonBlur={function (e) { console.log('onBlur'); console.log(e); }}onFocus={function (e) { console.log('onFocus'); console.log(e); }}>数字键盘</InputItem></List.Body></List>);},});FormatExample = createForm()(FormatExample);ReactDOM.render(<FormatExample />, mountNode);

API
| 成员 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| prefixListCls | String | am-list | ||
| type | 文本框样式类型 | String | 线型hasLine,有边框hasBorder,本期只支持hasLine | hasLine |
| format | 格式 | String | 银行卡输入bankCard,手机号输入phone(此时最大长度固定为11,maxLength设置无效),密码输入password,文本输入text, 数字输入number(尽量唤起数字键盘) | text |
| editable | 输入框是否可输入 | bool | true | |
| name | input的name | String | 无 | |
| value | input的初始值 | String | 无 | |
| placeholder | placeholder | String | 无 | |
| clear | 是否带清除功能 | bool | false | |
| maxLength | 最大长度 | number | 无 | |
| onChange | input change事件触发的回调函数,参数是value | Function | 无 | |
| onBlur | input blur事件触发的回调函数,参数是value | Function | 无 | |
| onFocus | input focus事件触发的回调函数,参数是value | Function | 无 | |
| extra | 右边注释 | string or node | '' | |
| onExtraClick | extra点击事件触发的回调函数,参数是event对象 | Function | 无 | |
| error | 报错样式 | bool | false | |
| onErrorClick | 点击报错icon触发的回调, | Function | 无 | |
| size(本期不实现) | 尺寸 | string | large,small | large |
| labelNumber | 标签字数(一个表单页面中,通常有多个InputItem,需要统一标签字数以便统一宽度,保持页面整洁、美观) | number | 2, 3, 4, 5, 6, 7 | 4 |
| labelPosition(本期不实现) | 标题方向 | string | left,top | left |
| textAlign(本期不实现) | 文本对齐 | string | left,center | left |