1
- import React , { useCallback , useRef , useState } from 'react' ;
1
+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
2
2
import type { FieldValues } from 'react-hook-form' ;
3
3
4
4
import { PydanticFormValidationErrorContext } from '@/PydanticForm' ;
@@ -18,10 +18,21 @@ export const PydanticFormHandler = ({
18
18
const [ formStep , setStep ] = useState < FieldValues > ( ) ;
19
19
const formStepsRef = useRef < FieldValues [ ] > ( [ ] ) ;
20
20
const [ initialValues , setInitialValues ] = useState < FieldValues > ( ) ;
21
+ const [ currentFormKey , setCurrentFormKey ] = useState < string > ( formKey ) ;
21
22
const formInputHistoryRef = useRef < Map < string , FieldValues > > (
22
23
new Map < string , object > ( ) ,
23
24
) ;
24
25
26
+ useEffect ( ( ) => {
27
+ if ( formKey && formKey !== currentFormKey ) {
28
+ formStepsRef . current = [ ] ;
29
+ formInputHistoryRef . current = new Map < string , object > ( ) ;
30
+ setCurrentFormKey ( formKey ) ;
31
+ setStep ( undefined ) ;
32
+ setInitialValues ( undefined ) ;
33
+ }
34
+ } , [ formKey , currentFormKey ] ) ;
35
+
25
36
const storeHistory = useCallback ( async ( stepData : FieldValues ) => {
26
37
const hashOfSteps = await getHashForArray ( formStepsRef . current ) ;
27
38
formInputHistoryRef . current . set ( hashOfSteps , stepData ) ;
0 commit comments