-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathexample-async.jsx
More file actions
68 lines (63 loc) · 1.97 KB
/
example-async.jsx
File metadata and controls
68 lines (63 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class ExampleAsyncForm extends React.Component {
constructor(props) {
super(props);
this.state = {
processing: false
};
this.validator = new SimpleReactValidator({
className: 'text-danger',
validators: {
unique: {
message: 'Not a unique email.',
asyncRule: function(val, params, validator, completion) {
setTimeout(() => validator.fail(completion), 1000);
}
}
}
});
}
submitForm() {
this.setState({processing: true});
this.validator.asyncValid({
pass: () => {
alert('You submitted the form and stuff!');
this.setState({processing: false});
},
fail: () => {
console.log(this.validator);
this.validator.showMessages();
this.setState({processing: false});
}
});
}
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
render() {
return (
<div className="container card my-4">
<div className="card-body">
<h3>Example Async Form</h3>
<small className="text-muted">Click submit to view messages.</small>
<hr />
<div className="row">
<div className="col-sm-6 col-md-4">
<div className="form-group">
<label>Email</label>
<input className="form-control" type="email" name="email" value={this.state.email} onChange={this.handleInputChange.bind(this)} />
{this.validator.message('email', this.state.email, 'email|unique')}
</div>
</div>
</div>
<button className="btn btn-primary" onClick={this.submitForm.bind(this)} disabled={this.state.processing}>Submit</button>
</div>
</div>
);
}
}
ReactDOM.render(<ExampleAsyncForm />, document.getElementById('exampleAsyncForm'));