Commit cc28e069 authored by WhatAKitty's avatar WhatAKitty Committed by ddcat1115

Do with new router (#519)

* Do with new router

* Move default values into form model
parent 8a15e9bd
...@@ -54,6 +54,9 @@ export const getRouterData = (app) => { ...@@ -54,6 +54,9 @@ export const getRouterData = (app) => {
'/form/step-form': { '/form/step-form': {
component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm')), 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': { '/form/step-form/confirm': {
component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step2')), component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step2')),
}, },
......
...@@ -7,6 +7,10 @@ export default { ...@@ -7,6 +7,10 @@ export default {
state: { state: {
step: { step: {
payAccount: 'ant-design@alipay.com',
receiverAccount: 'test@example.com',
receiverName: 'Alex',
amount: '500',
}, },
regularFormSubmitting: false, regularFormSubmitting: false,
stepFormSubmitting: false, stepFormSubmitting: false,
......
import React from 'react'; import React from 'react';
import { connect } from 'dva';
import { Form, Input, Button, Select, Divider } from 'antd'; import { Form, Input, Button, Select, Divider } from 'antd';
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import styles from './style.less'; import styles from './style.less';
const { Option } = Select; const { Option } = Select;
export default ({ formItemLayout, form, dispatch, data }) => { 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 { getFieldDecorator, validateFields } = form;
const onValidateForm = () => { const onValidateForm = () => {
validateFields((err, values) => { validateFields((err, values) => {
...@@ -26,7 +39,7 @@ export default ({ formItemLayout, form, dispatch, data }) => { ...@@ -26,7 +39,7 @@ export default ({ formItemLayout, form, dispatch, data }) => {
label="付款账户" label="付款账户"
> >
{getFieldDecorator('payAccount', { {getFieldDecorator('payAccount', {
initialValue: data.payAccount || 'ant-design@alipay.com', initialValue: data.payAccount,
rules: [{ required: true, message: '请选择付款账户' }], rules: [{ required: true, message: '请选择付款账户' }],
})( })(
<Select placeholder="test@example.com"> <Select placeholder="test@example.com">
...@@ -44,7 +57,7 @@ export default ({ formItemLayout, form, dispatch, data }) => { ...@@ -44,7 +57,7 @@ export default ({ formItemLayout, form, dispatch, data }) => {
<Option value="bank">银行账户</Option> <Option value="bank">银行账户</Option>
</Select> </Select>
{getFieldDecorator('receiverAccount', { {getFieldDecorator('receiverAccount', {
initialValue: data.receiverAccount || 'test@example.com', initialValue: data.receiverAccount,
rules: [ rules: [
{ required: true, message: '请输入收款人账户' }, { required: true, message: '请输入收款人账户' },
{ type: 'email', message: '账户名应为邮箱格式' }, { type: 'email', message: '账户名应为邮箱格式' },
...@@ -59,7 +72,7 @@ export default ({ formItemLayout, form, dispatch, data }) => { ...@@ -59,7 +72,7 @@ export default ({ formItemLayout, form, dispatch, data }) => {
label="收款人姓名" label="收款人姓名"
> >
{getFieldDecorator('receiverName', { {getFieldDecorator('receiverName', {
initialValue: data.receiverName || 'Alex', initialValue: data.receiverName,
rules: [{ required: true, message: '请输入收款人姓名' }], rules: [{ required: true, message: '请输入收款人姓名' }],
})( })(
<Input placeholder="请输入收款人姓名" /> <Input placeholder="请输入收款人姓名" />
...@@ -70,7 +83,7 @@ export default ({ formItemLayout, form, dispatch, data }) => { ...@@ -70,7 +83,7 @@ export default ({ formItemLayout, form, dispatch, data }) => {
label="转账金额" label="转账金额"
> >
{getFieldDecorator('amount', { {getFieldDecorator('amount', {
initialValue: data.amount || '500', initialValue: data.amount,
rules: [ rules: [
{ required: true, message: '请输入转账金额' }, { required: true, message: '请输入转账金额' },
{ pattern: /^(\d+)((?:\.\d+)?)$/, message: '请输入合法金额数字' }, { pattern: /^(\d+)((?:\.\d+)?)$/, message: '请输入合法金额数字' },
...@@ -101,4 +114,9 @@ export default ({ formItemLayout, form, dispatch, data }) => { ...@@ -101,4 +114,9 @@ export default ({ formItemLayout, form, dispatch, data }) => {
</div> </div>
</div> </div>
); );
}; }
}
export default connect(({ form }) => ({
data: form.step,
}))(Step1);
import React from 'react'; import React from 'react';
import { connect } from 'dva';
import { Form, Input, Button, Alert, Divider } from 'antd'; import { Form, Input, Button, Alert, Divider } from 'antd';
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import { digitUppercase } from '../../../utils/utils'; import { digitUppercase } from '../../../utils/utils';
import styles from './style.less'; import styles from './style.less';
export default ({ formItemLayout, form, data, dispatch, submitting }) => { 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 { getFieldDecorator, validateFields } = form;
const onPrev = () => { const onPrev = () => {
dispatch(routerRedux.push('/form/step-form')); dispatch(routerRedux.push('/form/step-form'));
...@@ -92,4 +105,10 @@ export default ({ formItemLayout, form, data, dispatch, submitting }) => { ...@@ -92,4 +105,10 @@ export default ({ formItemLayout, form, data, dispatch, submitting }) => {
</Form.Item> </Form.Item>
</Form> </Form>
); );
}; }
}
export default connect(({ form }) => ({
submitting: form.stepFormSubmitting,
data: form.step,
}))(Step2);
import React from 'react'; import React from 'react';
import { connect } from 'dva';
import { Button, Row, Col } from 'antd'; import { Button, Row, Col } from 'antd';
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import Result from '../../../components/Result'; import Result from '../../../components/Result';
import styles from './style.less'; import styles from './style.less';
export default ({ dispatch, data }) => { class Step3 extends React.PureComponent {
render() {
const { dispatch, data } = this.props;
const onFinish = () => { const onFinish = () => {
dispatch(routerRedux.push('/form/step-form')); dispatch(routerRedux.push('/form/step-form'));
}; };
...@@ -48,4 +51,9 @@ export default ({ dispatch, data }) => { ...@@ -48,4 +51,9 @@ export default ({ dispatch, data }) => {
className={styles.result} className={styles.result}
/> />
); );
}; }
}
export default connect(({ form }) => ({
data: form.step,
}))(Step3);
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { connect } from 'dva'; import { Route, Redirect, Switch } from 'dva/router';
import { Card, Steps, Form } from 'antd'; import { Card, Steps } from 'antd';
import PageHeaderLayout from '../../../layouts/PageHeaderLayout'; import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
import Step1 from './Step1'; import NotFound from '../../Exception/404';
import Step2 from './Step2'; import { getRoutes } from '../../../utils/utils';
import Step3 from './Step3';
import styles from '../style.less'; import styles from '../style.less';
const { Step } = Steps; const { Step } = Steps;
@Form.create() export default class StepForm extends PureComponent {
class StepForm extends PureComponent {
getCurrentStep() { getCurrentStep() {
const { location } = this.props; const { location } = this.props;
const { pathname } = location; const { pathname } = location;
const pathList = pathname.split('/'); const pathList = pathname.split('/');
switch (pathList[pathList.length - 1]) { switch (pathList[pathList.length - 1]) {
case 'step-form': return 0; case 'info': return 0;
case 'confirm': return 1; case 'confirm': return 1;
case 'result': return 2; case 'result': return 2;
default: return 0; default: return 0;
} }
} }
getCurrentComponent() {
const componentMap = {
0: Step1,
1: Step2,
2: Step3,
};
return componentMap[this.getCurrentStep()];
}
render() { render() {
const { form, stepFormData, submitting, dispatch } = this.props; const { match, routerData } = this.props;
const formItemLayout = {
labelCol: {
span: 5,
},
wrapperCol: {
span: 19,
},
};
const CurrentComponent = this.getCurrentComponent();
return ( return (
<PageHeaderLayout title="分步表单" content="将一个冗长或用户不熟悉的表单任务分成多个步骤,指导用户完成。"> <PageHeaderLayout title="分步表单" content="将一个冗长或用户不熟悉的表单任务分成多个步骤,指导用户完成。">
<Card bordered={false}> <Card bordered={false}>
...@@ -50,21 +31,23 @@ class StepForm extends PureComponent { ...@@ -50,21 +31,23 @@ class StepForm extends PureComponent {
<Step title="确认转账信息" /> <Step title="确认转账信息" />
<Step title="完成" /> <Step title="完成" />
</Steps> </Steps>
<CurrentComponent <Switch>
formItemLayout={formItemLayout} {
form={form} getRoutes(match.path, routerData).map(item => (
dispatch={dispatch} <Route
data={stepFormData} key={item.key}
submitting={submitting} path={item.path}
component={item.component}
exact={item.exact}
/> />
))
}
<Redirect exact from="/form/step-form" to="/form/step-form/info" />
<Route render={NotFound} />
</Switch>
</div> </div>
</Card> </Card>
</PageHeaderLayout> </PageHeaderLayout>
); );
} }
} }
export default connect(state => ({
stepFormData: state.form.step,
submitting: state.form.stepFormSubmitting,
}))(StepForm);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment