Skip to content

fix(form-core): shift the array's own index when shifting nested array meta - #2361

Open
dfedoryshchev wants to merge 1 commit into
TanStack:mainfrom
dfedoryshchev:fix/nested-array-meta-shift
Open

fix(form-core): shift the array's own index when shifting nested array meta#2361
dfedoryshchev wants to merge 1 commit into
TanStack:mainfrom
dfedoryshchev:fix/nested-array-meta-shift

Conversation

@dfedoryshchev

@dfedoryshchev dfedoryshchev commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Changes

Inserting into or removing from an array nested inside another array wipes the following rows' field meta instead of shifting it. A flat array behaves correctly, so the two disagree.

updateIndex (metaHelper.ts:227) rewrites the first bracketed index it finds: fieldKey.replace(/\[(\d+)\]/, ...). For users[1].name that is the right one. For teams[0].members[1] it rewrites teams's index, producing teams[1].members[1] - a key that does not exist - so the read returns undefined and the shifted row is reset to default meta. Touched, blurred, dirty and errors are all lost.

The fix anchors the rewrite on the array being mutated, so only the index directly after ${field}[ moves. handleArraySwap already does exactly this via getFieldPath(field, index) (metaHelper.ts:147), so this brings the other three helpers into line with it rather than introducing a new approach.

It reaches users through removeFieldValue, insertFieldValue and moveFieldValues; getAffectedFields narrows it to array paths that themselves contain an index, which is why only the nested case is affected.

Targeting main deliberately: on alpha the array subsystem was rewritten to a trie and updateIndex no longer exists, so this is v1-only.

Two regression tests next to the existing flat-array meta-shift tests. form-core is 507 passing with no type errors, eslint and prettier clean.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed form metadata shifting for nested array fields.
    • Inserting, removing, or moving items in nested arrays now preserves metadata on the correct fields.
    • Corrected related stored values when nested array entries change.
  • Tests

    • Added coverage for nested-array insert and remove scenarios.

@coderabbitai

coderabbitai Bot commented Aug 27, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8155873b-d196-4d2b-8839-798b56474227

📥 Commits

Reviewing files that changed from the base of the PR and between 57a855b and e208b6a.

📒 Files selected for processing (3)
  • .changeset/fix-nested-array-meta-shift.md
  • packages/form-core/src/metaHelper.ts
  • packages/form-core/tests/FormApi.spec.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The form core now preserves nested-array metadata indices during move, remove, and insert operations. New tests verify blurred metadata and array values after nested insert and remove actions. A patch changeset documents the fix.

Changes

Nested array metadata shifting

Layer / File(s) Summary
Field-aware metadata shifting
packages/form-core/src/metaHelper.ts
Array move, remove, and insert operations pass the parent array field to shiftMeta. Index updates now target only the matching array segment.
Nested array behavior validation
packages/form-core/tests/FormApi.spec.ts, .changeset/fix-nested-array-meta-shift.md
Tests verify metadata and values after nested array insert and remove operations. The changeset documents the patch.
Estimated code review effort: 2 (Simple) ~15 minutes

Merge Risk: ⚪ Minimal · up to e208b

This localized fix corrects nested-array field metadata shifting and includes regression coverage; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: pascalmh

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: fixing nested array metadata index shifting in form-core.
Description check ✅ Passed The description clearly explains the defect, root cause, fix, affected helpers, branch scope, regression tests, and validation results. The checklist boxes are not marked, but the required testing and…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description clearly explains the defect, root cause, fix, affected helpers, branch scope, regression tests, and validation results. The checklist boxes are not marked, but the required testing and changeset information is otherwise documented.

Full details: Docstring Coverage

Explanation

Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant