Skip to content

Commit 051e87b

Browse files
stefan-huckautofix-ci[bot]LeCarbonator
authored
fix(form-core): prevent duplicate dots when concatenating withFieldGroup paths (#1639)
* fix(form-core): Fix handle nested withFieldGroup properly * ci: apply automated fixes and generate docs * chore: add unit test for concatenatePath * chore: add FieldGroupApi unit test --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: LeCarbonator <18158911+LeCarbonator@users.noreply.github.com>
1 parent a6abf35 commit 051e87b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

packages/form-core/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ export function concatenatePaths(path1: string, path2: string): string {
196196
return path1 + path2
197197
}
198198

199+
// In cases where parent and child withFieldGroup forms are both nested
200+
if (path2.startsWith('.')) {
201+
return path1 + path2
202+
}
203+
199204
return `${path1}.${path2}`
200205
}
201206

packages/form-core/tests/FieldGroupApi.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,4 +888,35 @@ describe('field group api', () => {
888888
'Error',
889889
])
890890
})
891+
892+
it('should generate form field names properly with nested objects', () => {
893+
// https://github.com/TanStack/form/issues/1645
894+
const form = new FormApi({
895+
defaultValues: {
896+
complexValue: {
897+
prop1: 0,
898+
prop2: 0,
899+
},
900+
},
901+
})
902+
form.mount()
903+
904+
const group = new FieldGroupApi({
905+
defaultValues: {
906+
complexValue: {
907+
prop1: 0,
908+
prop2: 0,
909+
},
910+
},
911+
form,
912+
fields: {
913+
complexValue: 'complexValue',
914+
},
915+
})
916+
group.mount()
917+
918+
expect(group.getFormFieldName('complexValue.prop1')).toBe(
919+
'complexValue.prop1',
920+
)
921+
})
891922
})

packages/form-core/tests/utils.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ describe('concatenatePaths', () => {
688688
)
689689
expect(concatenatePaths('data', '[1].value')).toBe('data[1].value')
690690
})
691+
692+
it('should not duplicate dots if the second path starts with one', () => {
693+
expect(concatenatePaths('foo', '.bar')).toBe('foo.bar')
694+
})
691695
})
692696

693697
describe('createFieldMap', () => {

0 commit comments

Comments
 (0)