Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont merge arrays in mergeForm #995

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/functions/mergeform.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ function mergeForm<TFormData, TFormValidator>(baseForm, state): FormApi<NoInfer<

## Defined in

[packages/form-core/src/mergeForm.ts:37](https://github.com/TanStack/form/blob/main/packages/form-core/src/mergeForm.ts#L37)
[packages/form-core/src/mergeForm.ts:36](https://github.com/TanStack/form/blob/main/packages/form-core/src/mergeForm.ts#L36)
7 changes: 3 additions & 4 deletions packages/form-core/src/mergeForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export function mutateMergeDeep(target: object, source: object): object {
for (const key of keySet) {
const targetKey = key as never as keyof typeof target
const sourceKey = key as never as keyof typeof source

if (Array.isArray(target[targetKey]) && Array.isArray(source[sourceKey])) {
target[targetKey] = [
...(target[targetKey] as []),
...(source[sourceKey] as []),
] as never
// always use the source array to prevent array fields from multiplying
target[targetKey] = source[sourceKey] as [] as never
} else if (
typeof target[targetKey] === 'object' &&
typeof source[sourceKey] === 'object'
Expand Down
31 changes: 26 additions & 5 deletions packages/form-core/tests/mutateMergeDeep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,32 @@ describe('mutateMergeDeep', () => {
expect(a).toStrictEqual({ a: undefined })
})

test('Should merge two object by merging arrays', () => {
const a = { a: [1] }
const b = { a: [2] }
mutateMergeDeep(a, b)
expect(a).toStrictEqual({ a: [1, 2] })
test('Should merge two object by overriding arrays', () => {
const target = { a: [1] }
const source = { a: [2] }
mutateMergeDeep(target, source)
expect(target).toStrictEqual({ a: [2] })
})

test('Should merge add array element when it does not exist in target', () => {
const target = { a: [] }
const source = { a: [2] }
mutateMergeDeep(target, source)
expect(target).toStrictEqual({ a: [2] })
})

test('Should override the target array if source is undefined', () => {
const target = { a: [2] }
const source = { a: undefined }
mutateMergeDeep(target, source)
expect(target).toStrictEqual({ a: undefined })
})

test('Should merge update array element when it does not exist in source', () => {
const target = { a: [2] }
const source = { a: [] }
mutateMergeDeep(target, source)
expect(target).toStrictEqual({ a: [] })
})

test('Should merge two deeply nested objects', () => {
Expand Down