@@ -2,10 +2,11 @@ import React, { useCallback, useRef, useState } from 'react';
2
2
import type { FieldValues } from 'react-hook-form' ;
3
3
4
4
import { PydanticFormValidationErrorContext } from '@/PydanticForm' ;
5
+ import { useGetConfig , usePydanticForm } from '@/core/hooks' ;
5
6
import { PydanticFormSuccessResponse } from '@/types' ;
7
+ import { getHashForArray } from '@/utils' ;
6
8
7
9
import { ReactHookForm } from './ReactHookForm' ;
8
- import { useGetConfig , usePydanticForm } from './hooks' ;
9
10
10
11
export interface PydanticFormHandlerProps {
11
12
formKey : string ;
@@ -26,18 +27,44 @@ export const PydanticFormHandler = ({
26
27
const config = useGetConfig ( ) ;
27
28
const [ formStep , setStep ] = useState < FieldValues > ( ) ;
28
29
const formStepsRef = useRef < FieldValues [ ] > ( [ ] ) ;
30
+ const [ initialValues , setInitialValues ] = useState < FieldValues > ( ) ;
31
+ const formInputHistoryRef = useRef < Map < string , FieldValues > > (
32
+ new Map < string , object > ( ) ,
33
+ ) ;
34
+
35
+ const storeHistory = useCallback ( async ( stepData : FieldValues ) => {
36
+ const hashOfPreviousSteps = await getHashForArray ( formStepsRef . current ) ;
37
+ formInputHistoryRef . current . set ( hashOfPreviousSteps , stepData ) ;
38
+ } , [ ] ) ;
39
+
29
40
const {
30
41
validationErrorsDetails,
31
42
apiError,
32
43
hasNext,
33
44
isFullFilled,
34
45
isLoading,
35
46
pydanticFormSchema,
36
- initialValues ,
47
+ defaultValues ,
37
48
} = usePydanticForm ( formKey , config , formStepsRef , onSuccess , formStep ) ;
38
49
39
- const handleStepSubmit = useCallback ( ( fieldValues : FieldValues ) => {
40
- setStep ( fieldValues ) ;
50
+ const handleStepSubmit = useCallback (
51
+ async ( fieldValues : FieldValues ) => {
52
+ await storeHistory ( fieldValues ) ;
53
+ setStep ( fieldValues ) ;
54
+ } ,
55
+ [ storeHistory ] ,
56
+ ) ;
57
+
58
+ const onPrevious = useCallback ( async ( ) => {
59
+ const previousSteps = formStepsRef . current . slice ( 0 , - 1 ) ;
60
+ const hashOfPreviousSteps = await getHashForArray ( previousSteps ) ;
61
+ if ( formInputHistoryRef . current . has ( hashOfPreviousSteps ) ) {
62
+ setInitialValues (
63
+ formInputHistoryRef . current . get ( hashOfPreviousSteps ) || { } ,
64
+ ) ;
65
+ }
66
+ formStepsRef . current = previousSteps ;
67
+ setStep ( undefined ) ;
41
68
} , [ ] ) ;
42
69
43
70
const handleCancel = useCallback ( ( ) => {
@@ -50,16 +77,17 @@ export const PydanticFormHandler = ({
50
77
value = { validationErrorsDetails }
51
78
>
52
79
< ReactHookForm
53
- pydanticFormSchema = { pydanticFormSchema }
54
- isLoading = { isLoading }
55
- isFullFilled = { isFullFilled }
56
- isSending = { false }
57
- hasNext = { hasNext }
58
80
apiError = { apiError }
59
- initialValues = { initialValues }
60
- handleSubmit = { handleStepSubmit }
61
- hasPrevious = { false }
81
+ defaultValues = { defaultValues }
62
82
handleCancel = { handleCancel }
83
+ handleSubmit = { handleStepSubmit }
84
+ hasNext = { hasNext }
85
+ hasPrevious = { formStepsRef . current . length > 0 }
86
+ initialValues = { initialValues }
87
+ isFullFilled = { isFullFilled }
88
+ isLoading = { isLoading }
89
+ onPrevious = { onPrevious }
90
+ pydanticFormSchema = { pydanticFormSchema }
63
91
title = { title }
64
92
/>
65
93
</ PydanticFormValidationErrorContext . Provider >
0 commit comments