Skip to content

Commit 6c7df5e

Browse files
author
Kuldeep Saxena
authored
Update FormGenerator.md
1 parent 825ccbf commit 6c7df5e

File tree

1 file changed

+118
-18
lines changed

1 file changed

+118
-18
lines changed

docs/api/FormGenerator.md

Lines changed: 118 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11

22
# FormGenerator
3-
A react component which generates a tree of control objects or can be append to an existing control tree & renders the associated React components in the same order.
3+
A react component which generates a tree of control objects & render the UI by keeping the same order in which the controls have been defined in `fieldConfig`.
44

55
## How it works
6-
- It creates a new instance of [FormGroup](FormGroup.md) ( if `controls` property is an object ) or [FormArray]
7-
(FormArray.md) ( if `controls` property is an array ).
8-
- It renders the UI of the form according to associated components to the controls by keeping the same order in which they have been defined in `fieldConfig`.
6+
- It creates a new instance of [FormGroup](FormGroup.md) ( if `controls` property is an object ) or
7+
[FormArray](FormArray.md) ( if `controls` property is an array ) if the `control` property is not supplied.
8+
- It renders the form UI according to the control-component mapping by keeping the same order in which they have been
9+
defined in `fieldConfig`.
910
- You can define a parent control by passing the `parent` property.
1011
- If a `control` prop is defined then it just returns the same.
1112

1213

1314
## Props
15+
##
16+
```ts
17+
onMount: (form: FormGroup|FormArray) => void
18+
```
19+
A function callback called when a form has been rendered, the basic use case is to save the form instance for further uses.
20+
##
21+
```ts
22+
onUnmount: () => void
23+
```
24+
A function callback called when a form has been unmounted.
25+
##
26+
```ts
27+
fieldConfig: {[key: string]: any}
28+
```
29+
Field config has a set of properties which are required for the form configuration.
1430

15-
## fieldConfig
16-
17-
Checkout these properties of controls
31+
### Properties of `fieldConfig`
1832
```ts
1933
controls: Array<{[key: string]: any}> | {[key: string]: any};
2034
```
21-
Controls as an object
35+
FormGenerator creates a [FormGroup](FormGroup.md) if the `controls` property is an object.
36+
37+
For example the following config will create a [FormGroup](FormGroup.md) with two form controls named `username` & `password` respectively.
2238
```ts
2339
const fieldConfig = {
2440
controls: {
@@ -31,7 +47,11 @@ const fieldConfig = {
3147
}
3248
}
3349
```
34-
Controls as an array
50+
51+
FormGenerator creates a [FormArray](FormArray.md) if the `controls` property is an array.
52+
53+
For example the following config will create a [FormArray](FormArray.md) with two form controls at index `0` & `1` respectively.
54+
3555
```ts
3656
const fieldConfig = {
3757
controls: [
@@ -44,7 +64,11 @@ const fieldConfig = {
4464
]
4565
}
4666
```
47-
Nested Controls in object
67+
68+
#### Nested Controls
69+
You can also define the nested controls in the same way.
70+
71+
Example: Nested controls in `FormGroup`
4872
```ts
4973
const fieldConfig = {
5074
controls: {
@@ -61,8 +85,17 @@ const fieldConfig = {
6185
}
6286
}
6387
```
88+
The above example will create a structure like that:
89+
```ts
90+
{
91+
address: {
92+
city: "",
93+
country: ""
94+
}
95+
}
96+
```
6497

65-
Nested Controls In array
98+
Example: Nested controls in `FormArray`
6699

67100
```ts
68101
const fieldConfig = {
@@ -84,43 +117,110 @@ const fieldConfig = {
84117
]
85118
}
86119
```
120+
The above example will create a structure like that:
121+
```ts
122+
[
123+
{
124+
itemName: "",
125+
itemPrice: ""
126+
},
127+
"" // item2
128+
]
129+
```
87130
##
88-
89131
```ts
90132
formState: any|{ value: any, disabled: boolean }
91133
```
92134
You can use this prop to define the initial state of the control.
135+
### Note:
136+
Only works with [FormControl](FormControl.md)
137+
93138
##
94139
```ts
95140
meta: {[key: string]: any};
96141
```
142+
You can pass an object of custom inputs to customize your component.
143+
97144
##
98-
You can pass an object of custom variables to customize your component.
99145
```ts
100146
render: (control: FormArray|FormControl|FormGroup) => React.ReactElement<any>|React.ReactElement<any>[];
101147
```
102148
A render function prop which returns a react component which needs to be re-render whenever the control state changes.
103-
You can also pass a render function as a child.
104149

105-
##
150+
### Note:
151+
Only works with [FormControl](FormControl.md)
152+
153+
For example:
106154
```ts
107-
control: AbstractControl;
155+
const fieldConfig = {
156+
controls: {
157+
username: {
158+
render: TextInput // some react component to render an input,
159+
meta: {
160+
label: "Username"
161+
}
162+
},
163+
password: {
164+
render: TextInput,
165+
meta: {
166+
label: "Password",
167+
type: "password"
168+
}
169+
}
170+
}
171+
}
108172
```
109-
An instance of [AbstractControl](AbstractControl.md) control.
110-
111173
##
112174

113175
```ts
114176
index: number
115177
```
116178
To define at which index the controls has to be inserted if the parent control is an instance of [FormArray](FormArray.md).
117179

180+
### Note:
181+
Only works if the parent is [FormArray](FormArray.md).
182+
118183
##
119184
```ts
120185
options: AbstractControlOptions;
121186
```
122187
You can pass the [AbstractControlOptions](AbstractControlOptions.md) as `options` props.
123188

189+
For example:
190+
```ts
191+
const fieldConfig = {
192+
controls: {
193+
username: {
194+
render: TextInput // some react component to render an input,
195+
meta: {
196+
label: "Username"
197+
}
198+
options: {
199+
validators: Validators.required,
200+
updateOn: 'blur'
201+
}
202+
},
203+
password: {
204+
render: TextInput,
205+
meta: {
206+
label: "Password",
207+
type: "password"
208+
},
209+
options: {
210+
validators: Validators.required,
211+
}
212+
}
213+
}
214+
}
215+
```
216+
217+
218+
##
219+
```ts
220+
control: AbstractControl;
221+
```
222+
An instance of [AbstractControl](AbstractControl.md) control.
223+
124224
##
125225
```ts
126226
parent: AbstractControl;

0 commit comments

Comments
 (0)