Input输入框

通过鼠标或键盘输入内容,是最基础的表单域的包装。

何时使用

  • 需要用户输入表单域内容时。

  • 提供组合型输入框,带搜索的输入框,还可以进行大小选择。

代码演示

Input输入框 - 图1

基本使用

基本使用。

  1. import { Input } from 'antd';
  2. ReactDOM.render(<Input placeholder="Basic usage" />, mountNode);

Input输入框 - 图2

前置/后置标签

用于配置一些固定组合。

  1. import { Input, Select } from 'antd';
  2. import { SettingOutlined } from '@ant-design/icons';
  3. const { Option } = Select;
  4. const selectBefore = (
  5. <Select defaultValue="http://" className="select-before">
  6. <Option value="http://">http://</Option>
  7. <Option value="https://">https://</Option>
  8. </Select>
  9. );
  10. const selectAfter = (
  11. <Select defaultValue=".com" className="select-after">
  12. <Option value=".com">.com</Option>
  13. <Option value=".jp">.jp</Option>
  14. <Option value=".cn">.cn</Option>
  15. <Option value=".org">.org</Option>
  16. </Select>
  17. );
  18. ReactDOM.render(
  19. <>
  20. <div style={{ marginBottom: 16 }}>
  21. <Input addonBefore="http://" addonAfter=".com" defaultValue="mysite" />
  22. </div>
  23. <div style={{ marginBottom: 16 }}>
  24. <Input addonBefore={selectBefore} addonAfter={selectAfter} defaultValue="mysite" />
  25. </div>
  26. <div style={{ marginBottom: 16 }}>
  27. <Input addonAfter={<SettingOutlined />} defaultValue="mysite" />
  28. </div>
  29. <div style={{ marginBottom: 16 }}>
  30. <Input addonBefore="http://" suffix=".com" defaultValue="mysite" />
  31. </div>
  32. </>,
  33. mountNode,
  34. );
  1. .select-before {
  2. width: 90px;
  3. }
  4. .select-after {
  5. width: 80px;
  6. }
  7. [data-theme='compact'] .select-before {
  8. width: 71px;
  9. }
  10. [data-theme='compact'] .select-after {
  11. width: 65px;
  12. }

Input输入框 - 图3

搜索框

带有搜索按钮的输入框。

  1. import { Input } from 'antd';
  2. import { AudioOutlined } from '@ant-design/icons';
  3. const { Search } = Input;
  4. const suffix = (
  5. <AudioOutlined
  6. style={{
  7. fontSize: 16,
  8. color: '#1890ff',
  9. }}
  10. />
  11. );
  12. ReactDOM.render(
  13. <>
  14. <Search
  15. placeholder="input search text"
  16. onSearch={value => console.log(value)}
  17. style={{ width: 200 }}
  18. />
  19. <br />
  20. <br />
  21. <Search placeholder="input search text" onSearch={value => console.log(value)} enterButton />
  22. <br />
  23. <br />
  24. <Search
  25. placeholder="input search text"
  26. enterButton="Search"
  27. size="large"
  28. onSearch={value => console.log(value)}
  29. />
  30. <br />
  31. <br />
  32. <Search
  33. placeholder="input search text"
  34. enterButton="Search"
  35. size="large"
  36. suffix={suffix}
  37. onSearch={value => console.log(value)}
  38. />
  39. </>,
  40. mountNode,
  41. );

Input输入框 - 图4

文本域

用于多行输入。

  1. import { Input } from 'antd';
  2. const { TextArea } = Input;
  3. ReactDOM.render(<TextArea rows={4} />, mountNode);

Input输入框 - 图5

输入时格式化展示

