Skip to content

Commit c3d8cb7

Browse files
authored
Merge pull request #371 from rvsia/updateDoc
[V2] Update doc
2 parents daae740 + 6dd0dd0 commit c3d8cb7

23 files changed

+648
-250
lines changed

packages/react-renderer-demo/src/app/pages/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ const LandingPage = () => {
4545
<div className={classes.landingPageContainer}>
4646
<LandingPageTitle />
4747
<Typography className={classes.landingPageText}>
48-
Data Driven Forms is a component designed for ManageIQ and Red&nbsp;Hat&nbsp;Cloud&nbsp;Services projects that takes JSON form definitions
49-
and renders them into react components.
48+
Data Driven Forms converts JSON form definitions into fully functional React forms.
5049
</Typography>
5150
<div className={classes.getStartedLink}>
5251
<Link href="/renderer/installation">
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import Grid from '@material-ui/core/Grid'
2+
import RouterLink from 'next/link';
3+
import Link from '@material-ui/core/Link';
4+
import ListOfContents from '../../src/helpers/list-of-contents';
5+
6+
<Grid container item>
7+
<Grid item xs={12} md={10}>
8+
9+
# Action Mapper
10+
11+
The <RouterLink href="/renderer/renderer-api#optionalprops"><Link href="/renderer/renderer-api#optionalprops">ActionMapper</Link></RouterLink> allows you to map schema props to functions. This is useful when your schema is not written in JavaScript and you cannot use function inside of it, especially for schemas stored in databases.
12+
13+
## Mapper
14+
15+
```jsx
16+
{
17+
[actionName]: (...args) => {}
18+
}
19+
```
20+
21+
## Schema
22+
23+
```jsx
24+
{
25+
component: component,
26+
name: name,
27+
actions: {
28+
[props]: [actionName, ...args]
29+
}
30+
}
31+
```
32+
33+
Do not forget to keep order of args or use object with keys as named arguments.
34+
35+
# Example
36+
37+
So, let's say you need to translate labels of fields using your translate function `translate(id)` and the schema has no access to use JavaScript code.
38+
39+
Firstly, define a action mapper object.
40+
41+
```jsx
42+
const actionMapper = {
43+
translateLabel: (id) => translate(id)
44+
}
45+
```
46+
47+
Add this object as a prop to FormRenderer.
48+
49+
```jsx
50+
<FormRenderer
51+
{...props}
52+
schema={schema}
53+
actionMapper={actionMapper}
54+
/>
55+
```
56+
57+
Then, in your schema you can map `translateLabel` action to a prop:
58+
59+
```js
60+
{
61+
"fields": [
62+
{
63+
"component": "text-field",
64+
"name": "translate-me",
65+
"actions": {
66+
"label": ["translateLabel", "translate_label_id"]
67+
}
68+
}
69+
]
70+
}
71+
```
72+
73+
</Grid>
74+
<Grid item xs={false} md={2}>
75+
<ListOfContents file="renderer/action-mapper" />
76+
</Grid>
77+
</Grid>

packages/react-renderer-demo/src/app/pages/renderer/component-api.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ There is very short list of that can be applied to every form field
2020
|component|string|Component identifier from componentMapper. Rendered component is chosen by this value|
2121
|hideField|boolean|Equivalent to html attribute `hidden`. Hides the field but it remains in DOM. Note that the field is still impacted by the form state. Validation will still apply on hidden field but the error message will not be displayed.|
2222

23-
Each mapper provided in `react-forms` provides a default API for standard components. Standard components are:
23+
Each mapper provided in `react-forms` provides a default API for standard components. If you want to keep compatibility between our mappers and custom ones, please follow these APIs. Otherwise, it is up to you, which props you choose.
24+
25+
Standard components are:
2426
<br />
2527

2628
Wrapped in formGroup: <br/>
@@ -184,8 +186,8 @@ Wizard step <br/>
184186
|Prop|Type|Description|
185187
|----|:--:|----------:|
186188
|title|node/string|Step title|
187-
|stepKey|string, number|For first step: 1, otherwise anything|
188-
|nextStep|object/stepKey of next step|See below|
189+
|name|string, number|Uniq name of the step|
190+
|nextStep|object/name of next step|See <RouterLink href="/component-example/wizard"><Link href="/component-example/wizard">wizard documentation</Link></RouterLink>|
189191
|fields|array|An array of form fields|
190192

191193
<ExampleLink to='wizard' />

packages/react-renderer-demo/src/app/pages/renderer/component-mapping.md

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,16 @@ import ListOfContents from '../../src/helpers/list-of-contents';
77
<Grid container item>
88
<Grid item xs={12} md={10}>
99

10-
# Introduction
10+
# ComponentMapper
1111

1212
In order to successfully render a form via FormRenderer you need to assign component mappers. Component mapper is an object of React components,
1313
where key is a component identifier and value is the Component. The identifiers must be equivalent to `componentTypes` in your schema.
1414

15-
## FormTemplate
15+
## Creating componentMapper
1616

17-
TODO
18-
19-
## ComponentMapper
20-
Component mapper defines form inputs which can mutate the form state.
21-
22-
# Creating FormTemplate
23-
24-
25-
# Creating componentMapper
26-
27-
Form fields mapper defines components that are rendered from input schema. Each component in mapper must have an unique key,
28-
which corresponds to `componentType` in input schema. Keys names can be chosen but there are some predefined constants
29-
which cover most common component types. Predefined components are also automatically enhanced and connected to the form state.
17+
Component mapper defines components that are rendered from input schema. Each component in mapper must have an unique key,
18+
which corresponds to `componentType` in the schema. Keys names can be chosen but there are some predefined constants
19+
which cover most common component types.
3020

3121
```jsx
3222
import { componentTypes } from '@data-driven-forms/react-form-renderer';
@@ -48,6 +38,43 @@ const componentTypes = {
4838
}
4939
```
5040

41+
You have two options how to connect your component to the form state.
42+
43+
### useFieldApi
44+
45+
First, you can use `useFieldApi` hook.
46+
47+
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]
48+
49+
```jsx
50+
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
51+
52+
...
53+
54+
const { input, isDisabled, label, helperText, description, meta } = useFieldApi(props);
55+
```
56+
57+
### FieldProvider
58+
59+
Or you can import `FieldProvider` component from Data Driven Forms. This component needs to obtain `render` or `component` prop.
60+
61+
62+
```jsx
63+
import { FieldProvider } from '@data-driven-forms/react-form-renderer'
64+
65+
<FielProvider component={TextField}>
66+
67+
// or
68+
69+
<FielProvider render={({input, meta, ...props}) => <TextField {...props} input={input} meta={meta}>}>
70+
```
71+
72+
## Example
73+
74+
Below, you can see an basic implementation of custom component mapper:
75+
<br />
76+
77+
5178
<RawComponent source="component-mapper/form-fields-mapper" />
5279
</Grid>
5380
<Grid item xs={false} md={2}>

