@@ -49,7 +49,7 @@ import {
49
49
50
50
const TextInput = ({ handler, touched, hasError, meta }) => (
51
51
< div>
52
- < input placeholder= ` Enter ${ meta .label } ` {... handler ()}/ >
52
+ < input placeholder= { ` Enter ${ meta .label } ` } {... handler ()}/ >
53
53
< span>
54
54
{touched
55
55
&& hasError (" required" )
@@ -62,8 +62,7 @@ export default class Login extends Component {
62
62
username: [" " , Validators .required ],
63
63
password: [" " , Validators .required ],
64
64
rememberMe: false
65
- });
66
- }
65
+ });
67
66
handleReset = () => {
68
67
this .loginForm .reset ();
69
68
}
@@ -117,7 +116,97 @@ export default class Login extends Component {
117
116
}
118
117
}
119
118
```
120
-
119
+ ## Using [ FormGenerator] ( FormGenerator.md )
120
+ ``` js
121
+ import React , { Component } from ' react' ;
122
+ import {
123
+ Validators ,
124
+ FormGenerator
125
+ } from " ./react-reactive-form/src" ;
126
+ // Input component
127
+ const TextInput = ({ handler, touched, hasError, meta }) => (
128
+ < div>
129
+ < input placeholder= {` Enter ${ meta .label } ` } {... handler ()}/ >
130
+ < span>
131
+ {touched
132
+ && hasError (" required" )
133
+ && ` ${ meta .label } is required` }
134
+ < / span>
135
+ < / div>
136
+ )
137
+ // Checkbox component
138
+ const CheckBox = ({ handler }) => (
139
+ < div>
140
+ < input {... handler (" checkbox" )}/ >
141
+ < / div>
142
+ )
143
+ // Field config to configure form
144
+ const fieldConfig = {
145
+ controls: {
146
+ username: {
147
+ options: {
148
+ validators: Validators .required
149
+ },
150
+ render: TextInput,
151
+ meta: { label: " Username" }
152
+ },
153
+ password: {
154
+ options: {
155
+ validators: Validators .required
156
+ },
157
+ render: TextInput,
158
+ meta: { label: " Password" }
159
+ },
160
+ rememberMe: {
161
+ render: CheckBox
162
+ },
163
+ $field_0: {
164
+ isStatic: false ,
165
+ render : ({ invalid, meta: { handleReset } }) => (
166
+ < div>
167
+ < button
168
+ type= " button"
169
+ onClick= {handleReset}
170
+ >
171
+ Reset
172
+ < / button>
173
+ < button
174
+ type= " submit"
175
+ disabled= {invalid}
176
+ >
177
+ Submit
178
+ < / button>
179
+ < / div>
180
+ )
181
+ }
182
+ },
183
+ }
184
+ export default class Login extends Component {
185
+ handleReset = () => {
186
+ this .loginForm .reset ();
187
+ }
188
+ handleSubmit = (e ) => {
189
+ e .preventDefault ();
190
+ console .log (" Form values" , this .loginForm .value );
191
+ }
192
+ setForm = (form ) => {
193
+ this .loginForm = form;
194
+ this .loginForm .meta = {
195
+ handleReset: this .handleReset
196
+ }
197
+ }
198
+ render () {
199
+ return (
200
+ < form onSubmit= {this .handleSubmit }>
201
+ < FormGenerator
202
+ onMount= {this .setForm }
203
+ fieldConfig= {fieldConfig}
204
+ / >
205
+ < / form>
206
+ );
207
+ }
208
+ }
209
+ ```
121
210
## Add Controls Dynamically
122
211
123
212
You can also create controls without even initializing the group control object with the help of new react form components ( [ FieldGroup] ( docs/api/FieldGroup.md ) , [ FieldControl] ( docs/api/FieldControl.md ) , [ FieldArray] ( docs/api/FieldArray.md ) ).
@@ -265,6 +354,7 @@ export default class Login extends Component {
265
354
# Code Sandboxes
266
355
Try out ` react-reactive-forms ` in these sandbox versions of the Examples.
267
356
* [ Simple Form] ( https://codesandbox.io/s/4rxokpr270 )
357
+ * [ Simple Form With FormGenerator] ( https://codesandbox.io/s/jpkjny836v )
268
358
* [ Sync & Async Validation] ( https://codesandbox.io/s/qq8xq7j2w )
269
359
* [ User Registeration Form With Nested Forms] ( https://codesandbox.io/s/p2rqmr8qk7 )
270
360
* [ Form Array With Dynamic Controls] ( https://codesandbox.io/s/nw9wxw2nvl )
0 commit comments