form icon indicating copy to clipboard operation
form copied to clipboard

feat(form-core): add array method field.filterValues and form.filterFieldValues

Open Pascalmh opened this issue 8 months ago • 5 comments

Problem

When setting a field back to an empty array

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

form.setFieldValue('people', [
  { firstName: "Marry" }, // <-- "Bob" should be removed
]);

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 field.filterValues and form.filterFieldValues to TSF:

form.filterFieldValues(
  'people',
  ({firstName}) => firstName === "Marry"
)

Pascalmh avatar Apr 17 '25 06:04 Pascalmh

View your CI Pipeline Execution ↗ for commit f5c8c02a4f09d66529d20e48c31a8fcfead36769.

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

☁️ Nx Cloud last updated this comment at 2025-06-02 14:35:45 UTC

nx-cloud[bot] avatar Apr 17 '25 06:04 nx-cloud[bot]

More templates

@tanstack/angular-form

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

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

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

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

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

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

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

commit: f5c8c02

pkg-pr-new[bot] avatar Apr 17 '25 06:04 pkg-pr-new[bot]

Codecov Report

Attention: Patch coverage is 87.06897% with 15 lines in your changes missing coverage. Please review.

Project coverage is 88.65%. Comparing base (803f864) to head (f5c8c02).

Files with missing lines Patch % Lines
packages/form-core/src/metaHelper.ts 82.71% 13 Missing and 1 partial :warning:
packages/form-core/src/FormApi.ts 96.77% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1426      +/-   ##
==========================================
- Coverage   89.24%   88.65%   -0.59%     
==========================================
  Files          31       31              
  Lines        1432     1490      +58     
  Branches      366      386      +20     
==========================================
+ Hits         1278     1321      +43     
- Misses        137      151      +14     
- Partials       17       18       +1     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Apr 17 '25 06:04 codecov[bot]

I think if/when this gets merged, it would be great to rework the Arrays section of the docs to write about swap, insert, push, filter and clear. Thoughts? Should it be somewhere else?

LeCarbonator avatar Apr 20 '25 13:04 LeCarbonator

Oof, it did not like that refactor of metaHelper. I'll get to it later today.

Looks like it's beccause I'm trying to avoid a breaking change. I guess I can add some tests for that

LeCarbonator avatar Apr 22 '25 08:04 LeCarbonator