packages/react-renderer-demo/src/app/pages/renderer/field-array.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import ListOfContents from '../../src/helpers/list-of-contents';
1010

1111
# Field Array Provider
1212

13-
Simillarly to <RouterLink href='/renderer/field-provider'><Link href='/renderer/field-provider'>FieldProvider</Link></RouterLink> Data driven forms provide an option how to inlude <a href='https://github.com/final-form/react-final-form-arrays'>React Final Form Arrays</a> in your form.
13+
Simillarly to <RouterLink href='/renderer/field-provider'><Link href='/renderer/field-provider'>FieldProvider</Link></RouterLink> Data driven forms provide an option how to inlude <a href='https://github.com/final-form/react-final-form-arrays'>React Final Form Arrays</a> in your form. Please visit their documentation to learn about functionality.
1414

15-
<b>Please visit their documentation to learn about functionality.</b>
15+
## Using FieldArray
1616

17-
## Using FieldArrayProvider
17+
You have to just import `FieldArray` from Data Driven Forms.
1818

19-
Component mapped to `componentTypes.FIELD_ARRAY` (`field_array`) receives as a prop `FieldArrayProvider`. You can wrap your component into it and they you have an access to all functionallity.
19+
```jsx
20+
import { FieldArray } from '@data-driven-forms/react-form-renderer';
21+
```
2022

2123
<RawComponent source="field-array/form-fields-mapper" />
2224

packages/react-renderer-demo/src/app/pages/renderer/field-provider.md

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,51 @@ import ListOfContents from '../../src/helpers/list-of-contents';
99
<Grid container item>
1010
<Grid item xs={12} md={10}>
1111

12-
# Field Provider
13-
14-
## Custom components
12+
# Custom components
1513

