Skip to content

Commit af07b96

Browse files
committed
Add action mapper example
1 parent 6694711 commit af07b96

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

packages/react-renderer-demo/src/app/pages/renderer/action-mapper.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Grid from '@material-ui/core/Grid'
22
import ListOfContents from '../../src/helpers/list-of-contents';
3+
import RawComponent from '@docs/raw-component';
4+
35

46
<Grid container item>
57
<Grid item xs={12} md={10}>
@@ -68,6 +70,8 @@ Then, in your schema you can map `translateLabel` action to a prop:
6870
}
6971
```
7072

73+
<RawComponent source="action-mapper" />
74+
7175
</Grid>
7276
<Grid item xs={false} md={2}>
7377
<ListOfContents file="renderer/action-mapper" />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import FormRenderer from '@data-driven-forms/react-form-renderer/dist/cjs/form-renderer';
3+
import componentTypes from '@data-driven-forms/react-form-renderer/dist/cjs/component-types';
4+
5+
import TextField from '@data-driven-forms/pf4-component-mapper/dist/cjs/text-field';
6+
import FormTemplate from '@data-driven-forms/pf4-component-mapper/dist/cjs/form-template';
7+
8+
const translateLabel = (id, locale = 'en') =>
9+
({
10+
en: {
11+
name: 'User name',
12+
password: 'Password'
13+
},
14+
jp: {
15+
name: '名前',
16+
password: 'パスワード'
17+
}
18+
}[locale][id]);
19+
20+
const schema = {
21+
title: 'Action Mapper example (translated strings)',
22+
fields: [
23+
{
24+
component: componentTypes.TEXT_FIELD,
25+
name: 'name',
26+
actions: {
27+
label: ['translateLabel', 'name', 'jp']
28+
}
29+
},
30+
{
31+
component: componentTypes.TEXT_FIELD,
32+
name: 'password',
33+
actions: {
34+
label: ['translateLabel', 'password', 'jp']
35+
}
36+
}
37+
]
38+
};
39+
40+
const componentMapper = {
41+
[componentTypes.TEXT_FIELD]: TextField
42+
};
43+
44+
const actionMapper = {
45+
translateLabel
46+
};
47+
48+
const ActionMapper = () => (
49+
<div className="pf4">
50+
<FormRenderer FormTemplate={FormTemplate} actionMapper={actionMapper} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
51+
</div>
52+
);
53+
54+
export default ActionMapper;

0 commit comments

Comments
 (0)