@@ -9,7 +9,6 @@ import React, {
99 createContext ,
1010 useCallback ,
1111 useEffect ,
12- useMemo ,
1312 useRef ,
1413 useState ,
1514} from 'react' ;
@@ -174,17 +173,13 @@ function PydanticFormContextProvider({
174173 componentMatcher ,
175174 ) ;
176175
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 ,
188183 ) ;
189184
190185 // initialize the react-hook-form
@@ -204,6 +199,14 @@ function PydanticFormContextProvider({
204199 return ( ) => sub . unsubscribe ( ) ;
205200 } , [ rhf , onChange ] ) ;
206201
202+ const resetFormData = useCallback ( ( ) => {
203+ if ( ! pydanticFormSchema ) {
204+ return ;
205+ }
206+
207+ rhf . reset ( ) ;
208+ } , [ pydanticFormSchema , rhf ] ) ;
209+
207210 rhfRef . current = rhf ;
208211
209212 /* TODO: Reimplement
@@ -236,12 +239,12 @@ function PydanticFormContextProvider({
236239 }
237240
238241 setFormInputHistory ( new Map < string , object > ( ) ) ;
239- rhf . reset ( initialData ) ;
242+ resetFormData ( ) ;
240243 } , [
241244 apiResponse ,
242- initialData ,
243245 isFullFilled ,
244246 onSuccess ,
247+ resetFormData ,
245248 rhf ,
246249 skipSuccessNotice ,
247250 ] ) ;
@@ -257,6 +260,7 @@ function PydanticFormContextProvider({
257260
258261 // when we receive a form from the JSON, we fully reset the scheme
259262 if ( apiResponse ?. form ) {
263+ resetFormData ( ) ;
260264 setRawSchema ( apiResponse . form ) ;
261265 if ( apiResponse . meta ) {
262266 setHasNext ( ! ! apiResponse . meta . hasNext ) ;
@@ -270,23 +274,15 @@ function PydanticFormContextProvider({
270274 }
271275
272276 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 ] ) ;
282278
283279 // a useeffect for filling data whenever formdefinition or labels update
284280 useEffect ( ( ) => {
285281 getHashForArray ( formInputData ) . then ( ( hash ) => {
286282 const currentStepFromHistory = formInputHistory . get ( hash ) ;
287283
288284 if ( currentStepFromHistory ) {
289- rhf . reset ( ) ;
285+ resetFormData ( ) ;
290286 Object . entries ( currentStepFromHistory ) . forEach (
291287 ( [ fieldName , fieldValue ] ) =>
292288 rhf . setValue ( fieldName , fieldValue , {
0 commit comments