InputItem 文本输入

定义/Definition

文本框允许用户输入一行文本。通常可以使用文本框来收集用户的少量文本信息并执行一些直接作用,例如搜索操作。

规则 / Rule

  • 文本框的左侧一般用于表述文本框的含义,而右侧用于展示附加的功能,如书签。

  • 输入内容超出视窗宽度时,文本内容向前缩进。

代码演示

基本

输入框。

  1. import { List, InputItem } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. let BasicInput = React.createClass({
  4. onClick() {
  5. console.log(this.props.form.getFieldsValue());
  6. },
  7. render() {
  8. const { getFieldProps } = this.props.form;
  9. return (<div>
  10. <List>
  11. <List.Header>线型</List.Header>
  12. <List.Body>
  13. <InputItem
  14. {...getFieldProps('input1')}
  15. placeholder="内容"
  16. clear
  17. maxLength={10}
  18. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  19. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  20. >基本</InputItem>
  21. <InputItem
  22. {...getFieldProps('input2', {
  23. initialValue: '标签可以是node',
  24. })}
  25. placeholder="标签可以是node"
  26. ><div style={{ backgroundImage: 'url(https://os.alipayobjects.com/rmsportal/zumwvwrngNMGSWe.png)', backgroundSize: 'cover', height: '22px', width: '22px' }} /></InputItem>
  27. <InputItem
  28. value="仅供展示:editable={false}"
  29. editable={false}
  30. >不可编辑</InputItem>
  31. <InputItem
  32. {...getFieldProps('input3', {
  33. initialValue: '无标签',
  34. })}
  35. clear
  36. placeholder="内容"
  37. />
  38. <InputItem
  39. {...getFieldProps('input4')}
  40. clear
  41. placeholder="extra"
  42. extra="元"
  43. >带注释</InputItem>
  44. <InputItem
  45. {...getFieldProps('input5')}
  46. name="camera"
  47. placeholder="注释可以是node"
  48. clear
  49. extra={<img src="https://os.alipayobjects.com/rmsportal/mOoPurdIfmcuqtr.png" width="22" height="22" />}
  50. >带注释</InputItem>
  51. <InputItem
  52. {...getFieldProps('input6', {
  53. initialValue: '输入框',
  54. })}
  55. clear
  56. placeholder="clear"
  57. >带清除</InputItem>
  58. <InputItem
  59. {...getFieldProps('input7', {
  60. initialValue: '校验出错',
  61. })}
  62. error
  63. errorMsg="校验出错"
  64. clear
  65. placeholder="内容"
  66. >报错样式</InputItem>
  67. <InputItem
  68. {...getFieldProps('input8', {
  69. initialValue: '',
  70. })}
  71. error
  72. onErrorClick={() => { alert(111); }}
  73. placeholder="内容"
  74. clear
  75. extra={<img src="https://os.alipayobjects.com/rmsportal/mOoPurdIfmcuqtr.png" width="22" height="22" />}
  76. >报错样式</InputItem>
  77. </List.Body>
  78. </List>
  79. <List>
  80. <List.Header>两个字标签</List.Header>
  81. <List.Body>
  82. <InputItem
  83. {...getFieldProps('label3')}
  84. placeholder="内容"
  85. clear
  86. maxLength={10}
  87. labelNumber={2}
  88. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  89. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  90. >姓名</InputItem>
  91. </List.Body>
  92. </List>
  93. <List>
  94. <List.Header>三个字标签</List.Header>
  95. <List.Body>
  96. <InputItem
  97. {...getFieldProps('label3')}
  98. placeholder="内容"
  99. clear
  100. maxLength={10}
  101. labelNumber={3}
  102. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  103. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  104. >校验码</InputItem>
  105. </List.Body>
  106. </List>
  107. <List>
  108. <List.Header>四个字标签(默认)</List.Header>
  109. <List.Body>
  110. <InputItem
  111. {...getFieldProps('label4')}
  112. placeholder="内容"
  113. clear
  114. maxLength={10}
  115. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  116. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  117. >四字标签</InputItem>
  118. </List.Body>
  119. </List>
  120. <List>
  121. <List.Header>五个字标签</List.Header>
  122. <List.Body>
  123. <InputItem
  124. {...getFieldProps('label5')}
  125. placeholder="内容"
  126. clear
  127. maxLength={10}
  128. labelNumber={5}
  129. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  130. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  131. >五个字标签</InputItem>
  132. </List.Body>
  133. </List>
  134. <List>
  135. <List.Header>六个字标签</List.Header>
  136. <List.Body>
  137. <InputItem
  138. {...getFieldProps('label6')}
  139. placeholder="内容"
  140. clear
  141. maxLength={10}
  142. labelNumber={6}
  143. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  144. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  145. >六个字标签六</InputItem>
  146. </List.Body>
  147. </List>
  148. <List>
  149. <List.Header>七个字标签</List.Header>
  150. <List.Body>
  151. <InputItem
  152. {...getFieldProps('label6')}
  153. placeholder="内容"
  154. clear
  155. maxLength={10}
  156. labelNumber={7}
  157. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  158. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  159. >七个字标签七个</InputItem>
  160. </List.Body>
  161. </List>
  162. <List>
  163. <List.Header>有边框,本期不实现</List.Header>
  164. <List.Body>
  165. <InputItem
  166. {...getFieldProps('zzzz', {
  167. initialValue: '',
  168. valuePropName: 'value',
  169. })}
  170. placeholder="内容"
  171. clear
  172. maxLength={10}
  173. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  174. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  175. >五个字标签</InputItem>
  176. </List.Body>
  177. </List>
  178. </div>);
  179. },
  180. });
  181. BasicInput = createForm()(BasicInput);
  182. ReactDOM.render(<BasicInput />, mountNode);

