InputItem 文本输入

用于接受单行文本。

规则

  • 支持通过键盘或者剪切板输入文本。

  • 通过光标可以在水平方向进行移动。

  • 对特定格式的文本进行处理,eg:隐藏密码。

代码演示

金额键盘

受控组件建议使用(rc-form )

  1. import { List, InputItem, Button } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. class H5NumberInputExample extends React.Component {
  4. state = {
  5. moneyfocused: false,
  6. type: 'money',
  7. }
  8. render() {
  9. const { getFieldProps } = this.props.form;
  10. const { type } = this.state;
  11. return (
  12. <div>
  13. <List renderHeader={() => 'Format'}>
  14. <InputItem
  15. {...getFieldProps('money', {
  16. initialValue: '222',
  17. })}
  18. type={type}
  19. placeholder="money keyboard"
  20. clear
  21. maxLength={10}
  22. locale={{ confirmLabel: '计算' }}
  23. onBlur={value => console.log(value)}
  24. />
  25. <InputItem placeholder="22">普通键盘</InputItem>
  26. <InputItem
  27. {...getFieldProps('money2', {
  28. normalize: (v, prev) => {
  29. if (v && !/^(([1-9]\d*)|0)(\.\d{0,2}?)?$/.test(v)) {
  30. if (v === '.') {
  31. return '0.';
  32. }
  33. return prev;
  34. }
  35. return v;
  36. },
  37. })}
  38. type={type}
  39. placeholder="money format"
  40. onFocus={() => {
  41. this.setState({
  42. moneyfocused: false,
  43. });
  44. }}
  45. focused={this.state.moneyfocused}
  46. >数字键盘</InputItem>
  47. <List.Item>
  48. <div
  49. style={{ width: '100%', color: '#108ee9', textAlign: 'center' }}
  50. onClick={() => {
  51. this.setState({
  52. moneyfocused: true,
  53. });
  54. }}
  55. >
  56. click to focus
  57. </div>
  58. </List.Item>
  59. <InputItem
  60. {...getFieldProps('moneynatural', {
  61. normalize: (v) => {
  62. if (v && (v.charAt(0) === '0' || v.indexOf('.') >= 0)) {
  63. return v.replace(/^0*(\d*).*$/, '$1');
  64. }
  65. return v;
  66. },
  67. })}
  68. type={type}
  69. placeholder="money format natural"
  70. >正整数</InputItem>
  71. </List>
  72. <Button
  73. onClick={() => {
  74. this.setState({
  75. type: 'text',
  76. });
  77. }}
  78. >
  79. 重置为电话类型
  80. </Button>
  81. </div>
  82. );
  83. }
  84. }
  85. const H5NumberInputExampleWrapper = createForm()(H5NumberInputExample);
  86. ReactDOM.render(<H5NumberInputExampleWrapper />, mountNode);
  1. .demoTitle:before,
  2. .demoTitle:after {
  3. border-bottom: none;
  4. }

基本

受控组件建议使用(rc-form )

  1. import { List, InputItem, WhiteSpace } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. class BasicInputExample extends React.Component {
  4. state = {
  5. focused: false,
  6. focused1: false,
  7. }
  8. render() {
  9. const { getFieldProps } = this.props.form;
  10. return (
  11. <div>
  12. <List renderHeader={() => 'Customize to focus'}>
  13. <InputItem
  14. {...getFieldProps('autofocus')}
  15. clear
  16. placeholder="auto focus in Alipay client"
  17. autoFocus
  18. >标题</InputItem>
  19. <InputItem
  20. {...getFieldProps('focus')}
  21. clear
  22. placeholder="click the button below to focus"
  23. focused={this.state.focused}
  24. onFocus={() => {
  25. this.setState({
  26. focused: false,
  27. });
  28. }}
  29. >标题</InputItem>
  30. <List.Item>
  31. <div
  32. style={{ width: '100%', color: '#108ee9', textAlign: 'center' }}
  33. onClick={() => {
  34. this.setState({
  35. focused: true,
  36. });
  37. }}
  38. >
  39. click to focus
  40. </div>
  41. </List.Item>
  42. </List>
  43. <List renderHeader={() => 'Whether is controlled'}>
  44. <InputItem
  45. {...getFieldProps('control')}
  46. placeholder="Hello World"
  47. >受控组件</InputItem>
  48. <InputItem
  49. placeholder="please input content"
  50. data-seed="logId"
  51. >非受控组件</InputItem>
  52. </List>
  53. <WhiteSpace />
  54. <List renderHeader={() => 'click label to focus input'}>
  55. <InputItem
  56. placeholder="click label to focus input"
  57. focused={this.state.focused1}
  58. onFocus={() => {
  59. this.setState({
  60. focused1: false,
  61. });
  62. }}
  63. ><div onClick={() => this.setState({ focused1: true })}>标题</div></InputItem>
  64. </List>
  65. <List renderHeader={() => 'Show clear icon'}>
  66. <InputItem
  67. {...getFieldProps('inputclear')}
  68. clear
  69. placeholder="displayed clear icon while typing"
  70. >标题</InputItem>
  71. </List>
  72. <WhiteSpace />
  73. <List renderHeader={() => 'Number of words for title'}>
  74. <InputItem
  75. {...getFieldProps('label8')}
  76. placeholder="limited title length"
  77. labelNumber={5}
  78. >标题过长超过默认的5个字</InputItem>
  79. </List>
  80. <WhiteSpace />
  81. <List renderHeader={() => 'Custom title(text / image / empty)'}>
  82. <InputItem
  83. {...getFieldProps('input3')}
  84. placeholder="no label"
  85. />
  86. <InputItem
  87. {...getFieldProps('inputtitle2')}
  88. placeholder="title can be icon,image or text"
  89. >
  90. <div style={{ backgroundImage: 'url(https://zos.alipayobjects.com/rmsportal/DfkJHaJGgMghpXdqNaKF.png)', backgroundSize: 'cover', height: '0.44rem', width: '0.44rem' }} />
  91. </InputItem>
  92. </List>
  93. <WhiteSpace />
  94. <List renderHeader={() => 'Customize the extra content in the right'}>
  95. <InputItem
  96. {...getFieldProps('preice')}
  97. placeholder="0.00"
  98. extra="¥"
  99. >价格</InputItem>
  100. </List>
  101. <WhiteSpace />
  102. <List renderHeader={() => 'Format'}>
  103. <InputItem
  104. {...getFieldProps('bankCard', {
  105. initialValue: '8888 8888 8888 8888',
  106. })}
  107. type="bankCard"
  108. >银行卡</InputItem>
  109. <InputItem
  110. {...getFieldProps('phone')}
  111. type="phone"
  112. placeholder="186 1234 1234"
  113. >手机号码</InputItem>
  114. <InputItem
  115. {...getFieldProps('password')}
  116. type="password"
  117. placeholder="****"
  118. >密码</InputItem>
  119. <InputItem
  120. {...getFieldProps('number')}
  121. type="number"
  122. placeholder="click to show number keyboard"
  123. >数字键盘</InputItem>
  124. </List>
  125. <WhiteSpace />
  126. <List renderHeader={() => 'Not editable / Disabled'}>
  127. <InputItem
  128. value="Not editable"
  129. editable={false}
  130. >姓名</InputItem>
  131. <InputItem
  132. value="style of disabled `InputItem`"
  133. disabled
  134. >姓名</InputItem>
  135. </List>
  136. </div>
  137. );
  138. }
  139. }
  140. const BasicInputExampleWrapper = createForm()(BasicInputExample);
  141. ReactDOM.render(<BasicInputExampleWrapper />, mountNode);
  1. .demoTitle:before,
  2. .demoTitle:after {
  3. border-bottom: none;
  4. }

