Skip to content

Commit ff79995

Browse files
committed
Add docs for initializeOnMount
1 parent 335b24f commit ff79995

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Grid from '@material-ui/core/Grid'
2+
import RouterLink from 'next/link';
3+
import Link from '@material-ui/core/Link';
4+
import RawComponent from '@docs/raw-component';
5+
6+
import ListOfContents from '../../src/helpers/list-of-contents';
7+
8+
<Grid container item>
9+
<Grid item xs={12} md={10}>
10+
11+
# Initialize On Mount
12+
13+
Data Driven Forms provides a way how you can easily initialized a field when the field is mounted (re-mounted).
14+
15+
Just pass a `initializeOnMount` prop and set it to `true`.
16+
17+
The field will use the `initialValue` set in the schema (<RouterLink href="/renderer/component-api#formgroupwrappedcomponents"><Link>initialValue</Link></RouterLink>) or in the renderer (<RouterLink href="/renderer/renderer-api#optionalprops"><Link>initialValues</Link></RouterLink>).
18+
19+
`initialValue` has higher priority than a value from `initialValues`.
20+
21+
## Example
22+
23+
24+
```jsx
25+
{
26+
component: componentTypes.TEXT_FIELD,
27+
name: 'name',
28+
initializeOnMount: true,
29+
initialValue: 'this value will be set'
30+
}
31+
```
32+
33+
## When to use it?
34+
35+
This feature comes handy if you need change a value when an user traverses a form, which shows and hides fields, and the value is not set by the user. Very useful case is used it wizard forms, where you can set different value for the same input according the way the user went in the wizard form by using this option combined with <RouterLink href="/renderer/component-api#commonpropsforallformfields"><Link>hideField</Link></RouterLink> prop.
36+
37+
<RawComponent source="initialize-mount" />
38+
39+
## Clear the value
40+
41+
If you need clear the value after unmounting, you can do it by using <RouterLink href="/renderer/unmounting"><Link>clearOnUnmount</Link></RouterLink>.
42+
43+
</Grid>
44+
<Grid item xs={false} md={2}>
45+
<ListOfContents file="renderer/initialize-mount" />
46+
</Grid>
47+
</Grid>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export const docs = [{
2222
}, {
2323
component: 'unmounting',
2424
linkText: 'Clear On Unmount',
25+
}, {
26+
component: 'initialize-mount',
27+
linkText: 'Initialize On Mount',
2528
}, {
2629
component: 'validators',
2730
linkText: 'Validators',
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React, { useState } from 'react';
2+
import FormRenderer, { componentTypes } from '@data-driven-forms/react-form-renderer';
3+
import { layoutMapper, formFieldsMapper } from '@data-driven-forms/pf4-component-mapper';
4+
import { Title } from '@patternfly/react-core';
5+
6+
const schema = {
7+
fields: [{
8+
component: componentTypes.WIZARD,
9+
name: 'wizard',
10+
fields: [
11+
{
12+
title: 'Choose your way',
13+
name: 'step-1',
14+
stepKey: 1,
15+
nextStep: {
16+
when: 'selection',
17+
stepMapper: {
18+
'way-1': 'way-1',
19+
'way-2': 'way-2',
20+
},
21+
},
22+
fields: [
23+
{
24+
component: componentTypes.SELECT,
25+
name: 'selection',
26+
label: 'Select your way',
27+
isRequired: true,
28+
options: [
29+
{ label: 'Please choose your way' },
30+
{ value: 'way-1', label: 'way-1' },
31+
{ value: 'way-2', label: 'way-2' },
32+
],
33+
validate: [{ type: 'required-validator' }],
34+
},
35+
],
36+
},
37+
{
38+
title: 'Way 1',
39+
stepKey: 'way-1',
40+
fields: [
41+
{
42+
component: componentTypes.TEXT_FIELD,
43+
initializeOnMount: true,
44+
hideField: true,
45+
name: 'chosen-way',
46+
initialValue: 'User chose the first way',
47+
},
48+
],
49+
},
50+
{
51+
title: 'Way 2',
52+
stepKey: 'way-2',
53+
fields: [
54+
{
55+
component: componentTypes.TEXT_FIELD,
56+
initializeOnMount: true,
57+
hideField: true,
58+
name: 'chosen-way',
59+
initialValue: 'User chose the second way',
60+
},
61+
],
62+
},
63+
]},
64+
],
65+
};
66+
67+
const InitializeOnMountWizardExample = () => {
68+
const [ values, setValues ] = useState({});
69+
return (
70+
<div className="pf4">
71+
<FormRenderer
72+
layoutMapper={ layoutMapper }
73+
formFieldsMapper={ formFieldsMapper }
74+
schema={ schema }
75+
onSubmit={ console.log }
76+
onStateUpdate={ ({ values }) => setValues(values) }
77+
showFormControls={ false }
78+
/>
79+
<div style={{ marginTop: 16 }}>
80+
<Title size="md">Form values</Title>
81+
<pre>
82+
{ JSON.stringify(values, null, 2) }
83+
</pre>
84+
</div>
85+
</div>
86+
);};
87+
88+
export default InitializeOnMountWizardExample;

0 commit comments

Comments
 (0)