fix(form-core): apply async defaultValues to untouched fields after other fields are edited#2246
Open
xianjianlf2 wants to merge 1 commit into
Open
Conversation
…dits FormApi.update() gated the whole defaultValues merge on the form-level `isTouched` flag, which is true as soon as any single field is edited. As a result, once the user touched one field, asynchronously-arriving default values were no longer applied to *any* field, leaving untouched fields empty. Drop the form-level gate and instead preserve values per-field: edited (touched) fields keep the user's input, while untouched fields — including ones that have not mounted yet — receive their newly-arrived defaults. Closes TanStack#2229
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough
ChangesAsync default value updates
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
FormApi.update()gated itsdefaultValuesmerge on the form-levelisTouchedflag (shouldUpdateValues = ... && !this.state.isTouched). Form-levelisTouchedbecomes true as soon as any single field is edited, so once the user touched one field, asynchronously-arrivingdefaultValueswere no longer applied to any field — untouched fields stayed empty.This makes it impossible to render a form that is usable while part of its default data is still loading: editing one field permanently blocks the async defaults for the others.
Fixes #2229.
Fix
Drop the form-level gate and merge values per-field instead:
This preserves the existing "don't wipe an edited field on update" behavior (still covered by the existing test) while letting untouched fields update.
Tests
Added
form-coreregression tests covering both a text field and an array field: after editing one field and then callingupdate()with new asyncdefaultValues, the untouched field now receives its default while the edited field keeps the user's value. Both fail onmainand pass with this change. Fullform-coreandreact-formsuites pass; typecheck/lint clean.Summary by CodeRabbit