Input输入框

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

何时使用

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

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

代码演示

Input 输入框 - 图1

基本使用

基本使用。

  1. import { Input, Row, Col } from 'choerodon-ui';
  2. ReactDOM.render(
  3. <Row gutter={8}>
  4. <Col span={12}>
  5. <Input placeholder="Basic usage" maxLength={50} required label="Basic" copy />
  6. </Col>
  7. <Col span={12}>
  8. <Input defaultValue="123" placeholder="Basic usage" maxLength={20} required label="Basic" disabled />
  9. </Col>
  10. </Row>,
  11. mountNode);

Input 输入框 - 图2

前置/后置标签

用于配置一些固定组合。

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

Input 输入框 - 图3

搜索框

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

  1. import { Input } from 'choerodon-ui';
  2. const Search = Input.Search;
  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 placeholder="input search text" enterButton="Search" size="large" />
  16. </div>,
  17. mountNode,
  18. );

Input 输入框 - 图4

适应文本高度的文本域

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

  1. import { Input } from 'choerodon-ui';
  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. );

Input 输入框 - 图5

前缀和后缀

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

  1. import { Input, Icon } from 'choerodon-ui';
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. userName: 'xxx',
  7. };
  8. }
  9. emitEmpty = () => {
  10. this.userNameInput.focus();
  11. this.setState({ userName: '' });
  12. }
  13. onChangeUserName = (e) => {
  14. this.setState({ userName: e.target.value });
  15. }
  16. render() {
  17. const { userName } = this.state;
  18. const suffix = userName ? <Icon type="close" onClick={this.emitEmpty} /> : null;
  19. return (
  20. <Input
  21. placeholder="Enter your username"
  22. prefix="input-"
  23. suffix={suffix}
  24. label="username"
  25. value={userName}
  26. onChange={this.onChangeUserName}
  27. ref={node => this.userNameInput = node}
  28. copy
  29. />
  30. );
  31. }
  32. }
  33. ReactDOM.render(<App />, mountNode);
  1. .icon-close {
  2. cursor: pointer;
  3. color: #ccc;
  4. transition: color 0.3s;
  5. font-size: 12px;
  6. }
  7. .icon-close:hover {
  8. color: #999;
  9. }
  10. .icon-close:active {
  11. color: #666;
  12. }

Input 输入框 - 图6

三种大小

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

注意: 在表单里面,我们只使用大尺寸的输入框。

  1. import { Input } from 'choerodon-ui';
  2. ReactDOM.render(
  3. <div className="example-input">
  4. <Input size="large" placeholder="large size" label="Large" maxLength="20" />
  5. <Input placeholder="default size" />
  6. <Input size="small" placeholder="small size" label="small" />
  7. </div>,
  8. mountNode);

Input 输入框 - 图7

输入框组合

输入框的组合展现。

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

  1. import { Input, Col, Select, InputNumber, DatePicker, AutoComplete, Cascader } from 'choerodon-ui';
  2. const InputGroup = Input.Group;
  3. const Option = Select.Option;
  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. <Col span={5}>
  55. <Input defaultValue="0571" />
  56. </Col>
  57. <Col span={8}>
  58. <Input defaultValue="26888888" />
  59. </Col>
  60. </InputGroup>
  61. <br />
  62. <InputGroup compact>
  63. <Input style={{ width: '20%' }} defaultValue="0571" />
  64. <Input style={{ width: '30%' }} defaultValue="26888888" />
  65. </InputGroup>
  66. <br />
  67. <InputGroup compact>
  68. <Select defaultValue="Zhejiang">
  69. <Option value="Zhejiang">Zhejiang</Option>
  70. <Option value="Jiangsu">Jiangsu</Option>
  71. </Select>
  72. <Input style={{ width: '50%' }} defaultValue="Xihu District, Hangzhou" />
  73. </InputGroup>
  74. <br />
  75. <InputGroup compact>
  76. <Select defaultValue="Option1">
  77. <Option value="Option1">Option1</Option>
  78. <Option value="Option2">Option2</Option>
  79. </Select>
  80. <Input style={{ width: '50%' }} defaultValue="input content" />
  81. <InputNumber />
  82. </InputGroup>
  83. <br />
  84. <InputGroup compact>
  85. <Input style={{ width: '50%' }} defaultValue="input content" />
  86. <DatePicker />
  87. </InputGroup>
  88. <br />
  89. <InputGroup compact>
  90. <Select defaultValue="Option1-1">
  91. <Option value="Option1-1">Option1-1</Option>
  92. <Option value="Option1-2">Option1-2</Option>
  93. </Select>
  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={{ width: 30, borderLeft: 0, pointerEvents: 'none', backgroundColor: '#fff' }}
  108. value="~"
  109. disabled
  110. />
  111. <Input style={{ width: 100, textAlign: 'center', borderLeft: 0 }} placeholder="Maximum" />
  112. </InputGroup>
  113. <br />
  114. <InputGroup compact>
  115. <Select defaultValue="Sign Up">
  116. <Option value="Sign Up">Sign Up</Option>
  117. <Option value="Sign In">Sign In</Option>
  118. </Select>
  119. <AutoComplete
  120. dataSource={this.state.dataSource}
  121. style={{ width: 200 }}
  122. onChange={this.handleChange}
  123. placeholder="Email"
  124. />
  125. </InputGroup>
  126. <br />
  127. <InputGroup compact>
  128. <Select style={{ width: '30%' }} defaultValue="Home">
  129. <Option value="Home">Home</Option>
  130. <Option value="Company">Company</Option>
  131. </Select>
  132. <Cascader style={{ width: '70%' }} options={options} placeholder="Select Address" />
  133. </InputGroup>
  134. </div>
  135. );
  136. }
  137. }
  138. ReactDOM.render(<CompactDemo />, mountNode);

