@@ -49,7 +49,7 @@ import {
4949
5050const TextInput = ({ handler, touched, hasError, meta }) => (
5151 < div>
52- < input placeholder= ` Enter ${ meta .label } ` {... handler ()}/ >
52+ < input placeholder= { ` Enter ${ meta .label } ` } {... handler ()}/ >
5353 < span>
5454 {touched
5555 && hasError (" required" )
@@ -62,8 +62,7 @@ export default class Login extends Component {
6262 username: [" " , Validators .required ],
6363 password: [" " , Validators .required ],
6464 rememberMe: false
65- });
66- }
65+ });
6766 handleReset = () => {
6867 this .loginForm .reset ();
6968 }
@@ -117,7 +116,97 @@ export default class Login extends Component {
117116 }
118117}
119118```
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+ ```
121210## Add Controls Dynamically
122211
123212You 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 {
265354# Code Sandboxes
266355 Try out ` react-reactive-forms ` in these sandbox versions of the Examples.
267356* [ Simple Form] ( https://codesandbox.io/s/4rxokpr270 )
357+ * [ Simple Form With FormGenerator] ( https://codesandbox.io/s/jpkjny836v )
268358* [ Sync & Async Validation] ( https://codesandbox.io/s/qq8xq7j2w )
269359* [ User Registeration Form With Nested Forms] ( https://codesandbox.io/s/p2rqmr8qk7 )
270360* [ Form Array With Dynamic Controls] ( https://codesandbox.io/s/nw9wxw2nvl )
0 commit comments