InputItem 文本输入

用于接受单行文本。

规则

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

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

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

代码演示

金额键盘

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

  1. import { List, InputItem } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. // 通过自定义 moneyKeyboardWrapProps 修复虚拟键盘滚动穿透问题
  4. // https://github.com/ant-design/ant-design-mobile/issues/307
  5. // https://github.com/ant-design/ant-design-mobile/issues/163
  6. const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
  7. let moneyKeyboardWrapProps;
  8. if (isIPhone) {
  9. moneyKeyboardWrapProps = {
  10. onTouchStart: e => e.preventDefault(),
  11. };
  12. }
  13. class H5NumberInputExample extends React.Component {
  14. state = {
  15. type: 'money',
  16. }
  17. render() {
  18. const { getFieldProps } = this.props.form;
  19. const { type } = this.state;
  20. return (
  21. <div>
  22. <List>
  23. <InputItem
  24. {...getFieldProps('money3')}
  25. type={type}
  26. defaultValue={100}
  27. placeholder="start from left"
  28. clear
  29. moneyKeyboardAlign="left"
  30. moneyKeyboardWrapProps={moneyKeyboardWrapProps}
  31. >光标在左</InputItem>
  32. <InputItem
  33. type={type}
  34. placeholder="start from right"
  35. clear
  36. onChange={(v) => { console.log('onChange', v); }}
  37. onBlur={(v) => { console.log('onBlur', v); }}
  38. moneyKeyboardWrapProps={moneyKeyboardWrapProps}
  39. >光标在右</InputItem>
  40. <InputItem
  41. {...getFieldProps('money2', {
  42. normalize: (v, prev) => {
  43. if (v && !/^(([1-9]\d*)|0)(\.\d{0,2}?)?$/.test(v)) {
  44. if (v === '.') {
  45. return '0.';
  46. }
  47. return prev;
  48. }
  49. return v;
  50. },
  51. })}
  52. type={type}
  53. placeholder="money format"
  54. ref={el => this.inputRef = el}
  55. onVirtualKeyboardConfirm={v => console.log('onVirtualKeyboardConfirm:', v)}
  56. clear
  57. moneyKeyboardWrapProps={moneyKeyboardWrapProps}
  58. disabledKeys={['.', '0', '3']}
  59. >数字键盘</InputItem>
  60. <List.Item>
  61. <div
  62. style={{ width: '100%', color: '#108ee9', textAlign: 'center' }}
  63. onClick={() => this.inputRef.focus()}
  64. >
  65. click to focus
  66. </div>
  67. </List.Item>
  68. </List>
  69. </div>
  70. );
  71. }
  72. }
  73. const H5NumberInputExampleWrapper = createForm()(H5NumberInputExample);
  74. 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. componentDidMount() {
  5. // this.autoFocusInst.focus();
  6. }
  7. handleClick = () => {
  8. this.inputRef.focus();
  9. }
  10. render() {
  11. const { getFieldProps } = this.props.form;
  12. return (
  13. <div>
  14. <List renderHeader={() => 'Customize to focus'}>
  15. <InputItem
  16. {...getFieldProps('autofocus')}
  17. clear
  18. placeholder="auto focus"
  19. ref={el => this.autoFocusInst = el}
  20. >标题</InputItem>
  21. <InputItem
  22. {...getFieldProps('focus')}
  23. clear
  24. placeholder="click the button below to focus"
  25. ref={el => this.inputRef = el}
  26. >标题</InputItem>
  27. <List.Item>
  28. <div
  29. style={{ width: '100%', color: '#108ee9', textAlign: 'center' }}
  30. onClick={this.handleClick}
  31. >
  32. click to focus
  33. </div>
  34. </List.Item>
  35. </List>
  36. <List renderHeader={() => 'Whether is controlled'}>
  37. <InputItem
  38. {...getFieldProps('control')}
  39. placeholder="controled input"
  40. >受控组件</InputItem>
  41. <InputItem
  42. defaultValue="Title"
  43. placeholder="please input content"
  44. data-seed="logId"
  45. >非受控组件</InputItem>
  46. </List>
  47. <WhiteSpace />
  48. <List renderHeader={() => 'Click label to focus input'}>
  49. <InputItem
  50. placeholder="click label to focus input"
  51. ref={el => this.labelFocusInst = el}
  52. ><div onClick={() => this.labelFocusInst.focus()}>标题</div></InputItem>
  53. </List>
  54. <List renderHeader={() => 'Show clear'}>
  55. <InputItem
  56. {...getFieldProps('inputclear')}
  57. clear
  58. placeholder="displayed clear while typing"
  59. >标题</InputItem>
  60. </List>
  61. <WhiteSpace />
  62. <List renderHeader={() => 'Number of words for title'}>
  63. <InputItem
  64. {...getFieldProps('label8')}
  65. placeholder="limited title length"
  66. labelNumber={5}
  67. >标题过长超过默认的5个字</InputItem>
  68. </List>
  69. <WhiteSpace />
  70. <List renderHeader={() => 'Custom title(text / image / empty)'}>
  71. <InputItem
  72. {...getFieldProps('input3')}
  73. placeholder="no label"
  74. />
  75. <InputItem
  76. {...getFieldProps('inputtitle2')}
  77. placeholder="title can be icon,image or text"
  78. >
  79. <div style={{ backgroundImage: 'url(https://zos.alipayobjects.com/rmsportal/DfkJHaJGgMghpXdqNaKF.png)', backgroundSize: 'cover', height: '22px', width: '22px' }} />
  80. </InputItem>
  81. </List>
  82. <WhiteSpace />
  83. <List renderHeader={() => 'Customize the extra content in the right'}>
  84. <InputItem
  85. {...getFieldProps('preice')}
  86. placeholder="0.00"
  87. extra="¥"
  88. >价格</InputItem>
  89. </List>
  90. <WhiteSpace />
  91. <List renderHeader={() => 'Format'}>
  92. <InputItem
  93. {...getFieldProps('bankCard', {
  94. initialValue: '8888 8888 8888 8888',
  95. })}
  96. type="bankCard"
  97. >银行卡</InputItem>
  98. <InputItem
  99. {...getFieldProps('phone')}
  100. type="phone"
  101. placeholder="186 1234 1234"
  102. >手机号码</InputItem>
  103. <InputItem
  104. {...getFieldProps('password')}
  105. type="password"
  106. placeholder="****"
  107. >密码</InputItem>
  108. <InputItem
  109. {...getFieldProps('number')}
  110. type="number"
  111. placeholder="click to show number keyboard"
  112. >数字键盘</InputItem>
  113. <InputItem
  114. {...getFieldProps('digit')}
  115. type="digit"
  116. placeholder="click to show native number keyboard"
  117. >数字键盘</InputItem>
  118. </List>
  119. <WhiteSpace />
  120. <List renderHeader={() => 'Not editable / Disabled'}>
  121. <InputItem
  122. value="not editable"
  123. editable={false}
  124. >姓名</InputItem>
  125. <InputItem
  126. value="style of disabled `InputItem`"
  127. disabled
  128. >姓名</InputItem>
  129. </List>
  130. </div>
  131. );
  132. }
  133. }
  134. const BasicInputExampleWrapper = createForm()(BasicInputExample);
  135. 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

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

