From cc28e0695eba93f9b838a4223870c4dc1aac6552 Mon Sep 17 00:00:00 2001
From: WhatAKitty <104xuqiang@163.com>
Date: Fri, 29 Dec 2017 13:44:39 +0800
Subject: [PATCH] Do with new router (#519)
* Do with new router
* Move default values into form model
---
src/common/router.js | 3 +
src/models/form.js | 4 +
src/routes/Forms/StepForm/Step1.js | 202 ++++++++++++++++-------------
src/routes/Forms/StepForm/Step2.js | 195 +++++++++++++++-------------
src/routes/Forms/StepForm/Step3.js | 98 +++++++-------
src/routes/Forms/StepForm/index.js | 59 +++------
6 files changed, 298 insertions(+), 263 deletions(-)
diff --git a/src/common/router.js b/src/common/router.js
index 3eac195d..343d98ad 100644
--- a/src/common/router.js
+++ b/src/common/router.js
@@ -54,6 +54,9 @@ export const getRouterData = (app) => {
'/form/step-form': {
component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm')),
},
+ '/form/step-form/info': {
+ component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step1')),
+ },
'/form/step-form/confirm': {
component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step2')),
},
diff --git a/src/models/form.js b/src/models/form.js
index a3a144a2..afae2044 100644
--- a/src/models/form.js
+++ b/src/models/form.js
@@ -7,6 +7,10 @@ export default {
state: {
step: {
+ payAccount: 'ant-design@alipay.com',
+ receiverAccount: 'test@example.com',
+ receiverName: 'Alex',
+ amount: '500',
},
regularFormSubmitting: false,
stepFormSubmitting: false,
diff --git a/src/routes/Forms/StepForm/Step1.js b/src/routes/Forms/StepForm/Step1.js
index 20379b96..2a962fbc 100644
--- a/src/routes/Forms/StepForm/Step1.js
+++ b/src/routes/Forms/StepForm/Step1.js
@@ -1,104 +1,122 @@
import React from 'react';
+import { connect } from 'dva';
import { Form, Input, Button, Select, Divider } from 'antd';
import { routerRedux } from 'dva/router';
import styles from './style.less';
const { Option } = Select;
-export default ({ formItemLayout, form, dispatch, data }) => {
- const { getFieldDecorator, validateFields } = form;
- const onValidateForm = () => {
- validateFields((err, values) => {
- if (!err) {
- dispatch({
- type: 'form/saveStepFormData',
- payload: values,
- });
- dispatch(routerRedux.push('/form/step-form/confirm'));
- }
- });
- };
- return (
-
-
- {getFieldDecorator('payAccount', {
- initialValue: data.payAccount || 'ant-design@alipay.com',
- rules: [{ required: true, message: '请选择付款账户' }],
- })(
-
- )}
-
-
-
-
- {getFieldDecorator('receiverAccount', {
- initialValue: data.receiverAccount || 'test@example.com',
+const formItemLayout = {
+ labelCol: {
+ span: 5,
+ },
+ wrapperCol: {
+ span: 19,
+ },
+};
+
+@Form.create()
+class Step1 extends React.PureComponent {
+ render() {
+ const { form, dispatch, data } = this.props;
+ const { getFieldDecorator, validateFields } = form;
+ const onValidateForm = () => {
+ validateFields((err, values) => {
+ if (!err) {
+ dispatch({
+ type: 'form/saveStepFormData',
+ payload: values,
+ });
+ dispatch(routerRedux.push('/form/step-form/confirm'));
+ }
+ });
+ };
+ return (
+
+
+ {getFieldDecorator('payAccount', {
+ initialValue: data.payAccount,
+ rules: [{ required: true, message: '请选择付款账户' }],
+ })(
+
+ )}
+
+
+
+
+ {getFieldDecorator('receiverAccount', {
+ initialValue: data.receiverAccount,
+ rules: [
+ { required: true, message: '请输入收款人账户' },
+ { type: 'email', message: '账户名应为邮箱格式' },
+ ],
+ })(
+
+ )}
+
+
+
+ {getFieldDecorator('receiverName', {
+ initialValue: data.receiverName,
+ rules: [{ required: true, message: '请输入收款人姓名' }],
+ })(
+
+ )}
+
+
+ {getFieldDecorator('amount', {
+ initialValue: data.amount,
rules: [
- { required: true, message: '请输入收款人账户' },
- { type: 'email', message: '账户名应为邮箱格式' },
+ { required: true, message: '请输入转账金额' },
+ { pattern: /^(\d+)((?:\.\d+)?)$/, message: '请输入合法金额数字' },
],
})(
-
+
)}
-
-
-
- {getFieldDecorator('receiverName', {
- initialValue: data.receiverName || 'Alex',
- rules: [{ required: true, message: '请输入收款人姓名' }],
- })(
-
- )}
-
-
- {getFieldDecorator('amount', {
- initialValue: data.amount || '500',
- rules: [
- { required: true, message: '请输入转账金额' },
- { pattern: /^(\d+)((?:\.\d+)?)$/, message: '请输入合法金额数字' },
- ],
- })(
-
- )}
-
-
-
-
-
-
-
-
说明
-
转账到支付宝账户
-
如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。
-
转账到银行卡
-
如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。
+
+
+
+
+
+
+
+
说明
+
转账到支付宝账户
+
如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。
+
转账到银行卡
+
如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。如果需要,这里可以放一些关于产品的常见问题说明。
+
-
- );
-};
+ );
+ }
+}
+
+export default connect(({ form }) => ({
+ data: form.step,
+}))(Step1);
diff --git a/src/routes/Forms/StepForm/Step2.js b/src/routes/Forms/StepForm/Step2.js
index 89da4a29..19f60523 100644
--- a/src/routes/Forms/StepForm/Step2.js
+++ b/src/routes/Forms/StepForm/Step2.js
@@ -1,95 +1,114 @@
import React from 'react';
+import { connect } from 'dva';
import { Form, Input, Button, Alert, Divider } from 'antd';
import { routerRedux } from 'dva/router';
import { digitUppercase } from '../../../utils/utils';
import styles from './style.less';
-export default ({ formItemLayout, form, data, dispatch, submitting }) => {
- const { getFieldDecorator, validateFields } = form;
- const onPrev = () => {
- dispatch(routerRedux.push('/form/step-form'));
- };
- const onValidateForm = (e) => {
- e.preventDefault();
- validateFields((err, values) => {
- if (!err) {
- dispatch({
- type: 'form/submitStepForm',
- payload: {
- ...data,
- ...values,
- },
- });
- }
- });
- };
- return (
-
- {data.payAccount}
-
-
- {data.receiverAccount}
-
-
- {data.receiverName}
-
-
- {data.amount}
- ({digitUppercase(data.amount)})
-
-
-
- {getFieldDecorator('password', {
- initialValue: '123456',
- rules: [{
- required: true, message: '需要支付密码才能进行支付',
- }],
- })(
-
- )}
-
-
-
-
-
-
- );
+const formItemLayout = {
+ labelCol: {
+ span: 5,
+ },
+ wrapperCol: {
+ span: 19,
+ },
};
+
+@Form.create()
+class Step2 extends React.PureComponent {
+ render() {
+ const { form, data, dispatch, submitting } = this.props;
+ const { getFieldDecorator, validateFields } = form;
+ const onPrev = () => {
+ dispatch(routerRedux.push('/form/step-form'));
+ };
+ const onValidateForm = (e) => {
+ e.preventDefault();
+ validateFields((err, values) => {
+ if (!err) {
+ dispatch({
+ type: 'form/submitStepForm',
+ payload: {
+ ...data,
+ ...values,
+ },
+ });
+ }
+ });
+ };
+ return (
+
+ {data.payAccount}
+
+
+ {data.receiverAccount}
+
+
+ {data.receiverName}
+
+
+ {data.amount}
+ ({digitUppercase(data.amount)})
+
+
+
+ {getFieldDecorator('password', {
+ initialValue: '123456',
+ rules: [{
+ required: true, message: '需要支付密码才能进行支付',
+ }],
+ })(
+
+ )}
+
+
+
+
+
+
+ );
+ }
+}
+
+export default connect(({ form }) => ({
+ submitting: form.stepFormSubmitting,
+ data: form.step,
+}))(Step2);
diff --git a/src/routes/Forms/StepForm/Step3.js b/src/routes/Forms/StepForm/Step3.js
index 2de4c3a2..78295ae4 100644
--- a/src/routes/Forms/StepForm/Step3.js
+++ b/src/routes/Forms/StepForm/Step3.js
@@ -1,51 +1,59 @@
import React from 'react';
+import { connect } from 'dva';
import { Button, Row, Col } from 'antd';
import { routerRedux } from 'dva/router';
import Result from '../../../components/Result';
import styles from './style.less';
-export default ({ dispatch, data }) => {
- const onFinish = () => {
- dispatch(routerRedux.push('/form/step-form'));
- };
- const information = (
-
-
- 付款账户:
- {data.payAccount}
-
-
- 收款账户:
- {data.receiverAccount}
-
-
- 收款人姓名:
- {data.receiverName}
-
-
- 转账金额:
- {data.amount} 元
-
-
- );
- const actions = (
-
-
-
-
- );
- return (
-
- );
-};
+class Step3 extends React.PureComponent {
+ render() {
+ const { dispatch, data } = this.props;
+ const onFinish = () => {
+ dispatch(routerRedux.push('/form/step-form'));
+ };
+ const information = (
+
+
+ 付款账户:
+ {data.payAccount}
+
+
+ 收款账户:
+ {data.receiverAccount}
+
+
+ 收款人姓名:
+ {data.receiverName}
+
+
+ 转账金额:
+ {data.amount} 元
+
+
+ );
+ const actions = (
+
+
+
+
+ );
+ return (
+
+ );
+ }
+}
+
+export default connect(({ form }) => ({
+ data: form.step,
+}))(Step3);
diff --git a/src/routes/Forms/StepForm/index.js b/src/routes/Forms/StepForm/index.js
index a6d4ca8d..de9779b9 100644
--- a/src/routes/Forms/StepForm/index.js
+++ b/src/routes/Forms/StepForm/index.js
@@ -1,46 +1,27 @@
import React, { PureComponent } from 'react';
-import { connect } from 'dva';
-import { Card, Steps, Form } from 'antd';
+import { Route, Redirect, Switch } from 'dva/router';
+import { Card, Steps } from 'antd';
import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
-import Step1 from './Step1';
-import Step2 from './Step2';
-import Step3 from './Step3';
+import NotFound from '../../Exception/404';
+import { getRoutes } from '../../../utils/utils';
import styles from '../style.less';
const { Step } = Steps;
-@Form.create()
-class StepForm extends PureComponent {
+export default class StepForm extends PureComponent {
getCurrentStep() {
const { location } = this.props;
const { pathname } = location;
const pathList = pathname.split('/');
switch (pathList[pathList.length - 1]) {
- case 'step-form': return 0;
+ case 'info': return 0;
case 'confirm': return 1;
case 'result': return 2;
default: return 0;
}
}
- getCurrentComponent() {
- const componentMap = {
- 0: Step1,
- 1: Step2,
- 2: Step3,
- };
- return componentMap[this.getCurrentStep()];
- }
render() {
- const { form, stepFormData, submitting, dispatch } = this.props;
- const formItemLayout = {
- labelCol: {
- span: 5,
- },
- wrapperCol: {
- span: 19,
- },
- };
- const CurrentComponent = this.getCurrentComponent();
+ const { match, routerData } = this.props;
return (
@@ -50,21 +31,23 @@ class StepForm extends PureComponent {
-
+
+ {
+ getRoutes(match.path, routerData).map(item => (
+
+ ))
+ }
+
+
+
);
}
}
-
-export default connect(state => ({
- stepFormData: state.form.step,
- submitting: state.form.stepFormSubmitting,
-}))(StepForm);
--
GitLab