Skip to content

Commit 505754e

Browse files
WIP - HOC support
1 parent 7ef9462 commit 505754e

File tree

5 files changed

+108
-69
lines changed

5 files changed

+108
-69
lines changed

src/examples/Example.js

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export default class Example extends Component {
5555
preventEnterSubmission={true}
5656
nextTextOnFinalActionStep={"Save"}
5757
hocValidationAppliedTo={[3]}
58-
hocValidationPredicate={(compRef) => (compRef.isValid()) }
5958
/>
6059
</div>
6160
</div>

src/examples/Step4.js

+88-62
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,104 @@ import strategy from 'joi-validation-strategy';
66
import Joi from 'joi';
77

88
class Step4 extends Component {
9-
constructor(props) {
10-
super(props);
9+
constructor(props) {
10+
super(props);
1111

12-
this.state = {
13-
email: ''
14-
};
12+
this.state = {
13+
emailEmergency: ''
14+
};
1515

16-
this.validatorTypes = {
17-
email: Joi.string().email().required()
18-
};
16+
this.validatorTypes = {
17+
emailEmergency: Joi.string().email().required()
18+
};
1919

20-
this.getValidatorData = this.getValidatorData.bind(this);
21-
this.renderHelpText = this.renderHelpText.bind(this);
22-
}
20+
this.getValidatorData = this.getValidatorData.bind(this);
21+
this.renderHelpText = this.renderHelpText.bind(this);
22+
this.isValidated = this.isValidated.bind(this);
23+
}
2324

24-
getValidatorData() {
25-
return {
26-
email: this.refs.email.value,
25+
isValidated() {
26+
return new Promise((resolve, reject) => {
27+
this.props.validate((error) => {
28+
if (error) {
29+
reject(); // form contains errors
30+
return;
2731
}
28-
};
2932

30-
onChange(e) {
31-
let newState = {};
32-
newState[e.target.name] = e.target.value;
33-
this.setState(newState);
33+
this.props.updateStore({
34+
...this.getValidatorData(),
35+
savedToCloud: false // use this to notify step4 that some changes took place and prompt the user to save again
36+
}); // Update store here (this is just an example, in reality you will do it via redux or flux)
37+
38+
resolve(); // form is valid, fire action
39+
});
40+
});
41+
}
42+
43+
getValidatorData() {
44+
return {
45+
emailEmergency: this.refs.emailEmergency.value,
3446
}
47+
};
3548

36-
renderHelpText(message, id) {
37-
return (<div className="val-err-tooltip" key={id}><span>{message}</span></div>);
38-
};
49+
onChange(e) {
50+
let newState = {};
51+
newState[e.target.name] = e.target.value;
52+
this.setState(newState);
53+
}
54+
55+
renderHelpText(message, id) {
56+
return (<div className="val-err-tooltip" key={id}><span>{message}</span></div>);
57+
};
58+
59+
render() {
60+
// explicit class assigning based on validation
61+
let notValidClasses = {};
62+
notValidClasses.emailEmergencyCls = this.props.isValid('emailEmergency') ?
63+
'no-error col-md-8' : 'has-error col-md-8';
3964

40-
render() {
41-
// explicit class assigning based on validation
42-
let notValidClasses = {};
43-
notValidClasses.emailCls = this.props.isValid('email') ?
44-
'no-error col-md-8' : 'has-error col-md-8';
45-
46-
return (
47-
<div className="step step4">
48-
<div className="row">
49-
<form id="Form" className="form-horizontal">
50-
<div className="form-group">
51-
<label className="col-md-12 control-label">
52-
<h1>Step 4: Enter your emergency contacts details:</h1>
53-
</label>
54-
</div>
55-
<div className="form-group col-md-12">
56-
<label className="control-label col-md-4">
57-
Email
58-
</label>
59-
<div className={notValidClasses.emailCls}>
60-
<input
61-
ref="email"
62-
autoComplete="off"
63-
type="email"
64-
placeholder="[email protected]"
65-
className="form-control"
66-
name="email"
67-
value={this.state.email}
68-
required
69-
onBlur={this.props.handleValidation('email')}
70-
onChange={this.onChange.bind(this)}
71-
/>
72-
73-
{this.props.getValidationMessages('email').map(this.renderHelpText)}
74-
</div>
75-
</div>
76-
</form>
77-
</div>
65+
return (
66+
<div className="step step4">
67+
<div className="row">
68+
<form id="Form" className="form-horizontal">
69+
<div className="form-group">
70+
<label className="control-label col-md-12 ">
71+
<h1>Step 4: Enter your emergency contacts details:</h1>
72+
</label>
73+
</div>
74+
<div className="form-group col-md-12">
75+
<label className="control-label col-md-4">
76+
Your Emergency Email Address
77+
</label>
78+
<div className={notValidClasses.emailEmergencyCls}>
79+
<input
80+
ref="emailEmergency"
81+
autoComplete="off"
82+
type="emailEmergency"
83+
placeholder="[email protected]"
84+
className="form-control"
85+
name="emailEmergency"
86+
value={this.state.emailEmergency}
87+
required
88+
onBlur={this.props.handleValidation('emailEmergency')}
89+
onChange={this.onChange.bind(this)}
90+
/>
91+
92+
{this.props.getValidationMessages('emailEmergency').map(this.renderHelpText)}
93+
</div>
94+
</div>
95+
<div className="form-group hoc-alert col-md-12 ">
96+
<label className="col-md-12 control-label">
97+
<h4>You can also use <a href="https://github.com/jurassix/react-validation-mixin" target="_blank">react-validation-mixin</a> to handle your validations as well! (as of v4.3.0)!</h4>
98+
</label>
99+
<br />
100+
<div className="green">... StepZilla steps can use a mix of basic JS validation of Higer Order Component (HOC) based validation with react-validation-mixin.</div>
101+
</div>
102+
</form>
78103
</div>
79-
)
80-
}
104+
</div>
105+
)
106+
}
81107
}
82108

83109
Step4.propTypes = {

src/examples/Step5.js

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ export default class Step5 extends Component {
8888
{this.props.getStore().email}
8989
</div>
9090
</div>
91+
<div className="col-md-12 txt">
92+
<div className="col-md-4">
93+
Emergency Email
94+
</div>
95+
<div className="col-md-4">
96+
{this.props.getStore().emailEmergency}
97+
</div>
98+
</div>
9199
<div className="col-md-12 eg-jump-lnk">
92100
<a href="#" onClick={() => this.jumpToStep(1)}>e.g. showing how we use the jumpToStep method helper method to jump back to step 1</a>
93101
</div>

src/examples/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
margin-left: 10px;
2323
}
2424

25+
html h4 {
26+
font-size: 16px;
27+
}
28+
2529
.progtrckr {
2630
text-align: center;
2731
padding-bottom: 16px;
@@ -115,6 +119,10 @@
115119
span.bold {
116120
font-weight: bold;
117121
}
122+
123+
html .hoc-alert {
124+
margin-top: 20px;
125+
}
118126
</style>
119127
</head>
120128
<body>

src/main.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,15 @@ export default class StepZilla extends Component {
218218

219219
// are we allowed to move forward? via the next button or via jumpToStep?
220220
stepMoveAllowed(skipValidationExecution = false) {
221-
debugger;
222221
let proceed = false;
223222

224223
if (this.props.dontValidate) {
225224
proceed = true;
226225
}
227226
else {
228-
if (this.props.hocValidationPredicate != null && this.props.hocValidationAppliedTo.length > 0 && this.props.hocValidationAppliedTo.indexOf(this.state.compState) > -1) {
229-
// the user is using a higer order component (HOC) for validation (e.g react-validation-mixin), this wraps the StepZilla steps as per hocValidationAppliedTo, so use the provided predicate method to determine if the HOC validation passes/fails
230-
proceed = this.props.hocValidationPredicate(this.refs.activeComponent);
227+
if (this.props.hocValidationAppliedTo.length > 0 && this.props.hocValidationAppliedTo.indexOf(this.state.compState) > -1) {
228+
// the user is using a higer order component (HOC) for validation (e.g react-validation-mixin), this wraps the StepZilla steps as a HOC, so use hocValidationAppliedTo to determine if this step needs the aync validation as per react-validation-mixin interface
229+
proceed = this.refs.activeComponent.refs.component.isValidated();
231230
}
232231
else if (Object.keys(this.refs).length == 0 || typeof this.refs.activeComponent.isValidated == 'undefined') {
233232
// if its a form component, it should have implemeted a public isValidated class (also pure componenets wont even have refs - i.e. a empty object). If not then continue
@@ -328,6 +327,5 @@ StepZilla.defaultProps = {
328327
preventEnterSubmission: false,
329328
startAtStep: 0,
330329
nextTextOnFinalActionStep: "Next",
331-
hocValidationAppliedTo: [],
332-
hocValidationPredicate: null
330+
hocValidationAppliedTo: []
333331
};

0 commit comments

Comments
 (0)