Skip to content

Commit 5631b2c

Browse files
committed
Add documentation page for resolve props
1 parent bd2541f commit 5631b2c

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

packages/react-renderer-demo/src/components/navigation/schemas/schema.schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const schemaNav = [
2828
component: 'constants',
2929
linkText: 'Constants'
3030
},
31+
{
32+
component: 'resolve-props',
33+
linkText: 'Resolve props'
34+
},
3135
{
3236
subHeader: true,
3337
title: 'Validation',
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/mui-component-mapper/dist/cjs/text-field';
6+
import FormTemplate from '@data-driven-forms/mui-component-mapper/dist/cjs/form-template';
7+
8+
const schema = {
9+
fields: [
10+
{
11+
component: componentTypes.TEXT_FIELD,
12+
name: 'name',
13+
label: 'Validate me',
14+
isRequired: true,
15+
validate: [{ type: 'required' }],
16+
helperText: 'This field will indicate successful validation after onBlur event',
17+
resolveProps: (props, { meta, input }, formOptions) => {
18+
if (meta.valid && meta.touched) {
19+
return {
20+
helperText: 'Validated',
21+
style: {
22+
background: 'greenyellow'
23+
}
24+
};
25+
}
26+
27+
return {};
28+
}
29+
}
30+
]
31+
};
32+
33+
const componentMapper = {
34+
[componentTypes.TEXT_FIELD]: TextField
35+
};
36+
37+
const ResolveProps = () => <FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />;
38+
39+
export default ResolveProps;

packages/react-renderer-demo/src/pages/schema/introduction.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Other attribues, such as title or description, can be used in [form templates](/
3131
dataType: 'string',
3232
hideField: true,
3333
initializeOnMount: true,
34-
initialValue: 'default-login'
34+
initialValue: 'default-login',
35+
resolveProps: (props) => ({ label: props.label || 'default label' }),
3536
validate: [ ... ],
3637
// + component specific attributes
3738
...componentSpecificAttributes
@@ -155,6 +156,16 @@ Sets an initial value of the field. Read more [here](https://final-form.org/docs
155156

156157
---
157158

159+
### resolveProps
160+
161+
*function (props, {meta, input}, formOptions) => props*
162+
163+
**Only applicable for fields connected to the form state.**
164+
165+
A function allowing to compute field properties from the current state of the field. Read more [here](/schema/resolve-props).
166+
167+
---
168+
158169
### validate
159170

160171
*array*
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import DocPage from '@docs/doc-page';
2+
import InputMeta from '../input-meta.md';
3+
import CodeExample from '@docs/code-example';
4+
5+
<DocPage>
6+
7+
# Resolve props
8+
9+
*function (props, {meta, input}, formOptions) => props*
10+
11+
**Only applicable for fields connected to the form state.**
12+
13+
A function allowing to compute field properties from the current state of the field.
14+
15+
## Schema
16+
17+
```jsx
18+
const schema = {
19+
fields: [{
20+
component: 'text-field',
21+
resolveProps: (props, {meta, input}, formOptions) => ({ helperText: input.value ? 'You set a value' : 'No value' })
22+
}]
23+
}
24+
```
25+
26+
## Arguments
27+
28+
### Props
29+
30+
All field props passed in the schema.
31+
32+
### Meta and input
33+
34+
<InputMeta />
35+
36+
### FormOptions
37+
38+
Object containing access to the form state. Read more [here](/hooks/use-form-api).
39+
40+
## Rules
41+
42+
I. `resolveProps` cannot return `actions`. You can access `actions`'s code in the `resolveProps` prop if you need it.
43+
44+
II. Do not use `async` functions to get the props.
45+
46+
III. Do not trigger any side effects, as it could introduce bugs.
47+
48+
IV. `resolveProps` are merged together with following priority: `actions.resolveProps` (highest) > `field.resolveProps` > `mapper.resolveProps` (lowest)
49+
50+
## Global resolveProps
51+
52+
You can modify behavior for all components of the same type in your `componentMapper` via [global component props](/mappers/global-component-props).
53+
54+
```jsx
55+
const componentMapper = {
56+
'text-field': {
57+
component: TextField,
58+
resolveProps: () => { ... }
59+
}
60+
}
61+
```
62+
63+
## Change props according to state of other components
64+
65+
You can get states of all other fields in the form via functions from `formOptions`. Don't forget to set the right [subscription](/components/renderer#optionalprops) to trigger `resolveProps` functions from changing other fields.
66+
67+
## Example
68+
69+
Following example shows how can be a behavior of components changed using `resolveProps`. In this example, the component will have different color and helper text after it is validated.
70+
71+
<CodeExample mode="preview" source="components/resolve-props" />
72+
73+
</DocPage>

0 commit comments

Comments
 (0)