Skip to content

Commit b7d1dde

Browse files
authored
Merge pull request #146 from workfloworchestrator/2076-values-drop-on-errors
2076 values drop on errors
2 parents d42b0aa + 6202fd8 commit b7d1dde

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
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+
Removes form reset after render to avoid losing values on errors

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import { useFieldArray } from 'react-hook-form';
33

44
import { usePydanticFormContext } from '@/core';
55
import { fieldToComponentMatcher } from '@/core/helper';
6-
import {
7-
PydanticFormElementProps,
8-
PydanticFormField,
9-
PydanticFormFieldType,
10-
} from '@/types';
6+
import { PydanticFormElementProps } from '@/types';
117
import { itemizeArrayItem } from '@/utils';
128

139
import { RenderFields } from '../render';
1410

1511
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+
1621
const { rhf, config } = usePydanticFormContext();
1722

1823
const { control } = rhf;
@@ -24,19 +29,6 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
2429
const { minItems = 1, maxItems = undefined } =
2530
pydanticFormField?.validations;
2631

27-
const getInitialValue = (arrayItem?: PydanticFormField) => {
28-
switch (arrayItem?.type) {
29-
case PydanticFormFieldType.STRING:
30-
return '';
31-
case PydanticFormFieldType.INTEGER:
32-
return 0;
33-
case PydanticFormFieldType.BOOLEAN:
34-
return false;
35-
default:
36-
return undefined;
37-
}
38-
};
39-
4032
if (!arrayItem) return '';
4133

4234
const component = fieldToComponentMatcher(
@@ -96,8 +88,7 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
9688
<div
9789
onClick={() => {
9890
append({
99-
[arrayName]:
100-
arrayItem.default || getInitialValue(arrayItem),
91+
[arrayName]: arrayItem.default,
10192
});
10293
}}
10394
style={{

frontend/packages/pydantic-forms/src/components/render/RenderForm.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,17 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
2727
skipSuccessNotice,
2828
loadingComponent,
2929
rhf,
30-
initialData,
3130
} = contextProps;
31+
3232
const { formRenderer, footerRenderer, headerRenderer } = config || {};
3333

3434
useEffect(() => {
35-
if (rhf.getValues().length === 0) {
35+
if (rhf.getValues().length !== 0) {
3636
// Only trigger validations if there are values
3737
rhf.trigger();
3838
}
3939
}, [rhf, pydanticFormSchema]);
4040

41-
useEffect(() => {
42-
rhf.reset(initialData);
43-
}, [rhf, initialData]);
44-
4541
const pydanticFormComponents: PydanticFormComponents =
4642
getPydanticFormComponents(
4743
pydanticFormSchema?.properties || {},

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ function PydanticFormContextProvider({
178178
const rhf = useForm({
179179
resolver: zodResolver(zodSchema),
180180
mode: 'all',
181+
defaultValues: initialData,
182+
values: initialData,
181183
});
182184

183185
const resetFormData = useCallback(

0 commit comments

Comments
 (0)