## 新增页面 1. 我们到Demo文件夹下新建文件DemoAdd.js ~~~ import React, { PureComponent } from 'react'; import { Form, Input, Card, Button, DatePicker } from 'antd'; import moment from 'moment'; import Panel from '../../../components/Panel'; import styles from '../../../layouts/Sword.less'; const FormItem = Form.Item; const { TextArea } = Input; @Form.create() class DemoAdd extends PureComponent { render() { const { form: { getFieldDecorator }, } = this.props; const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 7 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 12 }, md: { span: 10 }, }, }; const action = ( <Button type="primary"> 提交 </Button> ); return ( <Panel title="新增" back="platform/demo" action={action}> <Form hideRequiredMark style={{ marginTop: 8 }}> <Card title="基本信息" className={styles.card} bordered={false}> <FormItem {...formItemLayout} label="标题"> {getFieldDecorator('title')(<Input placeholder="请输入标题" />)} </FormItem> <FormItem {...formItemLayout} label="时间"> {getFieldDecorator('date')( <DatePicker style={{ width: '100%' }} format="YYYY-MM-DD HH:mm:ss" showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }} /> )} </FormItem> <FormItem {...formItemLayout} label="内容"> {getFieldDecorator('content')( <TextArea style={{ minHeight: 32 }} placeholder="请输入内容" rows={10} /> )} </FormItem> </Card> </Form> </Panel> ); } } export default DemoAdd; ~~~ 2. 主要需要注意的是引入了form中的`getFieldDecorator`,用于进行表单操作、数据绑定、数据获取等功能 3. 当写入组件的时候,需要被`getFieldDecorator`包装起来,具体语法如下 ~~~ {getFieldDecorator('title')(<Input placeholder="请输入标题" />)} ~~~ 4. 这样生成了组件后,他的值就会和对于设定的字段进行绑定,从而对其进行操作 5. 页面写好后,我们到路由配置文件`router.config.js`增加路径 ![](https://box.kancloud.cn/9779af91e7701576617514d3e735c9bb_1672x748.png) 6. 打开系统点击新增按钮后发现页面跳转成功 ![](https://box.kancloud.cn/4653f612532aa710a8b8ff19a5350697_3358x1466.png) ## 表单提交 1. 页面构建成功,接下来我们需要的就是提交表单的数据 2. 增加按钮点击事件 ~~~ handleSubmit = e => { e.preventDefault(); const { form } = this.props; form.validateFieldsAndScroll((err, values) => { if (!err) { console.log(values) } }); }; ~~~ ~~~ const action = ( <Button type="primary" onClick={this.handleSubmit}> 提交 </Button> ); ~~~ 3. 打开系统控制台,输入一些数据提交,查看打印正确 ![](https://box.kancloud.cn/273dd21e65b6a0fb8263d864ed6890d8_3358x1410.png) ## 表单校验 1. 为了保证业务数据的准确性,表单提交前需要做数据校验 2. 我们先做一个最简单的非空校验,修改如下代码 将原先的 ~~~ {getFieldDecorator('title')(<Input placeholder="请输入标题" />)} ~~~ 修改为 ~~~ {getFieldDecorator('title', { rules: [ { required: true, message: '请输入标题', }, ], })(<Input placeholder="请输入标题" />)} ~~~ 3. 可以看到,在getFieldDecorator方法的入参中加入了一个json对象,值是rules,对应着校验规则(更多内容请看官方文档:https://ant.design/components/form-cn/) ~~~ { rules: [ { required: true, message: '请输入标题', }, ], } ~~~ 4. 打开系统,不输入日期,点击提交发现提示,校验成功 ![](https://box.kancloud.cn/dde15745ac4bc64423ce70a4234580aa_3358x1244.png) ## 对接接口 1. 到mockjs中新建api提交的接口,增加fakeSuccess返回 ~~~ function fakeSuccess(req, res) { const json = { code: 200, success: true, msg: '操作成功' }; return res.json(json); } const proxy = { 'GET /api/demo/list': getFakeList, 'GET /api/demo/detail': getFakeDetail, 'POST /api/demo/submit': fakeSuccess, 'POST /api/demo/remove': fakeSuccess, }; export default delay(proxy, 1000); ~~~ 2. 优化handelSubmit方法,增加submit方法传入表单数据values供api调用 ~~~ handleSubmit = e => { e.preventDefault(); const { form } = this.props; form.validateFieldsAndScroll((err, values) => { if (!err) { submit(values).then(resp => { if (resp.success) { message.success('提交成功'); router.push('/platform/demo'); } else { message.warn(resp.msg); } }); } }); }; ~~~ 3. 打开系统点击提交发现提交成功,network监听传递参数无误 ![](https://box.kancloud.cn/fc52f66109c58093ca46341bb23d8628_2564x1494.png) 4. 但是有一点需要注意的是,传递过去的日期类型并非标准的`YYYY-MM-DD HH:mm:ss`,所以需要再次优化下提交代码 ~~~ handleSubmit = e => { e.preventDefault(); const { form } = this.props; form.validateFieldsAndScroll((err, values) => { if (!err) { const params = { ...values, date: func.format(values.date), }; submit(params).then(resp => { if (resp.success) { message.success('提交成功'); router.push('/platform/demo'); } else { message.warn(resp.msg); } }); } }); }; ~~~ 5. 再次提交查看参数传递给接口的格式已经正确 ![](https://box.kancloud.cn/e0a5d89b0dbe1fe33e17e6411a9a98e9_1094x206.png) 6. 最后以下附上完整代码 ~~~ import React, { PureComponent } from 'react'; import router from 'umi/router'; import { Form, Input, Card, Button, message, DatePicker } from 'antd'; import moment from 'moment'; import Panel from '../../../components/Panel'; import styles from '../../../layouts/Sword.less'; import { submit } from '../../../services/demo'; import func from '../../../utils/Func'; const FormItem = Form.Item; const { TextArea } = Input; @Form.create() class DemoAdd extends PureComponent { handleSubmit = e => { e.preventDefault(); const { form } = this.props; form.validateFieldsAndScroll((err, values) => { if (!err) { const params = { ...values, date: func.format(values.date), }; submit(params).then(resp => { if (resp.success) { message.success('提交成功'); router.push('/platform/demo'); } else { message.warn(resp.msg); } }); } }); }; render() { const { form: { getFieldDecorator }, } = this.props; const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 7 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 12 }, md: { span: 10 }, }, }; const action = ( <Button type="primary" onClick={this.handleSubmit}> 提交 </Button> ); return ( <Panel title="新增" back="/platform/demo" action={action}> <Form hideRequiredMark style={{ marginTop: 8 }}> <Card title="基本信息" className={styles.card} bordered={false}> <FormItem {...formItemLayout} label="标题"> {getFieldDecorator('title', { rules: [ { required: true, message: '请输入标题', }, ], })(<Input placeholder="请输入标题" />)} </FormItem> <FormItem {...formItemLayout} label="日期"> {getFieldDecorator('date', { rules: [ { required: true, message: '请输入日期', }, ], })( <DatePicker style={{ width: '100%' }} format="YYYY-MM-DD HH:mm:ss" showTime={{ defaultValue: moment('00:00:00', 'HH:mm:ss') }} /> )} </FormItem> <FormItem {...formItemLayout} label="内容"> {getFieldDecorator('content')( <TextArea style={{ minHeight: 32 }} placeholder="请输入内容" rows={10} /> )} </FormItem> </Card> </Form> </Panel> ); } } export default DemoAdd; ~~~