Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions frontend/.changeset/weak-badgers-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pydantic-forms': patch
---

Fixes data reset bug
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import React, {
createContext,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
Expand Down Expand Up @@ -174,17 +173,13 @@ function PydanticFormContextProvider({
componentMatcher,
);

const initialData = useMemo(
() =>
getFormValuesFromFieldOrLabels(
pydanticFormSchema,
{
...formLabels?.data,
...customData,
},
componentMatcher,
),
[componentMatcher, customData, formLabels?.data, pydanticFormSchema],
const initialData = getFormValuesFromFieldOrLabels(
pydanticFormSchema,
{
...formLabels?.data,
...customData,
},
componentMatcher,
);

// initialize the react-hook-form
Expand All @@ -204,6 +199,14 @@ function PydanticFormContextProvider({
return () => sub.unsubscribe();
}, [rhf, onChange]);

const resetFormData = useCallback(() => {
if (!pydanticFormSchema) {
return;
}

rhf.reset();
}, [pydanticFormSchema, rhf]);

rhfRef.current = rhf;

/* TODO: Reimplement
Expand Down Expand Up @@ -236,12 +239,12 @@ function PydanticFormContextProvider({
}

setFormInputHistory(new Map<string, object>());
rhf.reset(initialData);
resetFormData();
}, [
apiResponse,
initialData,
isFullFilled,
onSuccess,
resetFormData,
rhf,
skipSuccessNotice,
]);
Expand All @@ -257,6 +260,7 @@ function PydanticFormContextProvider({

// when we receive a form from the JSON, we fully reset the scheme
if (apiResponse?.form) {
resetFormData();
setRawSchema(apiResponse.form);
if (apiResponse.meta) {
setHasNext(!!apiResponse.meta.hasNext);
Expand All @@ -270,23 +274,15 @@ function PydanticFormContextProvider({
}

setIsSending(false);
}, [apiResponse, onSuccess, rhf, skipSuccessNotice]);

const resetFormData = useCallback(() => {
if (!pydanticFormSchema) {
return;
}

rhf.reset(undefined, { keepDefaultValues: true });
}, [pydanticFormSchema, rhf]);
}, [apiResponse, onSuccess, resetFormData, rhf, skipSuccessNotice]);

// a useeffect for filling data whenever formdefinition or labels update
useEffect(() => {
getHashForArray(formInputData).then((hash) => {
const currentStepFromHistory = formInputHistory.get(hash);

if (currentStepFromHistory) {
rhf.reset();
resetFormData();
Object.entries(currentStepFromHistory).forEach(
([fieldName, fieldValue]) =>
rhf.setValue(fieldName, fieldValue, {
Expand Down