Skip to content

Commit b38fc17

Browse files
authored
Merge pull request #148 from workfloworchestrator/2076-values-drop-on-errors
2076 values drop on errors
2 parents d9bdaac + 84db88b commit b38fc17

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pydantic-forms': patch
3+
---
4+
5+
Fix error where resetForm is called to often clearing initialData in some cases

frontend/packages/pydantic-forms/src/components/fields/ArrayField.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ import { itemizeArrayItem } from '@/utils';
99
import { RenderFields } from '../render';
1010

1111
export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
12-
// TODO/NOTE: Default array values on nested object fields are not displayed correctly.
13-
// This looks like its a react-hook-form issue. It doesn't - so far - occur in WFO context so we
14-
// will not fix for now. Observed behavior:
15-
// - The default values are set.
16-
// - The array shows without items
17-
// - As soon as one item is added, the other items are automatically shown.
18-
// As possible fix is to do a setValue of the array field in useEffect.
19-
// Running a rhf.reset() in the RenderForm component works but introduces other issues, resetting the form when errors occur.
20-
2112
const { rhf, config } = usePydanticFormContext();
2213

2314
const { control } = rhf;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,16 @@ function PydanticFormContextProvider({
255255
return;
256256
}
257257

258-
// when we receive a form from the JSON, we fully reset the scheme
258+
// when we receive a new form from JSON, we fully reset the form
259+
if (apiResponse?.form && rawSchema !== apiResponse.form) {
260+
resetFormData();
261+
setRawSchema(apiResponse.form);
262+
if (apiResponse.meta) {
263+
setHasNext(!!apiResponse.meta.hasNext);
264+
}
265+
setErrorDetails(undefined);
266+
}
267+
259268
if (apiResponse?.form) {
260269
resetFormData();
261270
setRawSchema(apiResponse.form);
@@ -271,7 +280,8 @@ function PydanticFormContextProvider({
271280
}
272281

273282
setIsSending(false);
274-
}, [apiResponse, onSuccess, resetFormData, rhf, skipSuccessNotice]);
283+
// eslint-disable-next-line react-hooks/exhaustive-deps
284+
}, [apiResponse]); // Avoid completing the dependencies array here to avoid unwanted resetFormData calls
275285

276286
// a useeffect for filling data whenever formdefinition or labels update
277287

0 commit comments

Comments
 (0)