@@ -9,6 +9,7 @@ import React, {
99 createContext ,
1010 useCallback ,
1111 useEffect ,
12+ useMemo ,
1213 useRef ,
1314 useState ,
1415} from 'react' ;
@@ -151,6 +152,7 @@ function PydanticFormContextProvider({
151152 // we cache the form scheme so when there is an error, we still have the form
152153 // the form is not in the error response
153154 const [ rawSchema , setRawSchema ] = useState < PydanticFormSchemaRawJson > ( ) ;
155+ const [ hasNext , setHasNext ] = useState < boolean > ( false ) ;
154156
155157 // extract the JSON schema to a more usable custom schema
156158
@@ -172,10 +174,24 @@ function PydanticFormContextProvider({
172174 componentMatcher ,
173175 ) ;
174176
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 ] ,
188+ ) ;
189+
175190 // initialize the react-hook-form
176191 const rhf = useForm ( {
177192 resolver : zodResolver ( resolver ) ,
178193 mode : 'all' ,
194+ values : initialData ,
179195 } ) ;
180196
181197 // Adds watch subscripton on form values
@@ -220,8 +236,15 @@ function PydanticFormContextProvider({
220236 }
221237
222238 setFormInputHistory ( new Map < string , object > ( ) ) ;
223- rhf . reset ( ) ;
224- } , [ apiResponse , isFullFilled , onSuccess , rhf , skipSuccessNotice ] ) ;
239+ rhf . reset ( initialData ) ;
240+ } , [
241+ apiResponse ,
242+ initialData ,
243+ isFullFilled ,
244+ onSuccess ,
245+ rhf ,
246+ skipSuccessNotice ,
247+ ] ) ;
225248
226249 // a useeffect for whenever the error response updates
227250 // sometimes we need to update the form,
@@ -235,6 +258,9 @@ function PydanticFormContextProvider({
235258 // when we receive a form from the JSON, we fully reset the scheme
236259 if ( apiResponse ?. form ) {
237260 setRawSchema ( apiResponse . form ) ;
261+ if ( apiResponse . meta ) {
262+ setHasNext ( ! ! apiResponse . meta . hasNext ) ;
263+ }
238264 setErrorDetails ( undefined ) ;
239265 }
240266
@@ -251,26 +277,16 @@ function PydanticFormContextProvider({
251277 return ;
252278 }
253279
254- const initialData = getFormValuesFromFieldOrLabels (
255- pydanticFormSchema ,
256- {
257- ...formLabels ?. data ,
258- ...customData ,
259- } ,
260- componentMatcher ,
261- ) ;
262-
263- rhf . reset ( initialData ) ;
264- } , [ customData , formLabels , pydanticFormSchema , rhf , componentMatcher ] ) ;
280+ rhf . reset ( undefined , { keepDefaultValues : true } ) ;
281+ } , [ pydanticFormSchema , rhf ] ) ;
265282
266283 // a useeffect for filling data whenever formdefinition or labels update
267284 useEffect ( ( ) => {
268- // this makes sure default values are set.
269- resetFormData ( ) ;
270285 getHashForArray ( formInputData ) . then ( ( hash ) => {
271286 const currentStepFromHistory = formInputHistory . get ( hash ) ;
272287
273288 if ( currentStepFromHistory ) {
289+ rhf . reset ( ) ;
274290 Object . entries ( currentStepFromHistory ) . forEach (
275291 ( [ fieldName , fieldValue ] ) =>
276292 rhf . setValue ( fieldName , fieldValue , {
@@ -300,7 +316,6 @@ function PydanticFormContextProvider({
300316 const submitFormFn = useCallback ( ( ) => {
301317 setIsSending ( true ) ;
302318 addFormInputData ( rhf ?. getValues ( ) , ! ! errorDetails ) ;
303-
304319 window . scrollTo ( 0 , 0 ) ;
305320 } , [ rhf , errorDetails , addFormInputData ] ) ;
306321
@@ -362,6 +377,7 @@ function PydanticFormContextProvider({
362377 setFormInputData ( [ ] ) ;
363378 setIsFullFilled ( false ) ;
364379 setRawSchema ( undefined ) ;
380+ setHasNext ( false ) ;
365381 } , [ ] ) ;
366382
367383 const PydanticFormContextState = {
@@ -393,6 +409,8 @@ function PydanticFormContextProvider({
393409 formKey,
394410 formIdKey,
395411 clearForm,
412+ formInputData,
413+ hasNext,
396414 } ;
397415
398416 return (
0 commit comments