Skip to content

Commit 7a6acc3

Browse files
Got wizard working with Redux forms
1 parent 1fa1e39 commit 7a6acc3

File tree

6 files changed

+179
-10
lines changed

6 files changed

+179
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"classnames": "^2.2.6",
77
"history": "^4.7.2",
88
"localStorage": "^1.0.4",
9+
"lodash": "^4.17.11",
910
"react": "^16.8.4",
1011
"react-dom": "^16.8.4",
1112
"react-redux": "^6.0.1",

src/modules/wizard/components/Wizard.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ const CancelButton = ({ handleCancel }) => (
5656
const WizardBreadCrub = ({ pages, currentStep }) => (
5757
<ul className="wizard">
5858
{pages.map((page, index) => (
59-
<Step stepNumber={index + 1} currentStep={currentStep} page={page} />
59+
<Step
60+
key={index}
61+
stepNumber={index + 1}
62+
currentStep={currentStep}
63+
page={page}
64+
/>
6065
))}
6166
</ul>
6267
);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from "react";
2+
import { reduxForm, Field } from "redux-form";
3+
import classNames from "classnames";
4+
import "./wizardExample.css";
5+
import wizard from "../../../modules/wizard";
6+
7+
const {
8+
components: { ValidatingReduxFormWizardPage }
9+
} = wizard;
10+
11+
const required = value => {
12+
return (!isNaN(value) && +value) || (isNaN(value) && value !== undefined)
13+
? undefined
14+
: "Required";
15+
};
16+
17+
const ValidatingInputField = ({
18+
input,
19+
label,
20+
type,
21+
meta: { touched, error, warning }
22+
}) => (
23+
<div>
24+
<label>{label}</label>
25+
<div>
26+
<input
27+
className={classNames({ error: touched && error })}
28+
{...input}
29+
placeholder={label}
30+
type={type}
31+
/>
32+
{touched &&
33+
((error && <span className="error">{error}</span>) ||
34+
(warning && <span>{warning}</span>))}
35+
</div>
36+
</div>
37+
);
38+
39+
let PageWithReduxFormsValidations = ({ onSubmit, handleSubmit, ...rest }) => (
40+
<ValidatingReduxFormWizardPage {...rest}>
41+
<form onSubmit={handleSubmit(onSubmit)}>
42+
<Field
43+
component={ValidatingInputField}
44+
type="number"
45+
name="requiredNumber"
46+
validate={[required]}
47+
label="Required Number"
48+
/>
49+
</form>
50+
</ValidatingReduxFormWizardPage>
51+
);
52+
53+
PageWithReduxFormsValidations = reduxForm({
54+
form: "PageWithReduxFormsValidations"
55+
})(PageWithReduxFormsValidations);
56+
57+
export default class PageWithReduxFormsValidationsContainer extends React.Component {
58+
handleSubmit(values) {
59+
alert(`Submit succeeded with values: ${JSON.stringify(values)}`);
60+
61+
return Promise.resolve("Success!");
62+
}
63+
64+
render() {
65+
return (
66+
<PageWithReduxFormsValidations
67+
onSubmit={this.handleSubmit}
68+
{...this.props}
69+
/>
70+
);
71+
}
72+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React from "react";
2+
import { reduxForm, Field, SubmissionError } from "redux-form";
3+
import classNames from "classnames";
4+
import _ from "lodash";
5+
import "./wizardExample.css";
6+
import { getState } from "../../../modules/store";
7+
import wizard from "../../../modules/wizard";
8+
9+
const {
10+
selectors: { getWizardErrors }
11+
} = wizard;
12+
13+
const {
14+
components: { ValidatingReduxFormWizardPage }
15+
} = wizard;
16+
17+
const required = value => {
18+
return (!isNaN(value) && +value) || (isNaN(value) && value !== undefined)
19+
? undefined
20+
: "Required";
21+
};
22+
23+
const ValidatingInputField = ({
24+
input,
25+
label,
26+
type,
27+
meta: { touched, error, warning }
28+
}) => (
29+
<div>
30+
<label>{label}</label>
31+
<div>
32+
<input
33+
className={classNames({ error: touched && error })}
34+
{...input}
35+
placeholder={label}
36+
type={type}
37+
/>
38+
{touched &&
39+
((error && <span className="error">{error}</span>) ||
40+
(warning && <span>{warning}</span>))}
41+
</div>
42+
</div>
43+
);
44+
45+
const PageWithReduxFormsValidations = props => (
46+
<ValidatingReduxFormWizardPage {...props}>
47+
<form onSubmit={props.handleSubmit}>
48+
<Field
49+
component={ValidatingInputField}
50+
type="text"
51+
name="requiredText"
52+
validate={[required]}
53+
label="Required Text"
54+
/>
55+
</form>
56+
</ValidatingReduxFormWizardPage>
57+
);
58+
59+
// values left so we know it's available
60+
function submit() {
61+
// We can do form level validations here if needed using values
62+
63+
const wizardErrors = getWizardErrors(getState());
64+
65+
if (!_.isEmpty(wizardErrors)) {
66+
// We've got errors so let redux form know by returning a rejected promise
67+
return Promise.reject(
68+
new SubmissionError({
69+
...wizardErrors
70+
})
71+
);
72+
}
73+
74+
// No errors so let redux form know we are all good by returning a resolved promise
75+
return Promise.resolve();
76+
}
77+
78+
export default reduxForm({
79+
form: "PageWithReduxFormsValidations",
80+
destroyOnUnmount: false,
81+
forceUnregisterOnUnmount: true,
82+
onSubmit: submit
83+
})(PageWithReduxFormsValidations);

src/screens/wizardExample/components/WizardExampleContainer.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, { Component } from "react";
22
import { connect } from "react-redux";
3-
// import { Field } from "redux-form";
43
import wizard from "../../../modules/wizard";
54
import PageWithValidations from "./PageWithValidations";
65
import PageWithInitializations from "./PageWithInitializations";
6+
import PageWithReduxFormsValidations from "./PageWithReduxFormsValidations";
7+
import LastPageOfWizardWithReduxFormsValidations from "./LastPageOfWizardWithReduxFormsValidations";
78

89
const {
910
components: { WizardPage, Wizard },
@@ -22,14 +23,6 @@ const SimplePage = () => (
2223
</WizardPage>
2324
);
2425

25-
// const required = value => value;
26-
27-
// const PageWithReduxFormsValidations = () => (
28-
// <form name="wizardForm">
29-
// <Field component={text} name="test" validations={[required]} />
30-
// </form>
31-
// );
32-
3326
class WizardExampleContainer extends Component {
3427
componentDidUpdate(prevProps) {
3528
if (wizardIsDisposing(prevProps, this.props)) {
@@ -68,8 +61,19 @@ class WizardExampleContainer extends Component {
6861
{
6962
component: PageWithInitializations,
7063
title: "Step 4: Page with initializations"
64+
},
65+
{
66+
component: PageWithReduxFormsValidations,
67+
title: "Step 5: Page with redux forms validations",
68+
props: { formName: "PageWithReduxFormsValidations" }
69+
},
70+
{
71+
component: LastPageOfWizardWithReduxFormsValidations,
72+
title: "Step 6: Last page with redux forms validations",
73+
props: { formName: "PageWithReduxFormsValidations" }
7174
}
7275
]}
76+
formName="PageWithReduxFormsValidations"
7377
/>
7478
</div>
7579
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.error {
2+
color: red;
3+
border-color: red;
4+
}

0 commit comments

Comments
 (0)