File tree Expand file tree Collapse file tree 4 files changed +149
-0
lines changed
packages/react-renderer-demo/src
components/navigation/schemas
examples/components/examples Expand file tree Collapse file tree 4 files changed +149
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ const customExamplesSchema = [
26
26
{
27
27
component : 'form-level-errors' ,
28
28
linkText : 'Form level errors'
29
+ } ,
30
+ {
31
+ component : 'value-listener' ,
32
+ linkText : 'Value listener'
29
33
}
30
34
] ;
31
35
Original file line number Diff line number Diff line change
1
+ import React , { useEffect } from 'react' ;
2
+ import FormRenderer from '@data-driven-forms/react-form-renderer/form-renderer' ;
3
+ import componentTypes from '@data-driven-forms/react-form-renderer/component-types' ;
4
+ import useFormApi from '@data-driven-forms/react-form-renderer/use-form-api' ;
5
+
6
+ import FormTemplate from '@data-driven-forms/mui-component-mapper/form-template' ;
7
+ import TextField from '@data-driven-forms/mui-component-mapper/text-field' ;
8
+
9
+ const FieldListener = ( ) => {
10
+ const { getState, change } = useFormApi ( ) ;
11
+
12
+ const { original } = getState ( ) . values ;
13
+
14
+ useEffect ( ( ) => {
15
+ if ( original ) {
16
+ change ( 'updated' , `${ original } ${ new Date ( ) . toGMTString ( ) } ` ) ;
17
+ }
18
+ } , [ original ] ) ;
19
+
20
+ return null ;
21
+ } ;
22
+
23
+ const schema = {
24
+ fields : [
25
+ {
26
+ component : componentTypes . TEXT_FIELD ,
27
+ name : 'original' ,
28
+ label : 'Change me'
29
+ } ,
30
+ {
31
+ component : componentTypes . TEXT_FIELD ,
32
+ name : 'updated' ,
33
+ label : 'Updated value' ,
34
+ isReadOnly : true
35
+ } ,
36
+ {
37
+ component : 'field-listener' ,
38
+ name : 'listener' ,
39
+ hideField : true
40
+ }
41
+ ]
42
+ } ;
43
+
44
+ const ValueListener = ( ) => (
45
+ < FormRenderer
46
+ schema = { schema }
47
+ componentMapper = { {
48
+ [ componentTypes . TEXT_FIELD ] : TextField ,
49
+ 'field-listener' : FieldListener
50
+ } }
51
+ FormTemplate = { FormTemplate }
52
+ onSubmit = { console . log }
53
+ subscription = { { values : true } }
54
+ />
55
+ ) ;
56
+
57
+ export default ValueListener ;
Original file line number Diff line number Diff line change
1
+ import React , { useEffect } from 'react' ;
2
+ import FormRenderer from '@data-driven-forms/react-form-renderer/form-renderer' ;
3
+ import componentTypes from '@data-driven-forms/react-form-renderer/component-types' ;
4
+ import FormSpy from '@data-driven-forms/react-form-renderer/form-spy' ;
5
+ import useFormApi from '@data-driven-forms/react-form-renderer/use-form-api' ;
6
+
7
+ import FormTemplate from '@data-driven-forms/mui-component-mapper/form-template' ;
8
+ import TextField from '@data-driven-forms/mui-component-mapper/text-field' ;
9
+
10
+ const FieldListener = ( ) => {
11
+ const { getState, change } = useFormApi ( ) ;
12
+
13
+ const { original } = getState ( ) . values ;
14
+
15
+ useEffect ( ( ) => {
16
+ if ( original ) {
17
+ change ( 'updated' , `${ original } ${ new Date ( ) . toGMTString ( ) } ` ) ;
18
+ }
19
+ } , [ original ] ) ;
20
+
21
+ return null ;
22
+ } ;
23
+
24
+ const FieldListenerWrapper = ( ) => < FormSpy subcription = { { values : true } } > { ( ) => < FieldListener /> } </ FormSpy > ;
25
+
26
+ const schema = {
27
+ fields : [
28
+ {
29
+ component : componentTypes . TEXT_FIELD ,
30
+ name : 'original' ,
31
+ label : 'Change me'
32
+ } ,
33
+ {
34
+ component : componentTypes . TEXT_FIELD ,
35
+ name : 'updated' ,
36
+ label : 'Updated value' ,
37
+ isReadOnly : true
38
+ } ,
39
+ {
40
+ component : 'field-listener' ,
41
+ name : 'listener' ,
42
+ hideField : true
43
+ }
44
+ ]
45
+ } ;
46
+
47
+ const ValueListener = ( ) => (
48
+ < FormRenderer
49
+ schema = { schema }
50
+ componentMapper = { {
51
+ [ componentTypes . TEXT_FIELD ] : TextField ,
52
+ 'field-listener' : FieldListenerWrapper
53
+ } }
54
+ FormTemplate = { FormTemplate }
55
+ onSubmit = { console . log }
56
+ />
57
+ ) ;
58
+
59
+ export default ValueListener ;
Original file line number Diff line number Diff line change
1
+ import DocPage from '@docs/doc-page ';
2
+ import CodeExample from '@docs/code-example ';
3
+
4
+ <DocPage >
5
+
6
+
7
+ # Value listener
8
+
9
+ This example shows how to update a value of one field according to a value of others without using the set [ condition] ( /schema/condition-set ) .
10
+
11
+ ## Description
12
+
13
+ Basically you have to just a set up a new component, that will listen to the form state and updates the intended fields.
14
+
15
+ ** FormSpy** with a correct subscription ensures that the component will be always updated. You can also set the [ subcription] ( /components/renderer#subscription ) on the form level in the form renderer.
16
+
17
+ ** hideField** ensures that the listener component is not visible in the dom and it won't affect the form layout.
18
+
19
+ ## FormSpy example
20
+
21
+ <CodeExample source =" components/examples/value-listener " mode =" preview " />
22
+
23
+ ## Form subscription example
24
+
25
+ ** Global subscription can affect the form performance.** It's recommended to use it in smaller forms with less form fields.
26
+
27
+ <CodeExample source =" components/examples/value-listener " mode =" preview " />
28
+
29
+ </DocPage >
You can’t perform that action at this time.
0 commit comments