@@ -4,7 +4,6 @@ import * as React from 'react';
4
4
import { ConfigInput , DynamicFormValue } from '../../../common/types' ;
5
5
import { editorCn } from '../../utils/cn' ;
6
6
7
- import './DynamicForm.scss' ;
8
7
import ArrayDynamicField from './Fields/Array/Array' ;
9
8
import BooleanDynamicField from './Fields/Boolean/Boolean' ;
10
9
import NumberDynamicField from './Fields/Number/Number' ;
@@ -16,18 +15,35 @@ import TextDynamicField from './Fields/Text/Text';
16
15
import TextAreaDynamicField from './Fields/TextArea/TextArea' ;
17
16
import { getContent , getFullPath } from './utils' ;
18
17
18
+ import './DynamicForm.scss' ;
19
+
19
20
const b = editorCn ( 'dynamic-form' ) ;
20
21
21
22
interface DynamicFormProps {
22
23
blockConfig : Array < ConfigInput > ;
23
- contentConfig ?: DynamicFormValue ;
24
- onUpdate : ( key : string , value : DynamicFormValue ) => void ;
24
+ contentConfig ?: object ;
25
+ onUpdateByKey ?: ( key : string , value : DynamicFormValue ) => void ;
26
+ onUpdate ?: ( value : object ) => void ;
25
27
className ?: string ;
26
28
}
27
29
28
- const DynamicForm = ( { blockConfig, onUpdate, contentConfig} : DynamicFormProps ) => {
30
+ const DynamicForm = ( { blockConfig, onUpdateByKey , onUpdate, contentConfig} : DynamicFormProps ) => {
29
31
const inputs = blockConfig ;
30
32
33
+ const onDataUpdate = React . useCallback (
34
+ ( key : string , value : DynamicFormValue ) => {
35
+ if ( onUpdateByKey ) {
36
+ onUpdateByKey ( key , value ) ;
37
+ }
38
+ if ( onUpdate && contentConfig ) {
39
+ const newContentConfig = _ . cloneDeep ( contentConfig ) ;
40
+ _ . set ( newContentConfig , key , value ) ;
41
+ onUpdate ( newContentConfig ) ;
42
+ }
43
+ } ,
44
+ [ onUpdateByKey , onUpdate , contentConfig ] ,
45
+ ) ;
46
+
31
47
const getData = React . useCallback (
32
48
( variable : string ) => {
33
49
if ( variable . startsWith ( 'block.' ) ) {
@@ -87,19 +103,19 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
87
103
88
104
// Text, Select, Boolean and etc
89
105
const onSimpleDynamicFieldUpdate = ( value : DynamicFormValue ) => {
90
- onUpdate ( fieldPath , value ) ;
106
+ onDataUpdate ( fieldPath , value ) ;
91
107
} ;
92
108
93
109
// Array and Objects
94
110
const onComplexDynamicFieldUpdate = ( key : string , value : DynamicFormValue ) => {
95
- onUpdate ( getFullPath ( fieldPath , key ) , value ) ;
111
+ onDataUpdate ( getFullPath ( fieldPath , key ) , value ) ;
96
112
} ;
97
113
98
114
switch ( input . type ) {
99
115
case 'text' : {
100
116
return (
101
117
< TextDynamicField
102
- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
118
+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
103
119
title = { input . title }
104
120
value = { fieldValue }
105
121
onUpdate = { onSimpleDynamicFieldUpdate }
@@ -109,7 +125,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
109
125
case 'boolean' : {
110
126
return (
111
127
< BooleanDynamicField
112
- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
128
+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
113
129
title = { input . title }
114
130
value = { fieldValue }
115
131
onUpdate = { onSimpleDynamicFieldUpdate }
@@ -119,7 +135,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
119
135
case 'textarea' : {
120
136
return (
121
137
< TextAreaDynamicField
122
- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
138
+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
123
139
title = { input . title }
124
140
value = { fieldValue }
125
141
onUpdate = { onSimpleDynamicFieldUpdate }
@@ -129,7 +145,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
129
145
case 'select' : {
130
146
return (
131
147
< SelectDynamicField
132
- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
148
+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
133
149
input = { input }
134
150
value = { fieldValue }
135
151
onUpdate = { onSimpleDynamicFieldUpdate }
@@ -139,7 +155,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
139
155
case 'number' : {
140
156
return (
141
157
< NumberDynamicField
142
- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
158
+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
143
159
title = { input . title }
144
160
value = { fieldValue }
145
161
onUpdate = { onSimpleDynamicFieldUpdate }
@@ -153,7 +169,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
153
169
154
170
return (
155
171
< ObjectDynamicField
156
- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
172
+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
157
173
blockConfig = { input . properties }
158
174
title = { input . title }
159
175
value = { fieldValue }
@@ -202,7 +218,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
202
218
}
203
219
}
204
220
} ,
205
- [ contentConfig , decide , onUpdate ] ,
221
+ [ contentConfig , decide , onDataUpdate ] ,
206
222
) ;
207
223
208
224
const sortedInputs = inputs . sort ( ( x , y ) => {
0 commit comments