Input
A basic widget for getting the user input is a text field. Keyboard and mouse can be used for providing or changing data.
When To Use#
A user input in a form field is needed.
A search input is required.
Examples
import { Input } from 'antd';
ReactDOM.render(<Input placeholder="Basic usage" />, mountNode);
Http://.com
Http://
.com
import { Input, Select, Icon } from 'antd';
const Option = Select.Option;
const selectBefore = (
<Select defaultValue="Http://" style={{ width: 80 }}>
<Option value="Http://">Http://</Option>
<Option value="Https://">Https://</Option>
</Select>
);
const selectAfter = (
<Select defaultValue=".com" style={{ width: 70 }}>
<Option value=".com">.com</Option>
<Option value=".jp">.jp</Option>
<Option value=".cn">.cn</Option>
<Option value=".org">.org</Option>
</Select>
);
ReactDOM.render(
<div>
<div style={{ marginBottom: 16 }}>
<Input addonBefore="Http://" addonAfter=".com" defaultValue="mysite" />
</div>
<div style={{ marginBottom: 16 }}>
<Input addonBefore={selectBefore} addonAfter={selectAfter} defaultValue="mysite" />
</div>
<div style={{ marginBottom: 16 }}>
<Input addonAfter={<Icon type="setting" />} defaultValue="mysite" />
</div>
</div>
, mountNode);
import { Input } from 'antd';
const Search = Input.Search;
ReactDOM.render(
<Search
placeholder="input search text"
style={{ width: 200 }}
onSearch={value => console.log(value)}
/>
, mountNode);
import { Input } from 'antd';
const { TextArea } = Input;
ReactDOM.render(
<div>
<TextArea placeholder="Autosize height based on content lines" autosize />
<div style={{ margin: '24px 0' }} />
<TextArea placeholder="Autosize height with minimum and maximum number of lines" autosize={{ minRows: 2, maxRows: 6 }} />
</div>
, mountNode);
import { Input, Icon } from 'antd';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
userName: '',
};
}
emitEmpty = () => {
this.userNameInput.focus();
this.setState({ userName: '' });
}
onChangeUserName = (e) => {
this.setState({ userName: e.target.value });
}
render() {
const { userName } = this.state;
const suffix = userName ? <Icon type="close-circle" onClick={this.emitEmpty} /> : null;
return (
<Input
placeholder="Enter your userName"
prefix={<Icon type="user" />}
suffix={suffix}
value={userName}
onChange={this.onChangeUserName}
ref={node => this.userNameInput = node}
/>
);
}
}
ReactDOM.render(<App />, mountNode);
.anticon-close-circle {
cursor: pointer;
color: #ccc;
transition: color 0.3s;
font-size: 12px;
}
.anticon-close-circle:hover {
color: #999;
}
.anticon-close-circle:active {
color: #666;
}
import { Input } from 'antd';
ReactDOM.render(
<div className="example-input">
<Input size="large" placeholder="large size" />
<Input placeholder="default size" />
<Input size="small" placeholder="small size" />
</div>
, mountNode);
.example-input .ant-input {
width: 200px;
margin: 0 8px 8px 0;
}
Zhejiang
Option1
Option1-1
Option2-2
Between
Sign Up
Email
Home
import { Input, Col, Select, InputNumber, DatePicker, AutoComplete, Cascader } from 'antd';
const InputGroup = Input.Group;
const Option = Select.Option;
const options = [{
value: 'zhejiang',
label: 'Zhejiang',
children: [{
value: 'hangzhou',
label: 'Hangzhou',
children: [{
value: 'xihu',
label: 'West Lake',
}],
}],
}, {
value: 'jiangsu',
label: 'Jiangsu',
children: [{
value: 'nanjing',
label: 'Nanjing',
children: [{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
}],
}],
}];
class CompactDemo extends React.Component {
state = {
dataSource: [],
}
handleChange = (value) => {
this.setState({
dataSource: !value || value.indexOf('@') >= 0 ? [] : [
`${value}@gmail.com`,
`${value}@163.com`,
`${value}@qq.com`,
],
});
}
render() {
return (
<div>
<InputGroup size="large">
<Col span={4}>
<Input defaultValue="0571" />
</Col>
<Col span={8}>
<Input defaultValue="26888888" />
</Col>
</InputGroup>
<br />
<InputGroup compact>
<Input style={{ width: '20%' }} defaultValue="0571" />
<Input style={{ width: '30%' }} defaultValue="26888888" />
</InputGroup>
<br />
<InputGroup compact>
<Select defaultValue="Zhejiang">
<Option value="Zhejiang">Zhejiang</Option>
<Option value="Jiangsu">Jiangsu</Option>
</Select>
<Input style={{ width: '50%' }} defaultValue="Xihu District, Hangzhou" />
</InputGroup>
<br />
<InputGroup compact>
<Select defaultValue="Option1">
<Option value="Option1">Option1</Option>
<Option value="Option2">Option2</Option>
</Select>
<Input style={{ width: '50%' }} defaultValue="input content" />
<InputNumber />
</InputGroup>
<br />
<InputGroup compact>
<Input style={{ width: '50%' }} defaultValue="input content" />
<DatePicker />
</InputGroup>
<br />
<InputGroup compact>
<Select defaultValue="Option1-1">
<Option value="Option1-1">Option1-1</Option>
<Option value="Option1-2">Option1-2</Option>
</Select>
<Select defaultValue="Option2-2">
<Option value="Option2-1">Option2-1</Option>
<Option value="Option2-2">Option2-2</Option>
</Select>
</InputGroup>
<br />
<InputGroup compact>
<Select defaultValue="1">
<Option value="1">Between</Option>
<Option value="2">Except</Option>
</Select>
<Input style={{ width: 100, textAlign: 'center' }} placeholder="Minimum" />
<Input style={{ width: 24, borderLeft: 0, pointerEvents: 'none', backgroundColor: '#fff' }} placeholder="~" disabled />
<Input style={{ width: 100, textAlign: 'center', borderLeft: 0 }} placeholder="Maximum" />
</InputGroup>
<br />
<InputGroup compact>
<Select defaultValue="Sign Up">
<Option value="Sign Up">Sign Up</Option>
<Option value="Sign In">Sign In</Option>
</Select>
<AutoComplete
dataSource={this.state.dataSource}
style={{ width: 200 }}
onChange={this.handleChange}
placeholder="Email"
/>
</InputGroup>
<br />
<InputGroup compact>
<Select style={{ width: '30%' }} defaultValue="Home">
<Option value="Home">Home</Option>
<Option value="Company">Company</Option>
</Select>
<Cascader style={{ width: '70%' }} options={options} placeholder="Select Address" />
</InputGroup>
</div>
);
}
}
ReactDOM.render(<CompactDemo />, mountNode);
import { Input } from 'antd';
const { TextArea } = Input;
ReactDOM.render(<TextArea rows={4} />, mountNode);
import { Input, Tooltip } from 'antd';
function formatNumber(value) {
value += '';
const list = value.split('.');
const prefix = list[0].charAt(0) === '-' ? '-' : '';
let num = prefix ? list[0].slice(1) : list[0];
let result = '';
while (num.length > 3) {
result = `,${num.slice(-3)}${result}`;
num = num.slice(0, num.length - 3);
}
if (num) {
result = num + result;
}
return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
}
class NumericInput extends React.Component {
onChange = (e) => {
const { value } = e.target;
const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
if ((!isNaN(value) && reg.test(value)) || value === '' || value === '-') {
this.props.onChange(value);
}
}
// '.' at the end or only '-' in the input box.
onBlur = () => {
const { value, onBlur, onChange } = this.props;
if (value.charAt(value.length - 1) === '.' || value === '-') {
onChange({ value: value.slice(0, -1) });
}
if (onBlur) {
onBlur();
}
}
render() {
const { value } = this.props;
const title = value ? (
<span className="numeric-input-title">
{value !== '-' ? formatNumber(value) : '-'}
</span>
) : 'Input a number';
return (
<Tooltip
trigger={['focus']}
title={title}
placement="topLeft"
overlayClassName="numeric-input"
>
<Input
{...this.props}
onChange={this.onChange}
onBlur={this.onBlur}
placeholder="Input a number"
maxLength="25"
/>
</Tooltip>
);
}
}
class NumericInputDemo extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
}
onChange = (value) => {
this.setState({ value });
}
render() {
return <NumericInput style={{ width: 120 }} value={this.state.value} onChange={this.onChange} />;
}
}
ReactDOM.render(<NumericInputDemo />, mountNode);
/* to prevent the arrow overflow the popup container,
or the height is not enough when content is empty */
.numeric-input .ant-tooltip-inner {
min-width: 32px;
min-height: 37px;
}
.numeric-input .numeric-input-title {
font-size: 14px;
}
API#
Input#
Property | Description | Type | Default |
---|---|---|---|
addonAfter | The label text displayed after (on the right side of) the input field. | string|ReactNode | |
addonBefore | The label text displayed before (on the left side of) the input field. | string|ReactNode | |
defaultValue | The initial input content | string | |
disabled | Whether the input is disabled. | boolean | false |
id | The ID for input | string | |
prefix | The prefix icon for the Input. | string|ReactNode | |
size | The size of the input box. Note: in the context of a form, the large size is used. Available: large default small | string | default |
suffix | The suffix icon for the Input. | string|ReactNode | |
type | The type of input, text or textarea (type=textarea are deprecated after 2.12 , please use Input.TextArea ) | string | text |
value | The input content value | string | |
onPressEnter | The callback function that is triggered when Enter key is pressed. | function(e) |
When
Input
is used in aForm.Item
context, if theForm.Item
has theid
andoptions
props defined thenvalue
,defaultValue
, andid
props ofInput
are automatically set.
The rest of the props of Input are exactly the same as the original input.
Input.TextArea#
If you are using
antd@<2.12
, please useInput[type=textarea]
.
Property | Description | Type | Default |
---|---|---|---|
autosize | Height autosize feature, can be set to true|false or an object { minRows: 2, maxRows: 6 } | boolean|object | false |
defaultValue | The initial input content | string | |
value | The input content value | string | |
onPressEnter | The callback function that is triggered when Enter key is pressed. | function(e) |
The rest of the props of Input.TextArea
are the same as the original textarea.
Input.Search#
Added in 2.5.0
Property | Description | Type | Default |
---|---|---|---|
onSearch | The callback function that is triggered when you click on the search-icon or press Enter key. | function(value) |
Supports all props of Input
.
Input.Group#
Property | Description | Type | Default |
---|---|---|---|
compact | Whether use compact style | boolean | false |
size | The size of Input.Group specifies the size of the included Input fields. Available: large default small | string | default |
<Input.Group>
<Input />
<Input />
</Input.Group>