Ant Design Examples

  1. Form - complex form controls
    1. Complex form control
    2. InputNumber Component

Form - complex form controls

Complex form control

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Form, Input, Select, Tooltip, Button, Space, Typography } from 'antd';

const { Option } = Select;

const Demo = () => {
const onFinish = values => {
console.log('Received values of form: ', values);
};

return (
<Form name="complex-form" onFinish={onFinish} labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>
<Form.Item label="Username">
<Space>
<Form.Item
name="username"
noStyle
rules={[{ required: true, message: 'Username is required' }]}
>
<Input style={{ width: 160 }} placeholder="Please input" />
</Form.Item>
<Tooltip title="Useful information">
<Typography.Link href="#API">Need Help?</Typography.Link>
</Tooltip>
</Space>
</Form.Item>
<Form.Item label="Address">
<Input.Group compact>
<Form.Item
name={['address', 'province']}
noStyle
rules={[{ required: true, message: 'Province is required' }]}
>
<Select placeholder="Select province">
<Option value="Zhejiang">Zhejiang</Option>
<Option value="Jiangsu">Jiangsu</Option>
</Select>
</Form.Item>
<Form.Item
name={['address', 'street']}
noStyle
rules={[{ required: true, message: 'Street is required' }]}
>
<Input style={{ width: '50%' }} placeholder="Input street" />
</Form.Item>
</Input.Group>
</Form.Item>
<Form.Item label="BirthDate" style={{ marginBottom: 0 }}>
<Form.Item
name="year"
rules={[{ required: true }]}
style={{ display: 'inline-block', width: 'calc(50% - 8px)' }}
>
<Input placeholder="Input birth year" />
</Form.Item>
<Form.Item
name="month"
rules={[{ required: true }]}
style={{ display: 'inline-block', width: 'calc(50% - 8px)', margin: '0 8px' }}
>
<Input placeholder="Input birth month" />
</Form.Item>
</Form.Item>
<Form.Item label=" " colon={false}>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};

ReactDOM.render(<Demo />, mountNode);

This demo shows how to use Form.Item with multiple controls. <Form.Item name="field" /> will only bind the control(Input/Select) which is the only children of it. Imagine this case: you added some text description after the Input, then you have to wrap the Input by an extra <Form.Item name="field">. style property of Form.Item could be useful to modify the nested form item layout, or use <Form.Item noStyle /> to turn it into a pure form-binded component.

1
2
3
4
5
6
7
- <Form.Item label="Field" name="field">
- <Input />
- </Form.Item>
+ <Form.Item label="Field">
+ <Form.Item name="field" noStyle><Input /></Form.Item> // that will bind input
+ <span>description</span>
+ </Form.Item>

If you don’t follow the instruction, the following message will be printed on the console:

1
[antd: Form.Item] `children` is array of render props cannot have `name`.

InputNumber Component

  1. Basic - Positive integer only
1
<InputNumber min={1} precision={0} />
  • min - Set the min value to 1
  • precision - Set precision to 0 to allow positive integers only.
  1. Post tab - percent with two decimals
1
<InputNumber addonAfter="%" precision={2} min={0.01} />
  • addonAfter - The label text displayed after the input control
  • precision - Setting this prop to 2 means two numbers allowed after the dot.
  • min - The minimum number is set to 0.01

Note: The addonAfter prop is only supported since antd 4.17.0. This issue has been discussed here. If you are using an older version, you would use it as follows:

1
2
3
4
5
6
7
<InputNumber
precision={2}
min={0.01}
max={100}
formatter={value => `${value}%`}
parser={value => value.replace('%', '')}
/>