@@ -18,7 +18,7 @@ class Contact extends React.Component {
18
18
}
19
19
20
20
render ( ) {
21
- const { handleSubmit } = this . props ;
21
+ const { handleSubmit, reset } = this . props ;
22
22
23
23
return (
24
24
< div >
@@ -48,13 +48,17 @@ class Contact extends React.Component {
48
48
</ div >
49
49
< div className = "form-group" >
50
50
< label htmlFor = "exampleSelect1" > { 'Example select' } </ label >
51
- < select className = "form-control" id = "exampleSelect1" >
51
+ < Field
52
+ name = "exampleSelect1"
53
+ className = "form-control"
54
+ component = "select"
55
+ >
52
56
< option > 1</ option >
53
57
< option > 2</ option >
54
58
< option > 3</ option >
55
59
< option > 4</ option >
56
60
< option > 5</ option >
57
- </ select >
61
+ </ Field >
58
62
</ div >
59
63
< div className = "form-group" >
60
64
< Field
@@ -72,6 +76,7 @@ class Contact extends React.Component {
72
76
label = "This code is awesome!"
73
77
name = "codeQualityRadio"
74
78
option = "1"
79
+ checked = { true }
75
80
/>
76
81
< Field
77
82
component = { this . _renderRadio }
@@ -84,6 +89,7 @@ class Contact extends React.Component {
84
89
label = "This code is bad."
85
90
name = "codeQualityRadio"
86
91
option = "3"
92
+ disabled = { true }
87
93
/>
88
94
</ fieldset >
89
95
< div className = "form-check" >
@@ -94,14 +100,27 @@ class Contact extends React.Component {
94
100
type = "checkbox"
95
101
/>
96
102
</ div >
97
- < button type = "submit" className = "btn btn-primary" > { 'Submit' } </ button >
103
+ < button
104
+ type = "submit"
105
+ className = "btn btn-primary"
106
+ >
107
+ { 'Submit' }
108
+ </ button >
109
+ < button
110
+ type = "submit"
111
+ className = "btn btn-primary"
112
+ onClick = { reset }
113
+ >
114
+ { 'Reset' }
115
+ </ button >
98
116
</ form >
99
117
</ div >
100
118
) ;
101
119
}
102
120
103
121
_onFormSubmit ( formData ) {
104
122
// TODO: Can an acton with the form data.
123
+ alert ( formData ) ;
105
124
console . log ( formData ) ;
106
125
}
107
126
@@ -151,6 +170,8 @@ class Contact extends React.Component {
151
170
className = "form-check-input"
152
171
name = { field . input . name }
153
172
value = { field . option }
173
+ disabled = { field . disabled }
174
+ checked = { field . checked }
154
175
/>
155
176
{ field . label }
156
177
</ label >
@@ -186,13 +207,14 @@ export default reduxForm({
186
207
form : 'contactForm' ,
187
208
validate : ( formData ) => {
188
209
const errors = { } ;
210
+ const validEmailRegex = / ^ [ A - Z 0 - 9 . _ % + - ] + @ [ A - Z 0 - 9 . - ] + \. [ A - Z ] { 2 , 4 } $ / i;
189
211
190
- if ( ! formData . name ) {
191
- errors . name = 'Required ' ;
212
+ if ( ! validEmailRegex . test ( formData . email ) ) {
213
+ errors . email = 'Invalid email address ' ;
192
214
}
193
215
194
- if ( ! / ^ [ A - Z 0 - 9 . _ % + - ] + @ [ A - Z 0 - 9 . - ] + \. [ A - Z ] { 2 , 4 } $ / i . test ( formData . email ) ) {
195
- errors . email = 'Invalid email address ' ;
216
+ if ( ! formData . name ) {
217
+ errors . name = 'Required ' ;
196
218
}
197
219
198
220
if ( ! formData . message ) {
0 commit comments