@@ -4,7 +4,6 @@ import * as React from 'react';
44import { ConfigInput , DynamicFormValue } from '../../../common/types' ;
55import { editorCn } from '../../utils/cn' ;
66
7- import './DynamicForm.scss' ;
87import ArrayDynamicField from './Fields/Array/Array' ;
98import BooleanDynamicField from './Fields/Boolean/Boolean' ;
109import NumberDynamicField from './Fields/Number/Number' ;
@@ -16,18 +15,35 @@ import TextDynamicField from './Fields/Text/Text';
1615import TextAreaDynamicField from './Fields/TextArea/TextArea' ;
1716import { getContent , getFullPath } from './utils' ;
1817
18+ import './DynamicForm.scss' ;
19+
1920const b = editorCn ( 'dynamic-form' ) ;
2021
2122interface DynamicFormProps {
2223 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 ;
2527 className ?: string ;
2628}
2729
28- const DynamicForm = ( { blockConfig, onUpdate, contentConfig} : DynamicFormProps ) => {
30+ const DynamicForm = ( { blockConfig, onUpdateByKey , onUpdate, contentConfig} : DynamicFormProps ) => {
2931 const inputs = blockConfig ;
3032
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+
3147 const getData = React . useCallback (
3248 ( variable : string ) => {
3349 if ( variable . startsWith ( 'block.' ) ) {
@@ -87,19 +103,19 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
87103
88104 // Text, Select, Boolean and etc
89105 const onSimpleDynamicFieldUpdate = ( value : DynamicFormValue ) => {
90- onUpdate ( fieldPath , value ) ;
106+ onDataUpdate ( fieldPath , value ) ;
91107 } ;
92108
93109 // Array and Objects
94110 const onComplexDynamicFieldUpdate = ( key : string , value : DynamicFormValue ) => {
95- onUpdate ( getFullPath ( fieldPath , key ) , value ) ;
111+ onDataUpdate ( getFullPath ( fieldPath , key ) , value ) ;
96112 } ;
97113
98114 switch ( input . type ) {
99115 case 'text' : {
100116 return (
101117 < TextDynamicField
102- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
118+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
103119 title = { input . title }
104120 value = { fieldValue }
105121 onUpdate = { onSimpleDynamicFieldUpdate }
@@ -109,7 +125,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
109125 case 'boolean' : {
110126 return (
111127 < BooleanDynamicField
112- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
128+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
113129 title = { input . title }
114130 value = { fieldValue }
115131 onUpdate = { onSimpleDynamicFieldUpdate }
@@ -119,7 +135,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
119135 case 'textarea' : {
120136 return (
121137 < TextAreaDynamicField
122- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
138+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
123139 title = { input . title }
124140 value = { fieldValue }
125141 onUpdate = { onSimpleDynamicFieldUpdate }
@@ -129,7 +145,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
129145 case 'select' : {
130146 return (
131147 < SelectDynamicField
132- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
148+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
133149 input = { input }
134150 value = { fieldValue }
135151 onUpdate = { onSimpleDynamicFieldUpdate }
@@ -139,7 +155,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
139155 case 'number' : {
140156 return (
141157 < NumberDynamicField
142- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
158+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
143159 title = { input . title }
144160 value = { fieldValue }
145161 onUpdate = { onSimpleDynamicFieldUpdate }
@@ -153,7 +169,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
153169
154170 return (
155171 < ObjectDynamicField
156- onRefresh = { ( value ) => onUpdate ( fieldPath , value ) }
172+ onRefresh = { ( value ) => onDataUpdate ( fieldPath , value ) }
157173 blockConfig = { input . properties }
158174 title = { input . title }
159175 value = { fieldValue }
@@ -202,7 +218,7 @@ const DynamicForm = ({blockConfig, onUpdate, contentConfig}: DynamicFormProps) =
202218 }
203219 }
204220 } ,
205- [ contentConfig , decide , onUpdate ] ,
221+ [ contentConfig , decide , onDataUpdate ] ,
206222 ) ;
207223
208224 const sortedInputs = inputs . sort ( ( x , y ) => {
0 commit comments