1- import React , { useCallback , useRef , useState } from 'react' ;
1+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
22import type { FieldValues } from 'react-hook-form' ;
33
44import { PydanticFormValidationErrorContext } from '@/PydanticForm' ;
@@ -18,10 +18,21 @@ export const PydanticFormHandler = ({
1818 const [ formStep , setStep ] = useState < FieldValues > ( ) ;
1919 const formStepsRef = useRef < FieldValues [ ] > ( [ ] ) ;
2020 const [ initialValues , setInitialValues ] = useState < FieldValues > ( ) ;
21+ const [ currentFormKey , setCurrentFormKey ] = useState < string > ( formKey ) ;
2122 const formInputHistoryRef = useRef < Map < string , FieldValues > > (
2223 new Map < string , object > ( ) ,
2324 ) ;
2425
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+
2536 const storeHistory = useCallback ( async ( stepData : FieldValues ) => {
2637 const hashOfSteps = await getHashForArray ( formStepsRef . current ) ;
2738 formInputHistoryRef . current . set ( hashOfSteps , stepData ) ;
0 commit comments