1614
React form renderer is using [react-final-form](https://github.com/final-form/react-final-form) for form state management.
1715
Most of its features are not directly available for consistency and performance reasons. If you want to create any custom
18-
components, you can access these features via `FieldProvider` prop.
16+
components, you can access these features via `FieldProvider` component or `useFieldApi` hook.
1917

20-
FieldProvider is a wrapper component around standard
21-
[react-final-form Field component](https://final-form.org/docs/react-final-form/api/Field)
22-
which adds additional methods that will help you to control your form state.
18+
`FieldProvider` is a wrapper using `useFieldApi` to get the access to the form state.
2319

24-
# Accessing FieldProvider
20+
`useFieldApi` is a wrapper around [React Final Form useField hook](https://final-form.org/docs/react-final-form/api/useField).
2521

26-
To use Fieldprovider, you first need to register a component to your component mapper.
2722
You can read more about that in <RouterNav href="/renderer/component-mapping"><Link href="/renderer/component-mapping">Component mapping</Link></RouterNav>.
2823

29-
Each component will receive FieldProvider as a prop. Be aware that pre-defined component types are
30-
automatically wrapped in FieldProvider. This is done to make it easier to create component mappers for
31-
standard form components. List of standard components is available <RouterNav href="/renderer/component-api"><Link href="/renderer/component-api">here</Link></RouterNav>.
32-
33-
# Using FieldProvider
24+
## Implementation of component
3425

35-
## Register component
26+
Next example shows simple input field with label and error message.
3627

3728
```jsx
38-
import NewComponent from './new-component'
39-
40-
const componentMapper = {
41-
'new-component': NewComponent
29+
import React from 'react';
30+
import { useFieldApi } from '@data-driven-forms/react-form-renderer'
31+
32+
const NewComponent = (props) => {
33+
const { input, meta } = useFieldApi(props);
34+
35+
return (
36+
<div>
37+
<label>{props.label}</label>
38+
<input {...input} />
39+
{meta.error && <label>{meta.error}</label>}
40+
</div>
41+
)
4242
}
43+
44+
export default NewComponent
4345
```
4446

45-
## Implementation of component
47+
## Register component
4648

47-
Next example shows simple input field with label and error message.
49+
To be able to use your component in the schema, you first need to register the component in your component mapper.
4850

4951
```jsx
50-
import React from 'react';
51-
52-
const NewComponent = ({ FieldProvider, formOptions, name, ...rest }) => (
53-
<div>
54-
<FieldProvider {...rest} name={name}>
55-
{({ input, meta, ...props }) => {
56-
return (
57-
<div>
58-
<label>{props.label}</label>
59-
<input {...input} />
60-
{meta.error && <label>{meta.error}</label>}
61-
</div>
62-
)
63-
}}
64-
</FieldProvider>
65-
</div>
66-
)
52+
import NewComponent from './new-component'
6753

68-
export default NewComponent
54+
const componentMapper = {
55+
'new-component': NewComponent
56+
}
6957
```
7058

7159
# What are input and meta?
@@ -100,11 +88,23 @@ Meta is a object which contains meta information about field with given name. Th
10088
}
10189
```
10290

103-
# FormOptions
91+
# useFormApi - formOptions
10492

105-
In addition to FieldProvider, every component will also receive prop `formOptions`.
10693
This property contains a number of useful methods and attributes that will give you additional level of control
107-
and informations about the formState.
94+
and information about the formState.
95+
96+
You can access it from every component by using `useFormApi` hook.
97+
98+
```jsx
99+
import { useFormApi } from '@data-driven-forms/react-form-renderer';
100+
101+
const Component = (props) => {
102+
const formOptions = useFormApi();
103+
...
104+
}
105+
```
106+
107+
This hook returns object containing following information:
108108

109109
```jsx
110110
{
@@ -120,9 +120,29 @@ and informations about the formState.
120120
}
121121
```
122122

123-
# FormSpy provider
123+
# FormSpy
124+
125+
[FormSpy](https://final-form.org/docs/react-final-form/api/FormSpy) is exported via Data Driven Forms.
124126

125-
Every component also receives component `FormSpyProvider` as a prop. You can find more info <a href="https://final-form.org/docs/react-final-form/api/FormSpy" rel="noopener noreferrer" target="_blank">here!</a>
127+
```jsx
128+
import { FormSpy } from '@data-driven-forms/react-form-renderer';
129+
```
130+
131+
# FieldArray
132+
133+
[React Final Form Array](https://github.com/final-form/react-final-form-arrays) is exported via Data Driven Forms.
134+
135+
```jsx
136+
import { FieldArray } from '@data-driven-forms/react-form-renderer';
137+
```
138+
139+
# Form
140+
141+
For testing purposes, you can also import React Final Form's `Form` component.
142+
143+
```jsx
144+
import { Form } from '@data-driven-forms/react-form-renderer';
145+
```
126146

127147
</Grid>
128148
<Grid item xs={false} md={2}>

packages/react-renderer-demo/src/app/pages/renderer/form-controls.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)