格式

支持银行卡(format="bankCard"),手机号码(format="phone"),密码(format="password"),数字(format="number",尽量唤起数字键盘)四种类型的格式

  1. import { List, InputItem } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. let FormatExample = React.createClass({
  4. render() {
  5. const { getFieldProps } = this.props.form;
  6. return (<List>
  7. <List.Header>格式</List.Header>
  8. <List.Body>
  9. <InputItem
  10. {...getFieldProps('bankCard', {
  11. initialValue: '8888 8888 8888 8888',
  12. })}
  13. format="bankCard"
  14. placeholder="8888 8888 8888 8888"
  15. clear
  16. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  17. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  18. >银行卡</InputItem>
  19. <InputItem
  20. {...getFieldProps('phone', {
  21. initialValue: '186 1234 1234',
  22. })}
  23. format="phone"
  24. placeholder="186 1234 1234"
  25. clear
  26. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  27. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  28. >手机号码</InputItem>
  29. <InputItem
  30. {...getFieldProps('password', {
  31. initialValue: '123131',
  32. })}
  33. format="password"
  34. placeholder="****"
  35. clear
  36. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  37. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  38. >密码</InputItem>
  39. <InputItem
  40. {...getFieldProps('number', {
  41. initialValue: '123131',
  42. })}
  43. format="number"
  44. placeholder="123131"
  45. clear
  46. onBlur={function (e) { console.log('onBlur'); console.log(e); }}
  47. onFocus={function (e) { console.log('onFocus'); console.log(e); }}
  48. >数字键盘</InputItem>
  49. </List.Body>
  50. </List>);
  51. },
  52. });
  53. FormatExample = createForm()(FormatExample);
  54. ReactDOM.render(<FormatExample />, mountNode);

InputItem文本输入 - 图1

API

成员说明类型可选值默认值
prefixListClsStringam-list
type文本框样式类型String线型hasLine,有边框hasBorder,本期只支持hasLinehasLine
format格式String银行卡输入bankCard,手机号输入phone(此时最大长度固定为11,maxLength设置无效),密码输入password,文本输入text, 数字输入number(尽量唤起数字键盘)text
editable输入框是否可输入booltrue
nameinput的nameString
valueinput的初始值String
placeholderplaceholderString
clear是否带清除功能boolfalse
maxLength最大长度number
onChangeinput change事件触发的回调函数,参数是valueFunction
onBlurinput blur事件触发的回调函数,参数是valueFunction
onFocusinput focus事件触发的回调函数,参数是valueFunction
extra右边注释string or node''
onExtraClickextra点击事件触发的回调函数,参数是event对象Function
error报错样式boolfalse
onErrorClick点击报错icon触发的回调,Function
size(本期不实现)尺寸stringlarge,smalllarge
labelNumber标签字数(一个表单页面中,通常有多个InputItem,需要统一标签字数以便统一宽度,保持页面整洁、美观)number2, 3, 4, 5, 6, 74
labelPosition(本期不实现)标题方向stringleft,topleft
textAlign(本期不实现)文本对齐stringleft,centerleft