Skip to content

Commit 204b4ea

Browse files
author
Ruben van Leeuwen
committed
2040: USe reset form instead of setvalue when setting data from history
1 parent 1a3a11b commit 204b4ea

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ function PydanticFormContextProvider({
173173
values: initialData,
174174
});
175175

176+
const resetFormData = useCallback(
177+
(inputData: object = {}) => {
178+
if (!pydanticFormSchema) {
179+
return;
180+
}
181+
182+
rhf.reset(inputData);
183+
},
184+
[pydanticFormSchema, rhf],
185+
);
186+
187+
rhfRef.current = rhf;
188+
176189
// Adds watch subscripton on form values
177190
useEffect(() => {
178191
const sub = rhf.watch((values) => {
@@ -183,16 +196,6 @@ function PydanticFormContextProvider({
183196
return () => sub.unsubscribe();
184197
}, [rhf, onChange]);
185198

186-
const resetFormData = useCallback(() => {
187-
if (!pydanticFormSchema) {
188-
return;
189-
}
190-
191-
rhf.reset();
192-
}, [pydanticFormSchema, rhf]);
193-
194-
rhfRef.current = rhf;
195-
196199
/* TODO: Reimplement
197200
// prevent user from navigating away when there are unsaved changes
198201
const hasUnsavedData =
@@ -205,6 +208,7 @@ function PydanticFormContextProvider({
205208
'Er zijn aanpassingen in het formulier. \nWeet je zeker dat je de pagina wilt verlaten?',
206209
);
207210
*/
211+
208212
// handle successfull submits
209213
useEffect(() => {
210214
if (!isFullFilled) {
@@ -261,20 +265,13 @@ function PydanticFormContextProvider({
261265
}, [apiResponse, onSuccess, resetFormData, rhf, skipSuccessNotice]);
262266

263267
// a useeffect for filling data whenever formdefinition or labels update
268+
264269
useEffect(() => {
265270
getHashForArray(formInputData).then((hash) => {
266271
const currentStepFromHistory = formInputHistory.get(hash);
267272

268273
if (currentStepFromHistory) {
269-
resetFormData();
270-
Object.entries(currentStepFromHistory).forEach(
271-
([fieldName, fieldValue]) =>
272-
rhf.setValue(fieldName, fieldValue, {
273-
shouldDirty: true,
274-
shouldTouch: true,
275-
shouldValidate: true,
276-
}),
277-
);
274+
resetFormData(currentStepFromHistory);
278275
}
279276
});
280277
}, [formInputData, formInputHistory, resetFormData, rhf]);

0 commit comments

Comments
 (0)