-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClassForm.tsx
More file actions
113 lines (104 loc) · 3.87 KB
/
ClassForm.tsx
File metadata and controls
113 lines (104 loc) · 3.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import ValidationComponent, { ClassValidationProps, FormState } from 'react-simple-form-validator';
import { Fragment } from 'react';
class ClassForm extends ValidationComponent<ClassValidationProps, FormState> {
constructor(props: ClassValidationProps) {
super(props);
this.state = {
civility: '',
email: '',
firstName: '',
lastName: '',
touchedFields: { civility: false, firstName: false, lastName: false, email: false }
};
}
onBlurHandler(event: React.FormEvent<HTMLElement>, field: string): void {
this.setState({ touchedFields: { ...this.state.touchedFields, [field]: true } });
}
formSubmitHandler(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
const { civility, email, firstName, lastName } = this.state;
console.log('Form Values : ', civility, email, firstName, lastName);
this.setState({
civility: '',
email: '',
firstName: '',
lastName: '',
touchedFields: { civility: false, firstName: false, lastName: false, email: false }
});
}
render(): JSX.Element {
return (
<Fragment>
<h2>Class based Form</h2>
<form onSubmit={this.formSubmitHandler.bind(this)}>
<div className="form-control">
<label htmlFor="gender">Civility</label>
<input
id="civility"
type="text"
onChange={(e) => this.validate({ civility: e.target.value })}
onBlur={(e) => this.onBlurHandler(e, 'civility')}
value={this.state.civility}
placeholder="Seize a civility (Mrs or Ms or Miss)"
/>
<p className="error-text">
{this.state.touchedFields.civility &&
this.isFieldInError('civility') &&
this.getErrorsInField('civility').join('\n')}
</p>
</div>
<div className="form-control">
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
onChange={(e) => this.validate({ email: e.target.value })}
onBlur={(e) => this.onBlurHandler(e, 'email')}
value={this.state.email}
placeholder="Seize an email"
/>
<p className="error-text">
{this.state.touchedFields.email &&
this.isFieldInError('email') &&
this.getErrorsInField('email').join('\n')}
</p>
</div>
<div className="form-control">
<label htmlFor="firstName">First name</label>
<input
id="firstName"
type="text"
onChange={(e) => this.validate({ firstName: e.target.value })}
onBlur={(e) => this.onBlurHandler(e, 'firstName')}
value={this.state.firstName}
placeholder="Seize a first name"
/>
<p className="error-text">
{this.state.touchedFields.firstName &&
this.isFieldInError('firstName') &&
this.getErrorsInField('firstName').join('\n')}
</p>
</div>
<div className="form-control">
<label htmlFor="astName">Last Name</label>
<input
id="lastName"
type="text"
onChange={(e) => this.validate({ lastName: e.target.value })}
onBlur={(e) => this.onBlurHandler(e, 'lastName')}
value={this.state.lastName}
placeholder="Seize a last name"
/>
<p className="error-text">
{this.state.touchedFields.lastName &&
this.isFieldInError('lastName') &&
this.getErrorsInField('lastName').join('\n')}
</p>
</div>
<button disabled={!this.isFormValid}>Submit</button>
</form>
</Fragment>
);
}
}
export default ClassForm;