错误验证

  1. import { List, InputItem, Toast } from 'antd-mobile';
  2. class ErrorInputExample extends React.Component {
  3. state = {
  4. hasError: false,
  5. value: '',
  6. }
  7. onErrorClick = () => {
  8. if (this.state.hasError) {
  9. Toast.info('Please enter 11 digits');
  10. }
  11. }
  12. onChange = (value) => {
  13. if (value.replace(/\s/g, '').length < 11) {
  14. this.setState({
  15. hasError: true,
  16. });
  17. } else {
  18. this.setState({
  19. hasError: false,
  20. });
  21. }
  22. this.setState({
  23. value,
  24. });
  25. }
  26. render() {
  27. return (
  28. <div>
  29. <List renderHeader={() => 'Confirm when typing'}>
  30. <InputItem
  31. type="phone"
  32. placeholder="input your phone"
  33. error={this.state.hasError}
  34. onErrorClick={this.onErrorClick}
  35. onChange={this.onChange}
  36. value={this.state.value}
  37. >手机号码</InputItem>
  38. </List>
  39. </div>
  40. );
  41. }
  42. }
  43. ReactDOM.render(<ErrorInputExample />, mountNode);
  1. .demoTitle:before,
  2. .demoTitle:after {
  3. border-bottom: none;
  4. }

InputItem文本输入 - 图1

API

适用平台:WEB、React-Native

InputItem 必须用 List 组件包裹才能正常使用

属性说明类型默认值
type银行卡bankCard,手机号phone(此时最大长度固定为11,maxLength设置无效),密码password, 数字number(尽量唤起数字键盘,纯web环境目前无法唤起带小数点的数字键盘,如有此需求,目前需使用默认键盘并通过onChange自行处理),money带小数点的数字键盘(纯h5实现, Web OnlyStringtext
valuevalue 值(受控与否参考https://facebook.github.io/react/docs/forms.html)String
defaultValue设置初始默认值String-
placeholderplaceholderString''
editable是否可编辑booltrue
disabled是否禁用boolfalse
clear是否带清除功能(仅editabletrue,disabledfalse,value设置才生效)boolfalse
maxLength最大长度number
onChangechange 事件触发的回调函数(val: string): void-
onBlurblur 事件触发的回调函数(val: string): void-
onFocusfocus 事件触发的回调函数(val: string): void-
error报错样式boolfalse
onErrorClick点击报错 icon 触发的回调函数(e: Object): void
extra右边注释string or node''
onExtraClickextra 点击事件触发的回调函数(e: Object): void
labelNumber标签的文字个数,可用2-7之间的数字number5
autoFocus页面初始化时Input自动获取光标,每个页面只有一个Input的autpFocus会生效。(不保证所有浏览器都生效)boolfalse
focused页面运行过程中,Input获取光标,当Input获取光标(focused更新为true)后,需要在onFocus或者onBlur时再次将该属性设置为false。boolfalse
updatePlaceholder (web only)当清除内容时,是否将清除前的内容替换到 placeholder 中boolfalse
prefixListCls (web only)列表 className 前缀Stringam-list
name (web only)input 的 nameString
locale国际化,可覆盖全局LocaleProvider的配置, 当typemoney,可以自定义确认按钮的文案。Object: { confirmLabel }

更多 react-native InputItem 属性请参考 react-native TextInput (http://facebook.github.io/react-native/docs/textinput.html)