结合 Tooltip 组件,实现一个数值输入框,方便内容超长时的全量展现。

  1. import { Input, Tooltip } from 'antd';
  2. function formatNumber(value) {
  3. value += '';
  4. const list = value.split('.');
  5. const prefix = list[0].charAt(0) === '-' ? '-' : '';
  6. let num = prefix ? list[0].slice(1) : list[0];
  7. let result = '';
  8. while (num.length > 3) {
  9. result = `,${num.slice(-3)}${result}`;
  10. num = num.slice(0, num.length - 3);
  11. }
  12. if (num) {
  13. result = num + result;
  14. }
  15. return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
  16. }
  17. class NumericInput extends React.Component {
  18. onChange = e => {
  19. const { value } = e.target;
  20. const reg = /^-?\d*(\.\d*)?$/;
  21. if ((!isNaN(value) && reg.test(value)) || value === '' || value === '-') {
  22. this.props.onChange(value);
  23. }
  24. };
  25. // '.' at the end or only '-' in the input box.
  26. onBlur = () => {
  27. const { value, onBlur, onChange } = this.props;
  28. let valueTemp = value;
  29. if (value.charAt(value.length - 1) === '.' || value === '-') {
  30. valueTemp = value.slice(0, -1);
  31. }
  32. onChange(valueTemp.replace(/0*(\d+)/, '$1'));
  33. if (onBlur) {
  34. onBlur();
  35. }
  36. };
  37. render() {
  38. const { value } = this.props;
  39. const title = value ? (
  40. <span className="numeric-input-title">{value !== '-' ? formatNumber(value) : '-'}</span>
  41. ) : (
  42. 'Input a number'
  43. );
  44. return (
  45. <Tooltip
  46. trigger={['focus']}
  47. title={title}
  48. placement="topLeft"
  49. overlayClassName="numeric-input"
  50. >
  51. <Input
  52. {...this.props}
  53. onChange={this.onChange}
  54. onBlur={this.onBlur}
  55. placeholder="Input a number"
  56. maxLength={25}
  57. />
  58. </Tooltip>
  59. );
  60. }
  61. }
  62. class NumericInputDemo extends React.Component {
  63. constructor(props) {
  64. super(props);
  65. this.state = { value: '' };
  66. }
  67. onChange = value => {
  68. this.setState({ value });
  69. };
  70. render() {
  71. return (
  72. <NumericInput style={{ width: 120 }} value={this.state.value} onChange={this.onChange} />
  73. );
  74. }
  75. }
  76. ReactDOM.render(<NumericInputDemo />, mountNode);
  1. /* to prevent the arrow overflow the popup container,
  2. or the height is not enough when content is empty */
  3. .numeric-input .ant-tooltip-inner {
  4. min-width: 32px;
  5. min-height: 37px;
  6. }
  7. .numeric-input .numeric-input-title {
  8. font-size: 14px;
  9. }

Input输入框 - 图6

密码框

密码框。

  1. import { Input, Space } from 'antd';
  2. import { EyeInvisibleOutlined, EyeTwoTone } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <Space direction="vertical">
  5. <Input.Password placeholder="input password" />
  6. <Input.Password
  7. placeholder="input password"
  8. iconRender={visible => (visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />)}
  9. />
  10. </Space>,
  11. mountNode,
  12. );

Input输入框 - 图7

三种大小

我们为 <Input /> 输入框定义了三种尺寸(大、默认、小),高度分别为 40px32px24px

  1. import { Input } from 'antd';
  2. import { UserOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <>
  5. <Input size="large" placeholder="large size" prefix={<UserOutlined />} />
  6. <br />
  7. <br />
  8. <Input placeholder="default size" prefix={<UserOutlined />} />
  9. <br />
  10. <br />
  11. <Input size="small" placeholder="small size" prefix={<UserOutlined />} />
  12. </>,
  13. mountNode,
  14. );

Input输入框 - 图8

输入框组合

输入框的组合展现。

