Skip to content

Commit 38924ac

Browse files
author
Ruben van Leeuwen
committed
2143: Fixes resetting form on formKey change
1 parent 0c67d61 commit 38924ac

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

frontend/packages/pydantic-forms/src/core/PydanticFormHandler.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useRef, useState } from 'react';
1+
import React, { useCallback, useEffect, useRef, useState } from 'react';
22
import type { FieldValues } from 'react-hook-form';
33

44
import { 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

Comments
 (0)