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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
| import React from 'react'; import { Icon, Form, Card, Button, Cascader, Select, Input, DatePicker } from 'antd';
import provinces from 'china-division/dist/provinces.json'; import cities from 'china-division/dist/cities.json'; import areas from 'china-division/dist/areas.json';
const { Option } = Select;
const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 4 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 20 } } }; const formItemLayoutWithOutLabel = { wrapperCol: { xs: { span: 24, offset: 0 }, sm: { span: 20, offset: 4 } } };
areas.forEach(area => { const matchCity = cities.filter(city => city.code === area.cityCode)[0]; if (matchCity) { matchCity.children = matchCity.children || []; matchCity.children.push({ label: area.name, value: area.code }); } });
cities.forEach(city => { const matchProvince = provinces.filter( province => province.code === city.provinceCode )[0]; if (matchProvince) { matchProvince.children = matchProvince.children || []; matchProvince.children.push({ label: city.name, value: city.code, children: city.children }); } });
const options = provinces.map(province => ({ label: province.name, value: province.code, children: province.children }));
let workUid = 0; let projectUid = 0;
@Form.create() export default class App extends React.Component { save = () => { const { validateFieldsAndScroll } = this.props.form; validateFieldsAndScroll((err, values) => { if (err) { return; } const body = JSON.stringify(values, null, 2); console.log(body); }); }; reset = () => { const { resetFields } = this.props.form; resetFields(); }; removeWorkExp = k => { const { form } = this.props; const keys = form.getFieldValue('workKeys'); form.setFieldsValue({ workKeys: keys.filter(key => key !== k) }); }; addWorkExp = () => { const { form } = this.props; const keys = form.getFieldValue('workKeys'); const nextKeys = keys.concat(workUid); workUid++; form.setFieldsValue({ workKeys: nextKeys }); }; removeProjectExp = k => { const { form } = this.props; const keys = form.getFieldValue('projectKeys'); form.setFieldsValue({ projectKeys: keys.filter(key => key !== k) }); }; addProjectExp = () => { const { form } = this.props; const keys = form.getFieldValue('projectKeys'); const nextKeys = keys.concat(projectUid); projectUid++; form.setFieldsValue({ projectKeys: nextKeys }); }; render() { const { getFieldDecorator, getFieldValue } = this.props.form; getFieldDecorator('workKeys', { initialValue: [] }); getFieldDecorator('projectKeys', { initialValue: [] });
const workKeys = getFieldValue('workKeys'); const projectKeys = getFieldValue('projectKeys'); const workItems = workKeys.map((k, index) => { return ( <Form.Item {...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)} label={index === 0 ? '公司名' : ''} required={false} key={k} > {getFieldDecorator(`work[${k}]`, { validateTrigger: ['onChange', 'onBlur'], rules: [ { required: true, whitespace: true, message: '请输入公司名' } ] })( <Input placeholder="公司名" style={{ width: '60%', marginRight: 8 }} /> )} {workKeys.length > 1 ? ( <Icon className="dynamic-delete-button" type="minus-circle-o" disabled={workKeys.length === 1} onClick={() => this.removeWorkExp(k)} /> ) : null} </Form.Item> ); }); const projectItems = projectKeys.map((k, index) => { return ( <Card key={k} extra={<a onClick={() => this.removeProjectExp(k)}>删除</a>} > <Form.Item {...formItemLayout} label="项目名称"> {getFieldDecorator(`projects[${k}].title`, { validateTrigger: ['onChange', 'onBlur'], rules: [ { required: true, whitespace: true, message: '请输入项目名称' } ] })( <Input placeholder="项目名称" style={{ width: '60%', marginRight: 8 }} /> )} </Form.Item> <Form.Item {...formItemLayout} label="项目类型"> {getFieldDecorator(`projects[${k}].type`, { validateTrigger: ['onChange', 'onBlur'], rules: [ { required: true, message: '请输入项目类型' } ] })( <Select style={{ width: '100%' }} placeholder="请选择项目类型(公司项目需要填写公司)" > <Option value={0}>个人项目</Option> <Option value={1}>公司项目</Option> </Select> )} {getFieldValue(`projects[${k}].type`) === 1 && getFieldDecorator(`projects[${k}].company`, { validateTrigger: ['onChange', 'onBlur'], rules: [ { required: true, message: '请输入公司名称' } ] })(<Input placeholder="请输入公司名称" />)} </Form.Item> </Card> ); }); return ( <div className="resume"> <Card title="基本信息" style={{ marginBottom: 10 }} className="resume__basic" > <div className="basic__content"> <Form.Item label="姓名"> {getFieldDecorator('name', { rules: [ { required: true, message: '请输入姓名' }, { max: 10, message: '长度不能超过 10 个字符' } ] })(<Input placeholder="请输入姓名" />)} </Form.Item> <Form.Item label="出生年月"> {getFieldDecorator('birthday', { rules: [ { required: true, message: '请选择出生年月' } ] })(<DatePicker placeholder="请选择出生年月" />)} </Form.Item> <Form.Item label="性别"> {getFieldDecorator('sex', { rules: [ { required: true, message: '请选择性别' } ] })( <Select style={{ width: '100%' }} placeholder="请选择性别"> <Option value={0}>男</Option> <Option value={1}>女</Option> </Select> )} </Form.Item> <Form.Item label="所在城市"> {getFieldDecorator('city', { rules: [ { required: true, message: '请选择所在城市' } ] })( <Cascader options={options} showSearch placeholder="请选择地址(支持搜索)" style={{ width: 400 }} /> )} </Form.Item> <Form.Item label="邮箱"> {getFieldDecorator('email', { rules: [ { required: true, message: '请输入邮箱' }, { type: 'email', message: '邮箱格式不正确' } ] })(<Input placeholder="请输入邮箱" />)} </Form.Item> </div> </Card> <Card title="工作经历" style={{ marginBottom: 10 }} className="resume__work" > {workItems} <Form.Item {...formItemLayoutWithOutLabel}> <Button type="dashed" onClick={this.addWorkExp} style={{ width: '60%' }} > <Icon type="plus" /> 添加 </Button> </Form.Item> </Card> <Card title="项目经历" style={{ marginBottom: 10 }} className="resume__education" > {projectItems} <Form.Item {...formItemLayoutWithOutLabel}> <Button type="dashed" onClick={this.addProjectExp} style={{ width: '60%' }} > <Icon type="plus" /> 添加 </Button> </Form.Item> </Card> <Button type="primary" onClick={this.save}> 保存 </Button> <Button onClick={this.reset}>重置</Button> </div> ); } }
|