@@ -9,7 +9,6 @@ import React, {
9
9
createContext ,
10
10
useCallback ,
11
11
useEffect ,
12
- useMemo ,
13
12
useRef ,
14
13
useState ,
15
14
} from 'react' ;
@@ -174,17 +173,13 @@ function PydanticFormContextProvider({
174
173
componentMatcher ,
175
174
) ;
176
175
177
- const initialData = useMemo (
178
- ( ) =>
179
- getFormValuesFromFieldOrLabels (
180
- pydanticFormSchema ,
181
- {
182
- ...formLabels ?. data ,
183
- ...customData ,
184
- } ,
185
- componentMatcher ,
186
- ) ,
187
- [ componentMatcher , customData , formLabels ?. data , pydanticFormSchema ] ,
176
+ const initialData = getFormValuesFromFieldOrLabels (
177
+ pydanticFormSchema ,
178
+ {
179
+ ...formLabels ?. data ,
180
+ ...customData ,
181
+ } ,
182
+ componentMatcher ,
188
183
) ;
189
184
190
185
// initialize the react-hook-form
@@ -204,6 +199,14 @@ function PydanticFormContextProvider({
204
199
return ( ) => sub . unsubscribe ( ) ;
205
200
} , [ rhf , onChange ] ) ;
206
201
202
+ const resetFormData = useCallback ( ( ) => {
203
+ if ( ! pydanticFormSchema ) {
204
+ return ;
205
+ }
206
+
207
+ rhf . reset ( ) ;
208
+ } , [ pydanticFormSchema , rhf ] ) ;
209
+
207
210
rhfRef . current = rhf ;
208
211
209
212
/* TODO: Reimplement
@@ -236,12 +239,12 @@ function PydanticFormContextProvider({
236
239
}
237
240
238
241
setFormInputHistory ( new Map < string , object > ( ) ) ;
239
- rhf . reset ( initialData ) ;
242
+ resetFormData ( ) ;
240
243
} , [
241
244
apiResponse ,
242
- initialData ,
243
245
isFullFilled ,
244
246
onSuccess ,
247
+ resetFormData ,
245
248
rhf ,
246
249
skipSuccessNotice ,
247
250
] ) ;
@@ -257,6 +260,7 @@ function PydanticFormContextProvider({
257
260
258
261
// when we receive a form from the JSON, we fully reset the scheme
259
262
if ( apiResponse ?. form ) {
263
+ resetFormData ( ) ;
260
264
setRawSchema ( apiResponse . form ) ;
261
265
if ( apiResponse . meta ) {
262
266
setHasNext ( ! ! apiResponse . meta . hasNext ) ;
@@ -270,23 +274,15 @@ function PydanticFormContextProvider({
270
274
}
271
275
272
276
setIsSending ( false ) ;
273
- } , [ apiResponse , onSuccess , rhf , skipSuccessNotice ] ) ;
274
-
275
- const resetFormData = useCallback ( ( ) => {
276
- if ( ! pydanticFormSchema ) {
277
- return ;
278
- }
279
-
280
- rhf . reset ( undefined , { keepDefaultValues : true } ) ;
281
- } , [ pydanticFormSchema , rhf ] ) ;
277
+ } , [ apiResponse , onSuccess , resetFormData , rhf , skipSuccessNotice ] ) ;
282
278
283
279
// a useeffect for filling data whenever formdefinition or labels update
284
280
useEffect ( ( ) => {
285
281
getHashForArray ( formInputData ) . then ( ( hash ) => {
286
282
const currentStepFromHistory = formInputHistory . get ( hash ) ;
287
283
288
284
if ( currentStepFromHistory ) {
289
- rhf . reset ( ) ;
285
+ resetFormData ( ) ;
290
286
Object . entries ( currentStepFromHistory ) . forEach (
291
287
( [ fieldName , fieldValue ] ) =>
292
288
rhf . setValue ( fieldName , fieldValue , {
0 commit comments