Skip to content

Commit 4b0fc23

Browse files
authored
Merge pull request #143 from workfloworchestrator/2076-add-util-for-watching-fields
2076 add util for watching fields
2 parents 2d68bb7 + 4b54738 commit 4b0fc23

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
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+
Adds util for watching fields

frontend/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { FieldValues } from 'react-hook-form';
33
import { getMockPydanticFormField } from './core/helper.spec';
44
import { PydanticFormFieldType } from './types';
55
import {
6+
getFormFieldIdWithPath,
67
getFormFieldValue,
78
insertItemAtIndex,
89
itemizeArrayItem,
@@ -194,6 +195,26 @@ describe('getFormFieldValue', () => {
194195
});
195196
});
196197

198+
describe.only('getFormFieldIdWithPath', () => {
199+
it('returns fieldname when no path is supplied', () => {
200+
expect(getFormFieldIdWithPath('', 'name')).toBe('name');
201+
});
202+
203+
it('returns the full field id from the sybling level', () => {
204+
expect(getFormFieldIdWithPath('person.name', 'age')).toBe('person.age');
205+
});
206+
207+
it('works with array', () => {
208+
expect(getFormFieldIdWithPath('person.0', '')).toBe('person');
209+
});
210+
211+
it('works with array of objects', () => {
212+
expect(getFormFieldIdWithPath('person.0.name', 'age')).toBe(
213+
'person.0.age',
214+
);
215+
});
216+
});
217+
197218
describe('itemizeArrayItem', () => {
198219
// Postfixes .{index} to PydanticFormField id
199220
// When the item being itemized is an object field adds .{index} at correct position in to ids of all properties

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,26 @@ export function getFormFieldValue(
107107

108108
return current?.[name];
109109
}
110+
111+
/**
112+
* Returns the full field id from the sibling level. Used to pass into rhfs watch function.
113+
* This is relevant if the field is part of an array or object where there might be more
114+
*
115+
*/
116+
export function getFormFieldIdWithPath(
117+
pydanticFormFieldId: PydanticFormField['id'],
118+
fieldName: string,
119+
): string {
120+
const pathToParent = pydanticFormFieldId.split('.');
121+
pathToParent.pop();
122+
123+
if (fieldName) {
124+
return pathToParent?.length > 0
125+
? [pathToParent.join('.'), fieldName].join('.')
126+
: fieldName;
127+
} else if (pathToParent?.length > 0) {
128+
return pathToParent.join('.'); // Return the full path without the last segment
129+
}
130+
131+
return '';
132+
}

0 commit comments

Comments
 (0)