You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/react-renderer-demo/src/pages/components/field-provider.md
+20-12Lines changed: 20 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,35 +9,43 @@ React form renderer is using [react-final-form](https://github.com/final-form/re
9
9
Most of its features are not directly available for consistency and performance reasons. If you want to create any custom
10
10
components, you can access these features via `FieldProvider` component or `useFieldApi` hook.
11
11
12
-
`FieldProvider` is a wrapper using `useFieldApi` to get the access to the form state.
12
+
`FieldProvider` is a wrapper using [useFieldApi](/hooks/use-field-api) to get the access to the form state. **It's recommended to use the hook.** You can read more about that in [Component mapping](/renderer/component-mapping).
13
13
14
-
`useFieldApi` is a wrapper around [React Final Form useField hook](https://final-form.org/docs/react-final-form/api/useField).
14
+
## Props
15
15
16
-
You can read more about that in [Component mapping](/renderer/component-mapping).
16
+
|name|type|description|
17
+
|----|----|-----------|
18
+
|Component|component|A component that receives all field props + meta + input.|
19
+
|render|function|A render function that receives all field props + meta + input.|
17
20
18
-
## Implementation of component
21
+
## Usage
19
22
20
23
Next example shows simple input field with label and error message.
In order to successfully render a form via FormRenderer you need to assign component mappers. Component mapper is an object of React components,
9
-
where key is a component identifier and value is the Component. The identifiers must be equivalent to `componentTypes` in your schema.
9
+
In order to successfully render a form via FormRenderer you need to assign component mappers. Component mapper is an object of React components, where key is a component identifier and value is the component or an [object](/mappers/global-component-props) with the component and globally defined props. Each component in mapper must have an unique key, which corresponds to `componentType` in the schema. Keys names can be chosen but there are some predefined [constants](/schema/constants#componenttypes) which cover most common component types. Use these to prevent typos and inconsistencies.
10
10
11
-
## Creating componentMapper
11
+
## Creating form input components
12
12
13
-
Component mapper defines components that are rendered from input schema. Each component in mapper must have an unique key,
14
-
which corresponds to `componentType` in the schema. Keys names can be chosen but there are some predefined constants
15
-
which cover most common component types.
13
+
A custom component is just a standard React component, that has access to two form state objects: **meta** and **input**.
You have two options how to connect your component to the form state.
19
+
---
20
+
21
+
### Connecting component to form state
22
+
23
+
You have two options how to connect your component to these objects:
37
24
38
25
### useFieldApi
39
26
40
-
First, you can use `useFieldApi` hook.
27
+
**Recommended**
41
28
42
-
This hook needs `name`, in case of special input types which are using checked as the input value (checbkoxes, switches) you have to assign `type: checkbox`. The hook will return all field props, including input and meta. [TODO: LINK TO WHAT IS INPUT AND META]
29
+
First, you can use [useFieldApi](/hooks/use-field-api) hook.
30
+
31
+
This hook needs `name`, in case of special input types which are using checked as the input value (checbkoxes, switches) you have to assign `type: checkbox`. The hook will return all field props, including input and meta.
Or you can import `FieldProvider` component from Data Driven Forms. This component needs to obtain `render` or `Component` prop.
55
-
43
+
Or you can import [FieldProvider](/components/field-provider) component from Data Driven Forms. This component needs to obtain `render` or `Component` prop.
Below, you can see an basic implementation of custom component mapper:
70
-
<br />
57
+
Data Driven Forms will render any component you pass to it, so you don't have to connect components to the form state in order to render it. Be aware that the component will receive metadata props such as `component`, `validate`, etc. You have to catch them before passing to other elements, otherwise it could throw DOM warnings.
If you are building a new component mapper inside the Data Driven Forms repository, you can use a terminal command in the root folder to generate a basic mapper for you:
100
+
101
+
```bash
102
+
yarn generate-template
103
+
```
104
+
105
+
Or you can get the files directly on [GitHub](https://github.com/data-driven-forms/react-forms/tree/master/templates).
0 commit comments