|
1 | 1 |
|
| 2 | +# FormGenerator |
| 3 | +A react component which generates a new tree of control objects or can be used with an existing control tree & assign the components to the controls. |
| 4 | + |
| 5 | +## How it works |
| 6 | + - It creates a new instance of [FormControl](FormControl.md) if the `name` prop is defined. |
| 7 | + - If a `name` prop is defined then it means that the control has to be added in an already existing parent control ( [FormGroup](FormGroup.md) / [FormArray](FormArray.md)) i.e the parent control must be present. |
| 8 | + - If a control with the same name is already present in the parent control then it just returns the same otherwise it'll create a new instance of [FormControl](FormControl.md) class. |
| 9 | + - You can define a parent control either by passing the `parent` prop or using the component as a child of the `FieldArray` or `FieldGroup` component. |
| 10 | + - If a `control` prop is defined then it just returns the same. |
| 11 | + |
| 12 | + |
| 13 | +## Props |
| 14 | + |
| 15 | +## fieldConfig |
| 16 | + |
| 17 | +Checkout these properties of controls |
| 18 | +```ts |
| 19 | +controls: Array<{[key: string]: any}> | {[key: string]: any}; |
| 20 | +``` |
| 21 | +Controls as an object |
| 22 | +```ts |
| 23 | +const fieldConfig = { |
| 24 | + controls: { |
| 25 | + username: { |
| 26 | + ... |
| 27 | + }, |
| 28 | + password: { |
| 29 | + .... |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | +Controls as an array |
| 35 | +```ts |
| 36 | +const fieldConfig = { |
| 37 | + controls: [ |
| 38 | + { |
| 39 | + ... // item1 |
| 40 | + }, |
| 41 | + { |
| 42 | + ... // item2 |
| 43 | + } |
| 44 | + ] |
| 45 | +} |
| 46 | +``` |
| 47 | +Nested Controls in object |
| 48 | +```ts |
| 49 | +const fieldConfig = { |
| 50 | + controls: { |
| 51 | + address: { |
| 52 | + controls: { |
| 53 | + city: { |
| 54 | + ... |
| 55 | + }, |
| 56 | + country: { |
| 57 | + ... |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Nested Controls In array |
| 66 | + |
| 67 | +```ts |
| 68 | +const fieldConfig = { |
| 69 | + controls: [ |
| 70 | + { |
| 71 | + // item1 |
| 72 | + controls: { |
| 73 | + itemName: { |
| 74 | + .... |
| 75 | + }, |
| 76 | + itemPrice: { |
| 77 | + ... |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | + { |
| 82 | + // item2 |
| 83 | + } |
| 84 | + ] |
| 85 | +} |
| 86 | +``` |
| 87 | +## |
| 88 | + |
| 89 | +```ts |
| 90 | +formState: any|{ value: any, disabled: boolean } |
| 91 | +``` |
| 92 | +You can use this prop to define the initial state of the control. |
| 93 | +## |
| 94 | + |
| 95 | +```ts |
| 96 | + render: (control: FormArray|FormControl|FormGroup) => React.ReactElement<any>|React.ReactElement<any>[]; |
| 97 | +``` |
| 98 | +A render function prop which returns a react component which needs to be re-render whenever the control state changes. |
| 99 | +You can also pass a render function as a child. |
| 100 | + |
| 101 | +## |
| 102 | +```ts |
| 103 | +control: AbstractControl; |
| 104 | +``` |
| 105 | +An instance of [AbstractControl](AbstractControl.md) control. |
| 106 | + |
| 107 | +## |
| 108 | +```ts |
| 109 | +name: string; |
| 110 | +``` |
| 111 | +Name of the control. |
| 112 | + |
| 113 | +## |
| 114 | +```ts |
| 115 | +index: number |
| 116 | +``` |
| 117 | +To define at which index the controls has to be inserted if the parent control is an instance of [FormArray](FormArray.md). |
| 118 | + |
| 119 | +## |
| 120 | +```ts |
| 121 | +options: AbstractControlOptions; |
| 122 | +``` |
| 123 | +You can pass the [AbstractControlOptions](AbstractControlOptions.md) as `options` props. |
| 124 | + |
| 125 | +## |
| 126 | +```ts |
| 127 | +parent: AbstractControl; |
| 128 | +``` |
| 129 | +An instance of [FormGroup](FormGroup.md) or [FormArray](FormArray.md) class as a parent control. |
| 130 | + |
| 131 | +## |
| 132 | +```ts |
| 133 | +meta: {[key: string]: any}; |
| 134 | +``` |
| 135 | +You can pass an object of custom variables to customize your component. |
| 136 | + |
| 137 | +For example: |
| 138 | + |
| 139 | +```ts |
| 140 | +<FieldControl |
| 141 | + meta={{ |
| 142 | + label: "First Name", |
| 143 | + placeholder: "Enter your first name" |
| 144 | + }} |
| 145 | + ... |
| 146 | +/> |
| 147 | +``` |
0 commit comments