Skip to content
Open
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
17 changes: 16 additions & 1 deletion packages/form-core/src/FormApi/FormApi.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,22 @@ export class InternalFormApi<
updateOptions.fieldApiOverride = field

batch(() => {
this._atoms.values.set((prev) => setBy(prev, fieldName, updater))
const previousValue = this.getFieldValue(fieldName)
const nextValue = callUpdater(updater, previousValue)
const replacedSameLengthArray =
Array.isArray(previousValue) &&
Array.isArray(nextValue) &&
previousValue !== nextValue &&
previousValue.length === nextValue.length

this._atoms.values.set((prev) => setBy(prev, fieldName, nextValue))

if (field && replacedSameLengthArray) {
field._setMeta((prev) => ({
...prev,
_arrayVersion: prev._arrayVersion + 1,
}))
}

this._notifyFieldChange(field, updateOptions)
})
Expand Down
16 changes: 0 additions & 16 deletions packages/form-core/src/FormApi/array-methods.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,6 @@ function swapFieldValues({

if (!arrayField) return

// Since the length wasn't changed, we need to notify manually
arrayField._setMeta((prev) => ({
...prev,
_arrayVersion: prev._arrayVersion + 1,
}))

const fieldA = tryGetFieldApi(arrayField, [indexA])

const fieldB = tryGetFieldApi(arrayField, [indexB])
Expand Down Expand Up @@ -266,11 +260,6 @@ function moveFieldValue({

if (!arrayField) return

arrayField._setMeta((prev) => ({
...prev,
_arrayVersion: prev._arrayVersion + 1,
}))

const movingChild = tryGetFieldApi(arrayField, [fromIndex])

for (const child of arrayField._children) {
Expand Down Expand Up @@ -312,11 +301,6 @@ function clearFieldValues({

if (!arrayField) return

arrayField._setMeta((prev) => ({
...prev,
_arrayVersion: prev._arrayVersion + 1,
}))

// Kill all child fields since the array is now empty
// _kill() will remove each child from the parent's children
for (const child of arrayField._children) {
Expand Down
53 changes: 53 additions & 0 deletions packages/form-core/tests/FormApi/field-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,59 @@ describe('form - field state', () => {
expect(form.getFieldValue('count')).toBe(2)
})

it('increments the array version for a same-length array replacement', () => {
const form = new InternalFormApi({ defaultValues: { items: ['a'] } })
const field = form._getOrCreateFieldApi({ name: 'items' })

form.setFieldValue('items', ['b'])

expect(form.getFieldValue('items')).toEqual(['b'])
expect(field.meta._arrayVersion).toBe(1)
})

it('increments the array version for an updater replacement', () => {
const form = new InternalFormApi({ defaultValues: { items: ['a'] } })
const field = form._getOrCreateFieldApi({ name: 'items' })

form.setFieldValue('items', (items: Array<string>) =>
items.map((item) => item.toUpperCase()),
)

expect(form.getFieldValue('items')).toEqual(['A'])
expect(field.meta._arrayVersion).toBe(1)
})

it('does not increment a parent array version for a nested field update', () => {
const form = new InternalFormApi({
defaultValues: { items: [{ label: 'a' }] },
})
const field = form._getOrCreateFieldApi({ name: 'items' })

form.setFieldValue('items[0].label', 'b')

expect(form.getFieldValue('items')).toEqual([{ label: 'b' }])
expect(field.meta._arrayVersion).toBe(0)
})

it('increments the array version only once for array helpers', () => {
const form = new InternalFormApi({
defaultValues: { items: ['a', 'b', 'c'] },
})
const field = form._getOrCreateFieldApi({ name: 'items' })

form.swapFieldValues('items', 0, 1)
expect(field.meta._arrayVersion).toBe(1)

form.moveFieldValue('items', 0, 2)
expect(field.meta._arrayVersion).toBe(2)

form.clearFieldValues('items')
expect(field.meta._arrayVersion).toBe(2)

form.clearFieldValues('items')
expect(field.meta._arrayVersion).toBe(3)
})

it('marks form isTouched and isDirty after a change', () => {
const form = new InternalFormApi({ defaultValues: { name: '' } })
const field = form._getOrCreateFieldApi({ name: 'name' })
Expand Down
47 changes: 47 additions & 0 deletions packages/react-form/tests/FormField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,53 @@ describe('Form fields', () => {
expect(input).toHaveValue('new-value')
})

it('rerenders an ArrayField for a same-length replacement', () => {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test does fail without the change

let renders = 0

function Component() {
const form = useForm({
defaultValues: { items: [{ label: 'A' }] },
})

return (
<>
<button
data-testid="replace-array"
onClick={() =>
form.setFieldValue('items', [{ label: 'B' }])
}
/>
<button
data-testid="update-child"
onClick={() => form.setFieldValue('items[0].label', 'C')}
/>
<form.ArrayField name="items">
{(field) => {
renders++
return (
<output data-testid="array">{field.value[0]!.label}</output>
)
}}
</form.ArrayField>
</>
)
}

const { getByTestId } = render(<Component />)
const initialRenders = renders

fireEvent.click(getByTestId('replace-array'))

expect(getByTestId('array')).toHaveTextContent('B')
expect(renders).toBeGreaterThan(initialRenders)

const replacementRenders = renders

fireEvent.click(getByTestId('update-child'))

expect(renders).toBe(replacementRenders)
})

it('should have the correct meta when changing the field', async () => {
function Component() {
const form = useForm({ defaultValues: { name: 'tony-hawk' } })
Expand Down