fix(form-core): apply async defaultValues to untouched fields after other fields are edited #2246
main fromrefs/pull/2246/head·opened 🎯 Changes#
Fixes #2229. FormApi.update() previously gated default-value merging on the form-level touched state, so touching one field prevented asynchronously arriving defaults from applying to every other untouched field.
- Drop the form-level touched gate and merge updated defaults per field instead.
- Preserve user-edited values for touched fields.
- Apply new default values to untouched fields, including fields that have not mounted yet.
- Add regression coverage for both scalar fields and array fields.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
Notes#
The existing behavior of not wiping an edited field during update is preserved. The new behavior only applies newly arrived defaults to fields that are still untouched.
- 1
Checked
setBy/getByin utils.ts to confirm the merge approach actually does what the changeset claims.setByrebuilds the object path-segment by path-segment and only touches the exact path you give it, everything else comes from whatever you passed as the base object. So startingnextValuesfrom the full incomingoptions.defaultValuesand then overwriting just the touched field paths with their current live values viagetBy(this.state.values, fieldName)is exactly the right shape for "untouched fields get the new default, touched fields keep what the user typed," and it composes fine for nested/array paths sincesetBydoesn't care about depth.The loop iterates
this.state.fieldMeta, which only has entries for fields that have actually mounted or been interacted with, so a field that hasn't mounted yet simply has no entry and falls through to the new default untouched, which matches the changeset's claim about fields that "have not mounted yet." That's a detail that's easy to get wrong (checking some kind of "isMounted" flag instead of just relying on absence from the map), and this uses the simpler and correct one.Good that the second test specifically covers an array field (
asyncItems: []to['a', 'b']) rather than just a scalar, since arrays are exactly the shape most likely to be loaded asynchronously in a real form and where "touched" tracking has more edge cases than a single string field.One thing worth a second look, not a blocker: this only preserves values for fields with an existing
fieldMetaentry markedisTouched. If a field gets marked touched through something other than direct user edits (an arrayinsert/movemarking a parent array field touched as a side effect, for instance) rather than the field's own value being edited, this would also preserve that field's current value over the incoming default. That seems like the right call for an array a user has already manipulated, but wanted to flag it since it's the one place where "touched" doing double duty for both "edited" and "structurally mutated" could matter for less obvious field shapes than the ones in the tests.xianjianlf2(GitHub profile, opens in a new tab) linked this to fix #2229
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
FormApi.updatenow merges incoming default values with current values, preserving touched fields while updating untouched fields. Tests cover scalar and array defaults, and a patch changeset documents the fix.Changes#
Async default value updates
packages/form-core/src/FormApi.ts,.changeset/async-default-untouched-fields.mdFormApi.updateapplies changed defaults to untouched fields while retaining current values for touched fields.packages/form-core/tests/FormApi.spec.tsEstimated code review effort: 2 (Simple) | ~10 minutes
Possibly related PRs
defaultValuesupdates, including array-field update behavior.Suggested reviewers:
crutchcorn🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#2229by merging async defaults per field and preserving edited values.✨ 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.
❤️ Share