-
-
Notifications
You must be signed in to change notification settings - Fork 381
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
feat(react-form): add fieldOptions and dynamicFieldOptions APIs #1201
base: main
Are you sure you want to change the base?
feat(react-form): add fieldOptions and dynamicFieldOptions APIs #1201
Conversation
View your CI Pipeline Execution ↗ for commit 4b33df3.
☁️ Nx Cloud last updated this comment at |
…namicFieldOptions APIs
5fd9403
to
c002b5a
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1201 +/- ##
==========================================
+ Coverage 88.70% 88.80% +0.09%
==========================================
Files 28 29 +1
Lines 1257 1268 +11
Branches 329 331 +2
==========================================
+ Hits 1115 1126 +11
Misses 127 127
Partials 15 15 ☔ View full report in Codecov by Sentry. |
Added type and runtime tests, especially focused on making sure the errors object is still being properly inferred when any combo of form/field validations are used. Additionally, originally I wanted to do a single override on fieldOptions* but that wouldn't be possible if we want to allow any dynamic parameter within the function override. Sadly TS won't find the matching override if we make it dynamic. I think keeping it separate might be cleaner and easier to debug/iterate on anyways. |
…hared generic for expected test values to avoid redundancy.
…s and dynamicFieldOptions APIs.
Realized I had added a fieldOptions key inside the fieldOptions and dynamicFieldOptions APIs, removed it since unnecessary and aligns closer to what crutchcorn and I discussed here: I think we should keep the formOptions key/value as it makes it simpler to then only pull out the remaining options we want like this: export function fieldOptions<...>(options: { formOptions, .... }) {
//...
const { formOptions, ...fieldOpts } = options
return fieldOpts
} |
Related to #1202 Add fieldOptions and dynamicFieldOptions APIs that allow creating fieldOptions outside of JSX/html...
Implementation:
Usage in form:
Why?
Keep validation and listener logic out of JSX
The primary advantage is the clean separation of form validation and field behavior from component rendering code. This allows for leaner JSX without the need to define the configuration inline.
Centralized Tanstack Form Fields Configuration
Field configurations, validators, and behaviors can be defined in one central location, making it much easier to understand a form's complete behavior without navigating through scattered JSX files.
Dynamic Configuration Support
The ability to define configurations as functions (with arbitrary parameters) enables dynamic field configurations as needed! Developers can create configurations that adapt based on indices (perfect for field arrays) or other variables.
All done with Type Safety
With the implementation in this PR we provide strong TypeScript support with preserved type information throughout the configuration. This ensures that field-specific validators receive the correct value types and that configurations maintain their defined structure.