Skip to content

feat(form-core): add array method field.clearValues and form.clearFieldValues #1404

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

Merged
merged 4 commits into from
May 26, 2025

Conversation

Pascalmh
Copy link
Contributor

@Pascalmh Pascalmh commented Apr 11, 2025

Problem

When setting a field back to an empty array

const form = new FormApi({
  defaultValues: {
    items: [
        { firstName: "" }
    ],
  },
});

form.setFieldValue('items', []); <-- reset

items[0].firstName would still exist - when there's a validation on the firstName (on the Field-level) form.canSubmit would be false.

Solution

Adding array method field.clearValues and form.clearFieldValues

- form.setFieldValue('items', []);
+ form.clearFieldValues("items")

Copy link

nx-cloud bot commented Apr 11, 2025

View your CI Pipeline Execution ↗ for commit 1f4e90a.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m 41s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2025-05-26 05:14:07 UTC

Copy link

pkg-pr-new bot commented Apr 11, 2025

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1404

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1404

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1404

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1404

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1404

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1404

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1404

commit: 1f4e90a

@LeCarbonator
Copy link
Contributor

The library already adds multiple helper functions for arrays (pushValue(), insertValue(), swapValues()) because the meta has to be carefully accounted for during the update.

Following this principle, I suggest there should be one (or optionally two) helper methods added in this PR:

clearValues()

Clear the values from the array.

This technically can be done as edge case check in setFieldValue(), but I think this follows the established array methods better.

filterValues(callback)

Filter the values in the array based on this condition

The helper function would keep track of which indeces were removed and update the meta accordingly.

@LeCarbonator
Copy link
Contributor

The provided test case will always fail because the items field starts with the string items. This is an easy fix though, so not that big of a deal.

@Pascalmh
Copy link
Contributor Author

I'll rewrite the tests to use clearValues instead and change to assertions to be more explicit

@LeCarbonator
Copy link
Contributor

Sounds good. I got an implementation of clearValues ready for testing as well

@Pascalmh Pascalmh force-pushed the reset-form-delete-children branch 2 times, most recently from 5ebd548 to 76cc154 Compare April 14, 2025 06:27
@LeCarbonator
Copy link
Contributor

Since we're adding two new methods, the title should be changed to feat(form-core) so that the bot creates the proper semantic versioning

@Pascalmh Pascalmh changed the title fix(FormApi): setting a field to an empty array should delete its children feat(form-core): add array methods clearValues and filterValues Apr 15, 2025
Copy link

codecov bot commented Apr 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.24%. Comparing base (9011802) to head (1f4e90a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1404      +/-   ##
==========================================
+ Coverage   89.13%   89.24%   +0.11%     
==========================================
  Files          31       31              
  Lines        1417     1432      +15     
  Branches      362      366       +4     
==========================================
+ Hits         1263     1278      +15     
  Misses        137      137              
  Partials       17       17              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@LeCarbonator
Copy link
Contributor

How should onChange validation work in filterValues?

Currently:

  • swapValues calls it on array, array[fromIndex] and array[toIndex]
  • removeValue calls it on the array and all fields starting from index
  • insertValue calls it on the array and all fields starting from index

With this information, I deduced that

  • clearValues only needs to call it on the array itself

Should filterValues do:

  • array + validation starting from the first index that has a shifted meta
  • array + validation on all fields
  • array
    ?

@LeCarbonator LeCarbonator force-pushed the reset-form-delete-children branch from 5aaa21c to 3c2cfda Compare April 16, 2025 06:12

Verified

This commit was signed with the committer’s verified signature.
Pascalmh Pascal Küsgen
…FieldValues`

Co-authored-by: LeCarbonator <[email protected]>
@Pascalmh Pascalmh force-pushed the reset-form-delete-children branch from 57f8294 to 1884b7b Compare April 17, 2025 06:54
@Pascalmh Pascalmh changed the title feat(form-core): add array methods clearValues and filterValues feat(form-core): add array method field.clearValues and form.clearFieldValues Apr 17, 2025
Pascalmh and others added 3 commits April 25, 2025 14:20

Verified

This commit was signed with the committer’s verified signature.
Pascalmh Pascal Küsgen
@LeCarbonator LeCarbonator force-pushed the reset-form-delete-children branch from e684cef to 1f4e90a Compare May 26, 2025 05:10
@harry-whorlow harry-whorlow merged commit f10f43e into TanStack:main May 26, 2025
6 checks passed
@harry-whorlow
Copy link
Contributor

LGTM 🚀

@Pascalmh Pascalmh deleted the reset-form-delete-children branch May 27, 2025 04:37
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.

None yet

4 participants