|
| 1 | +# FieldControl |
| 2 | +A react component which creates a new or can be used with an existing [FormControl](FormControl.md) control. |
1 | 3 |
|
| 4 | +## How it works |
| 5 | + - It creates a new instance of [FormControl](FormControl.md) if the `name` prop is defined. |
| 6 | + - 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. |
| 7 | + - 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. |
| 8 | + - 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. |
| 9 | + - If a `control` prop is defined then it just returns the same. |
| 10 | + |
| 11 | + |
| 12 | +## Props |
| 13 | +```ts |
| 14 | +strict: boolean; |
| 15 | +``` |
| 16 | +Default value: `true` |
| 17 | + |
| 18 | +If `true` then it'll only re-render the component only when any change happens in the form control irrespective of the parent component(state and props) changes. |
| 19 | + |
| 20 | +## |
| 21 | + |
| 22 | +```ts |
| 23 | +formState: any|{ value: any, disabled: boolean } |
| 24 | +``` |
| 25 | +You can use this prop to define the initial state of the control. |
| 26 | +For e.g |
| 27 | + - To set an initial value |
| 28 | + ```ts |
| 29 | + <FieldControl |
| 30 | + formState="InitialValue" |
| 31 | + /> |
| 32 | + ``` |
| 33 | +- To set a form control with initial state as disabled |
| 34 | +```ts |
| 35 | + <FieldControl |
| 36 | + formState={ value: "InitialValue", disabled: true } |
| 37 | + /> |
| 38 | + ``` |
| 39 | +## |
| 40 | + |
| 41 | +```ts |
| 42 | + render: (control: FormArray|FormControl|FormGroup) => React.ReactElement<any>|React.ReactElement<any>[]; |
| 43 | +``` |
| 44 | +A render function prop which returns a react component which needs to be re-render whenever the control state changes. |
| 45 | +You can also pass a render function as a child. |
| 46 | +For eg. |
| 47 | + |
| 48 | +```ts |
| 49 | + <FieldControl ...> |
| 50 | + { |
| 51 | + (control) => <SomeComponent/> |
| 52 | + } |
| 53 | + </FieldControl> |
| 54 | +``` |
| 55 | + |
| 56 | +## |
| 57 | +```ts |
| 58 | +control: AbstractControl; |
| 59 | +``` |
| 60 | +An instance of [FieldControl](FieldControl.md) control. |
| 61 | + |
| 62 | +## |
| 63 | +```ts |
| 64 | +name: string; |
| 65 | +``` |
| 66 | +Name of the control. |
| 67 | + |
| 68 | +## |
| 69 | +```ts |
| 70 | +index: number |
| 71 | +``` |
| 72 | +To define at which index the controls has to be inserted if the parent control is an instance of [FormArray](FormArray.md). |
| 73 | + |
| 74 | +## |
| 75 | +```ts |
| 76 | +options: AbstractControlOptions; |
| 77 | +``` |
| 78 | +You can pass the [AbstractControlOptions](AbstractControlOptions.md) as `options` props. |
| 79 | + |
| 80 | +For eg. |
| 81 | + |
| 82 | +```ts |
| 83 | +<FieldControl |
| 84 | + options={{ |
| 85 | + validators: Validators.required, |
| 86 | + updateOn: 'blur' |
| 87 | + }} |
| 88 | +/> |
| 89 | +``` |
| 90 | + |
| 91 | +## |
| 92 | +```ts |
| 93 | +parent: AbstractControl; |
| 94 | +``` |
| 95 | +An instance of FormGroup or FormArray class as a parent control. |
0 commit comments