@@ -162,11 +162,25 @@ function PydanticFormContextProvider({
162162 } ) ;
163163
164164 const awaitReset = useCallback (
165- async ( payLoad : FieldValues = { } ) => {
166- reactHookForm . reset ( payLoad ) ;
167- await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ; // wait one tick
165+ async ( payLoad ?: FieldValues ) => {
166+ getHashForArray ( formInputData ) . then ( async ( hash ) => {
167+ let resetPayload = { } ;
168+
169+ if ( payLoad ) {
170+ resetPayload = { ...payLoad } ;
171+ } else {
172+ const currentStepFromHistory = formInputHistory . get ( hash ) ;
173+
174+ if ( currentStepFromHistory ) {
175+ resetPayload = { ...currentStepFromHistory } ;
176+ }
177+ }
178+ reactHookForm . reset ( resetPayload ) ;
179+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ; // wait one tick
180+ } ) ;
168181 } ,
169- [ reactHookForm ] ,
182+
183+ [ formInputData , formInputHistory , reactHookForm ] ,
170184 ) ;
171185
172186 const addFormInputData = useCallback (
@@ -242,6 +256,42 @@ function PydanticFormContextProvider({
242256 setHasNext ( false ) ;
243257 } , [ emptyRawSchema ] ) ;
244258
259+ const fieldDataStorageRef = useRef < Map < string , Map < string , unknown > > > (
260+ new Map ( ) ,
261+ ) ;
262+
263+ const fieldDataStorage = useMemo (
264+ ( ) => ( {
265+ has : ( fieldId : string , key : string | number ) => {
266+ if (
267+ fieldDataStorageRef . current &&
268+ fieldDataStorageRef . current . has ( fieldId )
269+ ) {
270+ const fieldStorage =
271+ fieldDataStorageRef . current . get ( fieldId ) ;
272+ return fieldStorage ?. has ( key . toString ( ) ) ?? false ;
273+ }
274+ return false ;
275+ } ,
276+ get : ( fieldId : string , key : string | number ) => {
277+ const fieldData = fieldDataStorageRef ?. current ?. get ( fieldId ) ;
278+ return fieldData ?. get ( key . toString ( ) ) ;
279+ } ,
280+ set : ( fieldId : string , key : string | number , value : unknown ) => {
281+ fieldDataStorageRef . current . set (
282+ fieldId ,
283+ new Map ( [ [ key . toString ( ) , value ] ] ) ,
284+ ) ;
285+ } ,
286+ delete : ( fieldId : string ) => {
287+ if ( fieldDataStorageRef . current ?. has ( fieldId ) ) {
288+ fieldDataStorageRef . current . delete ( fieldId ) ;
289+ }
290+ } ,
291+ } ) ,
292+ [ ] ,
293+ ) ;
294+
245295 const PydanticFormContextState = {
246296 // to prevent an issue where the sending state hangs
247297 // we check both the SWR hook state as our manual state
@@ -272,6 +322,7 @@ function PydanticFormContextProvider({
272322 formInputData,
273323 hasNext,
274324 initialData,
325+ fieldDataStorage,
275326 } ;
276327
277328 // a useeffect for whenever the error response updates
@@ -316,7 +367,7 @@ function PydanticFormContextProvider({
316367 // When the formKey changes we need to reset the form input data
317368 setFormInputData ( [ ] ) ;
318369 setFormInputHistory ( new Map < string , object > ( ) ) ;
319- awaitReset ( ) ;
370+ awaitReset ( { } ) ;
320371 formRef . current = formKey ;
321372 }
322373 } , [ awaitReset , formKey ] ) ;
@@ -371,15 +422,6 @@ function PydanticFormContextProvider({
371422 z . config ( getLocale ( ) ) ;
372423 } , [ locale ] ) ;
373424
374- useEffect ( ( ) => {
375- getHashForArray ( formInputData ) . then ( ( hash ) => {
376- const currentStepFromHistory = formInputHistory . get ( hash ) ;
377- if ( currentStepFromHistory ) {
378- awaitReset ( currentStepFromHistory ) ;
379- }
380- } ) ;
381- } , [ awaitReset , formInputData , formInputHistory , reactHookForm ] ) ;
382-
383425 return (
384426 < PydanticFormContext . Provider value = { PydanticFormContextState } >
385427 { children ( PydanticFormContextState ) }
0 commit comments