注意:使用 compact 模式时,不需要通过 Col 来控制宽度。

  1. import { Input, Col, Row, Select, InputNumber, DatePicker, AutoComplete, Cascader } from 'antd';
  2. const { Option } = Select;
  3. const options = [
  4. {
  5. value: 'zhejiang',
  6. label: 'Zhejiang',
  7. children: [
  8. {
  9. value: 'hangzhou',
  10. label: 'Hangzhou',
  11. children: [
  12. {
  13. value: 'xihu',
  14. label: 'West Lake',
  15. },
  16. ],
  17. },
  18. ],
  19. },
  20. {
  21. value: 'jiangsu',
  22. label: 'Jiangsu',
  23. children: [
  24. {
  25. value: 'nanjing',
  26. label: 'Nanjing',
  27. children: [
  28. {
  29. value: 'zhonghuamen',
  30. label: 'Zhong Hua Men',
  31. },
  32. ],
  33. },
  34. ],
  35. },
  36. ];
  37. const App = () => (
  38. <div className="site-input-group-wrapper">
  39. <Input.Group size="large">
  40. <Row gutter={8}>
  41. <Col span={5}>
  42. <Input defaultValue="0571" />
  43. </Col>
  44. <Col span={8}>
  45. <Input defaultValue="26888888" />
  46. </Col>
  47. </Row>
  48. </Input.Group>
  49. <br />
  50. <Input.Group compact>
  51. <Input style={{ width: '20%' }} defaultValue="0571" />
  52. <Input style={{ width: '30%' }} defaultValue="26888888" />
  53. </Input.Group>
  54. <br />
  55. <Input.Group compact>
  56. <Select defaultValue="Zhejiang">
  57. <Option value="Zhejiang">Zhejiang</Option>
  58. <Option value="Jiangsu">Jiangsu</Option>
  59. </Select>
  60. <Input style={{ width: '50%' }} defaultValue="Xihu District, Hangzhou" />
  61. </Input.Group>
  62. <br />
  63. <Input.Group compact>
  64. <Input style={{ width: '20%' }} defaultValue="0571" />
  65. <Input.Search style={{ width: '30%' }} defaultValue="26888888" />
  66. </Input.Group>
  67. <br />
  68. <Input.Group compact>
  69. <Select defaultValue="Option1">
  70. <Option value="Option1">Option1</Option>
  71. <Option value="Option2">Option2</Option>
  72. </Select>
  73. <Input style={{ width: '50%' }} defaultValue="input content" />
  74. <InputNumber />
  75. </Input.Group>
  76. <br />
  77. <Input.Group compact>
  78. <Input style={{ width: '50%' }} defaultValue="input content" />
  79. <DatePicker style={{ width: '50%' }} />
  80. </Input.Group>
  81. <br />
  82. <Input.Group compact>
  83. <Input style={{ width: '30%' }} defaultValue="input content" />
  84. <DatePicker.RangePicker style={{ width: '70%' }} />
  85. </Input.Group>
  86. <br />
  87. <Input.Group compact>
  88. <Select defaultValue="Option1-1">
  89. <Option value="Option1-1">Option1-1</Option>
  90. <Option value="Option1-2">Option1-2</Option>
  91. </Select>
  92. <Select defaultValue="Option2-2">
  93. <Option value="Option2-1">Option2-1</Option>
  94. <Option value="Option2-2">Option2-2</Option>
  95. </Select>
  96. </Input.Group>
  97. <br />
  98. <Input.Group compact>
  99. <Select defaultValue="1">
  100. <Option value="1">Between</Option>
  101. <Option value="2">Except</Option>
  102. </Select>
  103. <Input style={{ width: 100, textAlign: 'center' }} placeholder="Minimum" />
  104. <Input
  105. className="site-input-split"
  106. style={{
  107. width: 30,
  108. borderLeft: 0,
  109. borderRight: 0,
  110. pointerEvents: 'none',
  111. }}
  112. placeholder="~"
  113. disabled
  114. />
  115. <Input
  116. className="site-input-right"
  117. style={{
  118. width: 100,
  119. textAlign: 'center',
  120. }}
  121. placeholder="Maximum"
  122. />
  123. </Input.Group>
  124. <br />
  125. <Input.Group compact>
  126. <Select defaultValue="Sign Up" style={{ width: '30%' }}>
  127. <Option value="Sign Up">Sign Up</Option>
  128. <Option value="Sign In">Sign In</Option>
  129. </Select>
  130. <AutoComplete
  131. style={{ width: '70%' }}
  132. placeholder="Email"
  133. options={[{ value: 'text 1' }, { value: 'text 2' }]}
  134. />
  135. </Input.Group>
  136. <br />
  137. <Input.Group compact>
  138. <Select style={{ width: '30%' }} defaultValue="Home">
  139. <Option value="Home">Home</Option>
  140. <Option value="Company">Company</Option>
  141. </Select>
  142. <Cascader style={{ width: '70%' }} options={options} placeholder="Select Address" />
  143. </Input.Group>
  144. </div>
  145. );
  146. ReactDOM.render(<App />, mountNode);
  1. .site-input-group-wrapper .site-input-split {
  2. background-color: #fff;
  3. }
  4. .site-input-group-wrapper .site-input-right {
  5. border-left-width: 0;
  6. }
  7. .site-input-group-wrapper .site-input-right:hover,
  8. .site-input-group-wrapper .site-input-right:focus {
  9. border-left-width: 1px;
  10. }
  11. .site-input-group-wrapper .ant-input-rtl.site-input-right {
  12. border-right-width: 0;
  13. }
  14. .site-input-group-wrapper .ant-input-rtl.site-input-right:hover,
  15. .site-input-group-wrapper .ant-input-rtl.site-input-right:focus {
  16. border-right-width: 1px;
  17. }

Input输入框 - 图9

搜索框 loading

用于 onSearch 的时候展示 loading

  1. import { Input } from 'antd';
  2. const { Search } = Input;
  3. ReactDOM.render(
  4. <>
  5. <Search placeholder="input search loading deault" loading />
  6. <br />
  7. <br />
  8. <Search placeholder="input search loading with enterButton" loading enterButton />
  9. </>,
  10. mountNode,
  11. );

Input输入框 - 图10

适应文本高度的文本域

