Skip to content

Commit 69f8166

Browse files
authored
Merge pull request #116 from workfloworchestrator/1954-get-value-in-field
1954 get value in field
2 parents 7afe477 + a43619f commit 69f8166

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
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+
Fixes getFormFieldValue to handle arrayItem fields better

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('getFormFieldValue', () => {
8181
expect(value).toBe(undefined);
8282
});
8383

84-
it('gets the value at the right level ', () => {
84+
it('gets the value at the right level from an array of objects', () => {
8585
const complexValues = {
8686
company: {
8787
name: 'John Deer',
@@ -114,4 +114,55 @@ describe('getFormFieldValue', () => {
114114
const value2 = getFormFieldValue('licenses', complexValues, field2);
115115
expect(value2).toEqual(['C']);
116116
});
117+
118+
it('gets the value at the right level from an array item', () => {
119+
const complexValues = {
120+
company: {
121+
name: 'John Deer',
122+
age: 30,
123+
contactIds: ['123', '456'],
124+
},
125+
age: 666,
126+
};
127+
128+
const firstListElementField = getPydanticFormFieldDummy({
129+
id: 'company.contactIds.0',
130+
});
131+
const valueFromFirstListElement = getFormFieldValue(
132+
'age',
133+
complexValues,
134+
firstListElementField,
135+
);
136+
expect(valueFromFirstListElement).toEqual(30);
137+
138+
const secondListElementField = getPydanticFormFieldDummy({
139+
id: 'company.contactIds.1',
140+
});
141+
const valueFromSecondListElement = getFormFieldValue(
142+
'age',
143+
complexValues,
144+
secondListElementField,
145+
);
146+
expect(valueFromSecondListElement).toEqual(30);
147+
148+
const nameElementField = getPydanticFormFieldDummy({
149+
id: 'company.name',
150+
});
151+
const valueFromNameElement = getFormFieldValue(
152+
'age',
153+
complexValues,
154+
nameElementField,
155+
);
156+
expect(valueFromNameElement).toEqual(30);
157+
158+
const contactIdsElementField = getPydanticFormFieldDummy({
159+
id: 'company.contactIds',
160+
});
161+
const ownValue = getFormFieldValue(
162+
'contactIds',
163+
complexValues,
164+
contactIdsElementField,
165+
);
166+
expect(ownValue).toEqual(['123', '456']);
167+
});
117168
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ export const itemizeArrayItem = (
104104
export function getFormFieldValue(
105105
fieldName: string,
106106
formValues: FieldValues,
107-
field: PydanticFormField,
107+
pydanticFormField: PydanticFormField,
108108
) {
109-
const pathToParent = field.id.split('.').slice(0, -1);
109+
// Determine by the path if we are part of an array. If we are, we need to chop of one more element
110+
const pathSegments = pydanticFormField.id.split('.');
111+
const lastSegment = pathSegments[pathSegments.length - 1];
112+
const sliceParts = isNaN(Number(lastSegment)) ? 1 : 2;
113+
const pathToParent = pydanticFormField.id.split('.').slice(0, -sliceParts);
110114
let current: FieldValues = { ...formValues };
111115

112116
for (const segment of pathToParent) {

0 commit comments

Comments
 (0)