属性说明类型默认值
type可以是银行卡bankCard; 手机号phone(此时最大长度固定为11,maxLength设置无效); 密码password; 数字number(为了尽量唤起带小数点的数字键盘,此类型并不是原生 number,而是<input type="text" pattern="[0-9]*" />); digit(表示原生的 number 类型); money(带小数点的模拟的数字键盘) 以及其他标准 html input type 类型Stringtext
valuevalue 值(受控与否参考https://facebook.github.io/react/docs/forms.html)String
defaultValue设置初始默认值String-
placeholderplaceholderString''
editable是否可编辑booltrue
disabled是否禁用boolfalse
clear是否带清除功能(仅editabletrue,disabledfalse才生效)boolfalse
maxLength最大长度number无。除money类型外,仅当text, email, search, password, tel, or url 有效。具体可以查看文档 https://developer.mozilla.org/zh-TW/docs/Web/HTML/Element/input,以及相关issuehttps://github.com/ant-design/ant-design-mobile/issues/2966
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
onVirtualKeyboardConfirm虚拟键盘点击确认时的回调函数(val: string): void
labelNumber标签的文字个数,可用2-7之间的数字number5
updatePlaceholder当清除内容时,是否将清除前的内容替换到 placeholder 中boolfalse
prefixListCls列表 className 前缀Stringam-list
nameinput 的 nameString
moneyKeyboardAlign文字排版起始方向, 只有 type='money' 支持, 可选为 'left', 'right'String'right'
moneyKeyboardWrapProps自定义金额虚拟键盘属性Object{}
moneyKeyboardHeader自定义金额虚拟键盘头部ReactNodenull
locale国际化,可覆盖全局LocaleProvider的配置, 当typemoney,可以自定义确认按钮的文案。Object: { confirmLabel }
autoAdjustHeight防止输入框被键盘遮挡。(仅 type=money时有效)boolfalse
disabledKeys禁用部分数字按键(仅 type=money时有效)arraynullnull

注意: 不要在受控组件的 onChange 事件中异步设置 value,否则中文输入可能带来问题,相关问题参考

注意: InputItemtype=number 时不支持输入负号, 你可以利用 type=text 来自己实现。

注意: 使用 moneyKeyboardHeader 时,页面中只能有一个 type=moneyInputItem

InputItem methods

属性说明类型默认值
focus使 input 聚焦(): void-