autoSize 属性适用于 textarea 节点,并且只有高度会自动变化。另外 autoSize 可以设定为一个对象,指定最小行数和最大行数。

  1. import { Input } from 'antd';
  2. const { TextArea } = Input;
  3. class Demo extends React.Component {
  4. state = {
  5. value: '',
  6. };
  7. onChange = ({ target: { value } }) => {
  8. this.setState({ value });
  9. };
  10. render() {
  11. const { value } = this.state;
  12. return (
  13. <>
  14. <TextArea placeholder="Autosize height based on content lines" autoSize />
  15. <div style={{ margin: '24px 0' }} />
  16. <TextArea
  17. placeholder="Autosize height with minimum and maximum number of lines"
  18. autoSize={{ minRows: 2, maxRows: 6 }}
  19. />
  20. <div style={{ margin: '24px 0' }} />
  21. <TextArea
  22. value={value}
  23. onChange={this.onChange}
  24. placeholder="Controlled autosize"
  25. autoSize={{ minRows: 3, maxRows: 5 }}
  26. />
  27. </>
  28. );
  29. }
  30. }
  31. ReactDOM.render(<Demo />, mountNode);

Input输入框 - 图11

前缀和后缀

在输入框上添加前缀或后缀图标。

  1. import { Input, Tooltip } from 'antd';
  2. import { InfoCircleOutlined, UserOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <>
  5. <Input
  6. placeholder="Enter your username"
  7. prefix={<UserOutlined className="site-form-item-icon" />}
  8. suffix={
  9. <Tooltip title="Extra information">
  10. <InfoCircleOutlined style={{ color: 'rgba(0,0,0,.45)' }} />
  11. </Tooltip>
  12. }
  13. />
  14. <br />
  15. <br />
  16. <Input prefix="¥" suffix="RMB" />
  17. <br />
  18. <br />
  19. <Input prefix="¥" suffix="RMB" disabled />
  20. </>,
  21. mountNode,
  22. );

Input输入框 - 图12

带移除图标

带移除图标的输入框,点击图标删除所有内容。

  1. import { Input } from 'antd';
  2. const { TextArea } = Input;
  3. const onChange = e => {
  4. console.log(e);
  5. };
  6. ReactDOM.render(
  7. <>
  8. <Input placeholder="input with clear icon" allowClear onChange={onChange} />
  9. <br />
  10. <br />
  11. <TextArea placeholder="textarea with clear icon" allowClear onChange={onChange} />
  12. </>,
  13. mountNode,
  14. );

API

Input

参数说明类型默认值
addonAfter带标签的 input,设置后置标签string|ReactNode
addonBefore带标签的 input,设置前置标签string|ReactNode
defaultValue输入框默认内容string
disabled是否禁用状态,默认为 falsebooleanfalse
id输入框的 idstring
maxLength最大长度number
prefix带有前缀图标的 inputstring|ReactNode
size控件大小。注:标准表单内的输入框大小限制为 largelarge | middle | small
suffix带有后缀图标的 inputstring|ReactNode
type声明 input 类型,同原生 input 标签的 type 属性,见:MDN(请直接使用 Input.TextArea 代替 type=”textarea”)。stringtext
value输入框内容string
onChange输入框内容变化时的回调function(e)
onPressEnter按下回车的回调function(e)
allowClear可以点击清除图标删除内容boolean

如果 InputForm.Item 内,并且 Form.Item 设置了 idoptions 属性,则 value defaultValueid 属性会被自动设置。

Input 的其他属性和 React 自带的 input 一致。

Input.TextArea

参数说明类型默认值
autoSize自适应内容高度,可设置为 true|false 或对象:{ minRows: 2, maxRows: 6 }boolean|objectfalse
defaultValue输入框默认内容string
value输入框内容string
onPressEnter按下回车的回调function(e)
allowClear可以点击清除图标删除内容boolean
onResizeresize 回调function({ width, height })

Input.TextArea 的其他属性和浏览器自带的 textarea 一致。

Input.Search

参数说明类型默认值
enterButton是否有确认按钮,可设为按钮文字。该属性会与 addonAfter 冲突。boolean|ReactNodefalse
onSearch点击搜索或按下回车键时的回调function(value, event)
loading搜索 loadingboolean

其余属性和 Input 一致。

Input.Group

参数说明类型默认值
compact是否用紧凑模式booleanfalse
sizeInput.Group 中所有的 Input 的大小,可选 large default smallstringdefault
  1. <Input.Group>
  2. <input />
  3. <input />
  4. </Input.Group>

Input.Password

参数说明类型默认值
visibilityToggle是否显示切换按钮booleantrue

FAQ

为什么我动态改变 prefix/suffix 时,Input 会失去焦点?

当 Input 动态添加或者删除 prefix/suffix 时,React 会重新创建 DOM 结构而新的 input 是没有焦点的。你可以预设一个空的 <span /> 来保持 DOM 结构不变:

  1. const suffix = condition ? <Icon type="smile" /> : <span />;
  2. <Input suffix={suffix} />;