Input 输入框 - 图8

文本域

用于多行输入。

  1. import { Input } from 'choerodon-ui';
  2. const { TextArea } = Input;
  3. ReactDOM.render(
  4. <TextArea rows={4} maxLength={20} label="textarea" placeholder="textarea usage" />,
  5. mountNode,
  6. );

Input 输入框 - 图9

输入时格式化展示

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

  1. import { Input, Tooltip } from 'choerodon-ui';
  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 = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
  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. if (value.charAt(value.length - 1) === '.' || value === '-') {
  29. onChange({ value: value.slice(0, -1) });
  30. }
  31. if (onBlur) {
  32. onBlur();
  33. }
  34. }
  35. render() {
  36. const { value } = this.props;
  37. const title = value ? (
  38. <span className="numeric-input-title">
  39. {value !== '-' ? formatNumber(value) : '-'}
  40. </span>
  41. ) : 'Input a number';
  42. return (
  43. <Tooltip
  44. trigger={['focus']}
  45. title={title}
  46. placement="topLeft"
  47. overlayClassName="numeric-input"
  48. >
  49. <Input
  50. {...this.props}
  51. onChange={this.onChange}
  52. onBlur={this.onBlur}
  53. placeholder="Input a number"
  54. maxLength="25"
  55. />
  56. </Tooltip>
  57. );
  58. }
  59. }
  60. class NumericInputDemo extends React.Component {
  61. constructor(props) {
  62. super(props);
  63. this.state = { value: '' };
  64. }
  65. onChange = (value) => {
  66. this.setState({ value });
  67. }
  68. render() {
  69. return <NumericInput style={{ width: 120 }} value={this.state.value} onChange={this.onChange} />;
  70. }
  71. }
  72. 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 .c7n-tooltip-inner {
  4. min-width: 32px;
  5. min-height: 37px;
  6. }
  7. .numeric-input .numeric-input-title {
  8. font-size: 14px;
  9. }

Input 输入框 - 图10

密码输入框

密码输入框

  1. import { Input, Row, Col } from 'choerodon-ui';
  2. ReactDOM.render(
  3. <Row gutter={8}>
  4. <Col span={8}>
  5. <Input placeholder="username" maxLength={50} required label="username" />
  6. </Col>
  7. <Col span={8}>
  8. <Input type="password" placeholder="password" maxLength={20} required label="password" />
  9. </Col>
  10. <Col span={8}>
  11. <Input type="password" showPasswordEye placeholder="password" maxLength={20} required label="password" />
  12. </Col>
  13. </Row>,
  14. mountNode);

API

Input

参数说明类型默认值
addonAfter带标签的 input,设置后置标签string|ReactNode
addonBefore带标签的 input,设置前置标签string|ReactNode
defaultValue输入框默认内容string
disabled是否禁用状态,默认为 falsebooleanfalse
showPasswordEye是否显示type为password的俺就按住显示密码的眼睛,默认为 falsebooleanfalse
id输入框的 idstring
prefix带有前缀图标的 inputstring|ReactNode
size控件大小。注:标准表单内的输入框大小限制为 large。可选 large default smallstringdefault
suffix带有后缀图标的 inputstring|ReactNode
type声明 input 类型,同原生 input 标签的 type 属性,见:MDN(请直接使用 Input.TextArea 代替 type="textarea")。stringtext
value输入框内容string
onPressEnter按下回车的回调function(e)
copy显示复制按钮booleanfalse
onCopy点击复制按钮的回调function(copyValue)
underlineinput 下划线booleantrue

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

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

Input.TextArea

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

参数说明类型默认值
autosize自适应内容高度,可设置为 true|false 或对象:{ minRows: 2, maxRows: 6 }boolean|objectfalse
defaultValue输入框默认内容string
value输入框内容string
onPressEnter按下回车的回调function(e)

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

参数说明类型默认值
enterButton是否有确认按钮,可设为按钮文字boolean|ReactNodefalse
onSearch点击搜索或按下回车键时的回调function(value)

其余属性和 Input 一致。

Input.Group

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