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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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 0c5b406.

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

☁️ Nx Cloud last updated this comment at 2025-04-25 12:22:47 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/lit-form

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

@tanstack/react-form

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

@tanstack/svelte-form

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

@tanstack/solid-form

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

@tanstack/vue-form

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

commit: 0c5b406

@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 88.95%. Comparing base (ba66490) to head (0c5b406).
Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1404      +/-   ##
==========================================
+ Coverage   88.52%   88.95%   +0.42%     
==========================================
  Files          28       31       +3     
  Lines        1316     1394      +78     
  Branches      343      351       +8     
==========================================
+ Hits         1165     1240      +75     
- Misses        134      137       +3     
  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
…FieldValues`

Co-authored-by: LeCarbonator <18158911+LeCarbonator@users.noreply.github.com>
@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
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.

2 participants