Skip to content

Commit 0740fd5

Browse files
committed
Add action mapper documentation
1 parent 5ce5c58 commit 0740fd5

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed
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/renderer-api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ Form Renderer provides a lot of customization via props.
1717
|Prop|Type|Description|
1818
|----|:--:|----------:|
1919
|<RouterLink href="/renderer/component-mapping"><Link href="/renderer/component-mapping">componentMapper</Link></RouterLink>|object|Defines types of form field components. Field components can change the state of the form.|
20-
|<RouterLink href="/renderer/component-mapping"><Link href="/renderer/component-mapping">FormTemplate</Link></RouterLink>|Component|Components which defines a template of the form. This component receives two props from the renderer: `formFields` and `schema`. `formFields` is the content of the form. You should wrap this content into your `<form>` component and add form buttons.|
20+
|<RouterLink href="/renderer/form-template"><Link href="/renderer/component-mapping">FormTemplate</Link></RouterLink>|Component|Components which defines a template of the form. This component receives two props from the renderer: `formFields` and `schema`. `formFields` is the content of the form. You should wrap this content into your `<form>` component and add form buttons.|
2121
|onSubmit|func|A submit callback which receives two arguments: `values` and `formApi`.|
2222
|schema|object|A schema which defines structure of the form.|
2323

2424
## Optional props
2525

2626
|Prop|Type|Description|Default|
2727
|----|:--:|----------:|------:|
28+
|<RouterLink href="/renderer/action-mapper"><Link href="/renderer/action-mapper">actionMapper</Link></RouterLink>|object|Action mapper allows to map props to functions.||
2829
|<RouterLink href="/renderer/unmounting"><Link href="/renderer/unmounting">clearOnUnmount</Link></RouterLink>|bool|Will clear values of unmounted components. You can also set this to specific component in the form schema.|false|
2930
|<RouterLink href="/renderer/cleared-value"><Link>clearedValue</Link></RouterLink>|any|Value that will be set to field with **initialValue** after deleting it. Useful for forms while editing.|undefined|
3031
|onReset|func|A reset callback. You don't need to manually clear the form values!||

packages/react-renderer-demo/src/app/src/components/navigation/documentation-pages.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export const docs = [
2323
component: 'component-api',
2424
linkText: 'Components API'
2525
},
26+
{
27+
component: 'form-template',
28+
linkText: 'Form Template'
29+
},
2630
{
2731
component: 'condition',
2832
linkText: 'Conditional fields'
@@ -52,11 +56,11 @@ export const docs = [
5256
linkText: 'Custom components'
5357
},
5458
{
55-
component: 'field-array',
56-
linkText: 'FieldArray Provider'
59+
component: 'action-mapper',
60+
linkText: 'Action mapper'
5761
},
5862
{
59-
component: 'form-template',
60-
linkText: 'Form Template'
63+
component: 'field-array',
64+
linkText: 'FieldArray Provider'
6165
}
6266
];

0 commit comments

Comments
 (0)