Skip to content

Commit

Permalink
fix(form-core): creation of form group mutating members array
Browse files Browse the repository at this point in the history
  • Loading branch information
gutentag2012 committed Jun 17, 2024
1 parent 56b4baa commit e5432e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/form-core/src/FormLogic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,19 @@ describe('FormLogic', () => {
group.unmount()
expect(form.fieldGroups.value).toEqual([])
})
it("should not mutate the passed members array when creating a field group", () => {
const members = ['start', 'end'] as ["start", "end"]
const form = new FormLogic({
defaultValues: {
start: 0,
end: 1,
},
})
form.mount()

form.getOrCreateFieldGroup(members)
expect(members).toEqual(['start', 'end'])
})
})
describe('validation', () => {
it('should trigger submit validation for all fields on submit as well as the form', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/form-core/src/FormLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
unSignalifyValueSubscribed,
} from './utils'
import { deepCopy } from './utils/access.utils'
import { Truthy } from './utils/internal.utils'
import {getGroupKey, Truthy} from './utils/internal.utils'
import type { ConnectPath, ExcludeAll, KeepOptionalKeys } from './utils/types'
import {
clearErrorMap,
Expand Down Expand Up @@ -827,7 +827,7 @@ export class FormLogic<
TMixin
>,
): FieldGroupLogic<TData, TMembers, TGroupAdapter, TAdapter, TMixin> {
const groupKey = members.sort().join('-')
const groupKey = getGroupKey(members)
const existingGroup = this._fieldGroups.value.get(groupKey) as
| FieldGroupLogic<TData, TMembers, TGroupAdapter, TAdapter, TMixin>
| undefined
Expand Down Expand Up @@ -856,7 +856,7 @@ export class FormLogic<
members: TMembers,
group: FieldGroupLogic<TData, TMembers, TGroupAdapter, TAdapter, TMixin>,
): void {
const groupKey = members.sort().join('-')
const groupKey = getGroupKey(members)
if (this._fieldGroups.peek().has(groupKey)) return

const newMap = new Map(this._fieldGroups.peek())
Expand All @@ -867,7 +867,7 @@ export class FormLogic<
public unregisterFieldGroup<TMembers extends Paths<TData>[]>(
members: TMembers,
): void {
const groupKey = members.sort().join('-')
const groupKey = getGroupKey(members)
if (!this._fieldGroups.peek().has(groupKey)) return

const newMap = new Map(this._fieldGroups.peek())
Expand Down
4 changes: 4 additions & 0 deletions packages/form-core/src/utils/internal.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export function Truthy<T>(
): value is NonNullable<T> {
return !!value
}

export function getGroupKey(members: string[]) {
return [...members].sort().join('.')
}

0 comments on commit e5432e0

Please # to comment.