Skip to content

Commit

Permalink
Merge pull request gutentag2012#51 from gutentag2012/fix/array-defaul…
Browse files Browse the repository at this point in the history
…t-override

fix/array default override
  • Loading branch information
gutentag2012 authored Jun 16, 2024
2 parents c967ca7 + 4a386c8 commit eb60c3c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/form-core/src/FieldGroupLogic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ describe('FieldGroupLogic', () => {
expect(field.errors.value).toEqual(['error'])
expect(group.isDirty.value).toBe(true)
expect(group.dirtyFields.value).toEqual([
'array',
'array.1',
'deep.value',
'name',
Expand Down
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 @@ -533,6 +533,18 @@ describe('FormLogic', () => {
array: [2],
})
})
it('should not update the defaultValue of an array when updating the options', async () => {
const form = new FormLogic<{ array: number[] }>({
defaultValues: {
array: [1, 2, 3],
},
})
await form.mount()

form.handleChange('array', [4, 5])
form.updateOptions({ defaultValues: { array: [1, 2, 3] } })
expect(form.json.value.array).toEqual([4, 5])
})

it('should update the data when using the handleChange method', () => {
const form = new FormLogic<{ name: string }>()
Expand Down Expand Up @@ -1645,6 +1657,7 @@ describe('FormLogic', () => {
expect(form.dirtyFields.value).toEqual([
'name',
'deep.value',
'array',
'array.1',
])
expect(form.submitCount.value).toBe(1)
Expand Down
10 changes: 1 addition & 9 deletions packages/form-core/src/utils/equality.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ describe('equality.utils', () => {
[fileA, fileA],
[{}, {}],
[{ a: 1 }, { a: 1 }],
[
{ a: 1, b: { c: 2 } },
{ a: 1, b: { c: 2, d: 3 } },
],
[
{ nestedArray: [1, undefined, null, new Date(0)] },
{ nestedArray: [1, undefined, null, new Date(0)] },
Expand All @@ -76,11 +72,6 @@ describe('equality.utils', () => {
expect(getLeftUnequalPaths(a, b)).toEqual([])
},
)
it('should not report additional keys from right object', () => {
expect(getLeftUnequalPaths({ a: 1, b: 2 }, { a: 1, b: 2, c: 3 })).toEqual(
[],
)
})
it.each([
[1, 2, ['']],
[null, undefined, ['']],
Expand Down Expand Up @@ -113,6 +104,7 @@ describe('equality.utils', () => {
},
[
'nestedArray.3',
'nestedArray.4.deeply.nested',
'nestedArray.4.deeply.nested.0',
'nestedArray.4.deeply.nested.1',
'nestedArray.5',
Expand Down
6 changes: 6 additions & 0 deletions packages/form-core/src/utils/equality.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ function getLeftUnequalPathsInternal(
const aNonNullable = a as NonNullable<unknown>
const bNonNullable = b as NonNullable<unknown>
const aKeys = Object.keys(aNonNullable)
const bKeys = Object.keys(bNonNullable)

// In this case we do not want to return, since an object can both be dirty and have dirty children
if (aKeys.length !== bKeys.length) {
internalAcc = [...internalAcc, currentKey]
}

for (const key of aKeys) {
const nextKey = currentKey ? `${currentKey}.${key}` : key
Expand Down

0 comments on commit eb60c3c

Please sign in to comment.