Input输入框

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

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

    基本使用

    基本使用。

    Input输入框 - 图2

    用于配置一些固定组合。

    1. const { Option } = Select;
    2. const selectBefore = (
    3. <Select defaultValue="Http://" style={{ width: 90 }}>
    4. <Option value="Http://">Http://</Option>
    5. <Option value="Https://">Https://</Option>
    6. </Select>
    7. );
    8. const selectAfter = (
    9. <Select defaultValue=".com" style={{ width: 80 }}>
    10. <Option value=".com">.com</Option>
    11. <Option value=".jp">.jp</Option>
    12. <Option value=".cn">.cn</Option>
    13. <Option value=".org">.org</Option>
    14. </Select>
    15. );
    16. ReactDOM.render(
    17. <div>
    18. <div style={{ marginBottom: 16 }}>
    19. <Input addonBefore="Http://" addonAfter=".com" defaultValue="mysite" />
    20. </div>
    21. <div style={{ marginBottom: 16 }}>
    22. <Input addonBefore={selectBefore} addonAfter={selectAfter} defaultValue="mysite" />
    23. </div>
    24. <div style={{ marginBottom: 16 }}>
    25. <Input addonAfter={<Icon type="setting" />} defaultValue="mysite" />
    26. </div>
    27. </div>,
    28. mountNode,
    29. );

    搜索框

    带有搜索按钮的输入框,2.5.0 时新增。

    1. import { Input } from 'antd';
    2. const { Search } = Input;
    3. ReactDOM.render(
    4. <div>
    5. <Search
    6. placeholder="input search text"
    7. onSearch={value => console.log(value)}
    8. style={{ width: 200 }}
    9. />
    10. <br />
    11. <br />
    12. <Search placeholder="input search text" onSearch={value => console.log(value)} enterButton />
    13. <br />
    14. <br />
    15. <Search
    16. placeholder="input search text"
    17. enterButton="Search"
    18. size="large"
    19. onSearch={value => console.log(value)}
    20. />
    21. </div>,
    22. mountNode,
    23. );

    Input输入框 - 图4

    1. import { Input } from 'antd';
    2. const { TextArea } = Input;
    3. ReactDOM.render(
    4. <div>
    5. <TextArea placeholder="Autosize height based on content lines" autosize />
    6. <div style={{ margin: '24px 0' }} />
    7. <TextArea
    8. placeholder="Autosize height with minimum and maximum number of lines"
    9. autosize={{ minRows: 2, maxRows: 6 }}
    10. />
    11. </div>,
    12. mountNode,
    13. );

    前缀和后缀

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

    1. import { Input, Tooltip, Icon } from 'antd';
    2. ReactDOM.render(
    3. <Input
    4. placeholder="Enter your username"
    5. prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
    6. suffix={
    7. <Tooltip title="Extra information">
    8. <Icon type="info-circle" style={{ color: 'rgba(0,0,0,.45)' }} />
    9. </Tooltip>
    10. />,
    11. mountNode,
    12. );

    Input输入框 - 图6

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

    三种大小

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

    1. import { Input } from 'antd';
    2. ReactDOM.render(
    3. <div className="example-input">
    4. <Input size="large" placeholder="large size" />
    5. <Input placeholder="default size" />
    6. <Input size="small" placeholder="small size" />
    7. </div>,
    8. mountNode,
    9. );
    1. width: 200px;
    2. margin: 0 8px 8px 0;
    3. }

    Input输入框 - 图8

    输入框的组合展现。

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

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

    文本域

    用于多行输入。

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

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

    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输入框 - 图11

    密码框,版本 3.12.0 中新增。

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

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

    2.12 后新增的组件,旧版请使用 Input[type=textarea]

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

    其余属性和 Input 一致。

    Input.Group

    1. <Input.Group>
    2. <input />
    3. <input />
    4. </Input.Group>

    Input.Password (3.12.0 中新增)

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