This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tsx
185 lines (163 loc) · 5.55 KB
/
main.tsx
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import {
ConfigProvider, Form, Row, Col,
Input, Button, Icon, Card,
Tooltip, DatePicker, Radio, message
} from 'antd';
import { FormComponentProps } from 'antd/lib/form';
import zh_CN from 'antd/lib/locale-provider/zh_CN';
import moment from 'moment';
import 'moment/locale/zh-cn';
import { createFormArray } from '../../index';
import 'antd/dist/antd.css';
const GENDER = {
MALE: 1,
FEMALE: 2
}
const DEFAULT_BIRTH_YEAR = moment().subtract(6, 'y'); // 默认的出生年份
const formLayout = {
labelCol: {
sm: 24, xl: 6
},
wrapperCol: {
sm: 24, xl: 18
}
}
class Student {
name: string;
birthday: string;
gender: number;
}
const mockStudents: Student[] = [
{ name: '小明', birthday: '2013-01-01', gender: GENDER.MALE },
{ name: '小红', birthday: '2014-02-02', gender: GENDER.FEMALE },
{ name: '小强', birthday: '2013-10-13', gender: GENDER.MALE }
];
class FormDemo extends PureComponent<FormComponentProps> {
state = {
students: createFormArray(mockStudents),
submitData: {}
}
handleSave = () => {
const { form: { validateFieldsAndScroll } } = this.props;
validateFieldsAndScroll((err, value) => {
if (err) { return message.warn('请检查表单中的错误'); }
const submitData = {
...value,
students: value.students.filter((v: any) => v).map((item: any) => {
return {
...item,
birthday: item.birthday && item.birthday.format('YYYY-MM-DD')
}
})
}
console.log(submitData);
this.setState({ submitData });
});
}
render() {
const { students, submitData } = this.state;
const { form: { getFieldDecorator, getFieldsValue } } = this.props;
return (
<Row>
<Col span={12}>
<Card title='学生列表'>
<Form>
{
students.render((student, key) => {
return (
<Row key={key} gutter={20}>
<Col span={8}>
<Form.Item label='姓名' {...formLayout}>
{
getFieldDecorator(`students[${key}].name`, {
initialValue: student.name,
rules: [
{ required: true, whitespace: true, message: '请填写姓名' },
{ pattern: /^[\u2E80-\u9FFF·\s]+$/, message: '姓名不合法' }
]
})(<Input placeholder='请填写姓名' />)
}
</Form.Item>
</Col>
<Col span={8}>
<Form.Item label='出生日期' {...formLayout}>
{
getFieldDecorator(`students[${key}].birthday`, {
initialValue: student.birthday ? moment(student.birthday) : undefined
})(
<DatePicker defaultPickerValue={DEFAULT_BIRTH_YEAR} />
)
}
</Form.Item>
</Col>
<Col span={6}>
<Form.Item label='性别' {...formLayout}>
{
getFieldDecorator(`students[${key}].gender`, {
initialValue: student.gender || 1
})(
<Radio.Group>
<Radio value={GENDER.MALE}>男</Radio>
<Radio value={GENDER.FEMALE}>女</Radio>
</Radio.Group>
)
}
</Form.Item>
</Col>
<Col span={2}>
{
students.length > 1 &&
<Tooltip title='删除'>
<Icon type='delete'
onClick={() => {
this.setState({
students: students.delete(key)
});
}}
style={{ marginTop: 12 }} />
</Tooltip>
}
</Col>
</Row>
)
})
}
</Form>
<Button type='link'
onClick={() => {
this.setState({
students: students.add(new Student())
})
}}>添加学生</Button>
<div style={{ marginTop: 20, textAlign: 'center' }}>
<Button type='primary' onClick={this.handleSave}>保存</Button>
</div>
</Card>
</Col>
<Col span={6}>
<Card title='表单数据'>
<pre>
{JSON.stringify(getFieldsValue(), null, ' ')}
</pre>
</Card>
</Col>
<Col span={6}>
<Card title='提交数据' extra='点击保存时提交的数据'>
<pre>
{JSON.stringify(submitData, null, ' ')}
</pre>
</Card>
</Col>
</Row>
)
}
}
const DemoComponent = Form.create()(FormDemo);
render(
<ConfigProvider locale={zh_CN}>
<DemoComponent />
</ConfigProvider>,
document.getElementById('root')
);