Skip to content

Commit b5cf834

Browse files
author
Ruben van Leeuwen
committed
2175: Temp fix for adding simple values to arrayItems
1 parent c79ba32 commit b5cf834

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from 'react';
22

3+
import _ from 'lodash';
4+
35
import type { PydanticFormControlledElementProps } from '@/types';
6+
import { getFormFieldIdWithPath } from '@/utils';
47

58
export const IntegerField = ({
69
value,
@@ -9,6 +12,14 @@ export const IntegerField = ({
912
disabled,
1013
pydanticFormField,
1114
}: PydanticFormControlledElementProps) => {
15+
// If the field is part of an array the value is passed in as an object with the field name as key
16+
// this is imposed by react-hook-form. We try to detect this and extract the actual value
17+
const fieldName = getFormFieldIdWithPath(pydanticFormField.id);
18+
const fieldValue =
19+
_.isObject(value) && _.has(value, fieldName)
20+
? _.get(value, fieldName)
21+
: value;
22+
1223
return (
1324
<input
1425
data-testid={pydanticFormField.id}
@@ -18,7 +29,7 @@ export const IntegerField = ({
1829
onChange(value);
1930
}}
2031
disabled={disabled}
21-
value={value}
32+
value={fieldValue}
2233
type="number"
2334
style={{
2435
padding: '8px',

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,40 @@
55
*/
66
import React from 'react';
77

8+
import _ from 'lodash';
9+
810
import { PydanticFormControlledElementProps } from '@/types';
11+
import { getFormFieldIdWithPath } from '@/utils';
912

1013
export const TextField = ({
1114
value,
1215
onChange,
1316
onBlur,
1417
disabled,
1518
pydanticFormField,
16-
}: PydanticFormControlledElementProps) => (
17-
<input
18-
data-testid={pydanticFormField.id}
19-
onBlur={onBlur}
20-
onChange={(t) => {
21-
onChange(t.currentTarget.value);
22-
}}
23-
disabled={disabled}
24-
value={value}
25-
type="text"
26-
style={{
27-
padding: '8px',
28-
margin: '8px 0',
29-
}}
30-
/>
31-
);
19+
}: PydanticFormControlledElementProps) => {
20+
// If the field is part of an array the value is passed in as an object with the field name as key
21+
// this is imposed by react-hook-form. We try to detect this and extract the actual value
22+
const fieldName = getFormFieldIdWithPath(pydanticFormField.id);
23+
const fieldValue =
24+
_.isObject(value) && _.has(value, fieldName)
25+
? _.get(value, fieldName)
26+
: value;
27+
28+
return (
29+
<input
30+
data-testid={pydanticFormField.id}
31+
onBlur={onBlur}
32+
onChange={(t) => {
33+
onChange(t.currentTarget.value);
34+
}}
35+
disabled={disabled}
36+
value={fieldValue}
37+
type="text"
38+
style={{
39+
padding: '8px',
40+
margin: '8px 0',
41+
}}
42+
/>
43+
);
44+
};

frontend/packages/pydantic-forms/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function getFormFieldValue(
127127
*/
128128
export function getFormFieldIdWithPath(
129129
pydanticFormFieldId: PydanticFormField['id'],
130-
fieldName: string,
130+
fieldName?: string,
131131
): string {
132132
const pathToParent = pydanticFormFieldId.split('.');
133133
pathToParent.pop();

0 commit comments

Comments
 (0)