File tree Expand file tree Collapse file tree 3 files changed +42
-18
lines changed
frontend/packages/pydantic-forms/src Expand file tree Collapse file tree 3 files changed +42
-18
lines changed Original file line number Diff line number Diff line change 11import React from 'react' ;
22
3+ import _ from 'lodash' ;
4+
35import type { PydanticFormControlledElementProps } from '@/types' ;
6+ import { getFormFieldIdWithPath } from '@/utils' ;
47
58export 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' ,
Original file line number Diff line number Diff line change 55 */
66import React from 'react' ;
77
8+ import _ from 'lodash' ;
9+
810import { PydanticFormControlledElementProps } from '@/types' ;
11+ import { getFormFieldIdWithPath } from '@/utils' ;
912
1013export 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+ } ;
Original file line number Diff line number Diff line change @@ -127,7 +127,7 @@ export function getFormFieldValue(
127127 */
128128export function getFormFieldIdWithPath (
129129 pydanticFormFieldId : PydanticFormField [ 'id' ] ,
130- fieldName : string ,
130+ fieldName ? : string ,
131131) : string {
132132 const pathToParent = pydanticFormFieldId . split ( '.' ) ;
133133 pathToParent . pop ( ) ;
You can’t perform that action at this time.
0 commit comments