Fix #988: Update initialValues when field initialValue prop changes#543
Conversation
When a Field's initialValue prop changes (e.g., after submit when the saved value updates), the form's initialValues should be updated to match. Previously, the form would remain dirty even though the current value matched the new "saved" initialValue. This fix adds logic to update form.initialValues when: - Field's initialValue prop changes - Field already has a value - Without overwriting the user's current input Fixes final-form/react-final-form#988
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughCaches current field value/initialValue during registration, adds logic to update formState.initialValues when a field's Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/FinalForm.field-initialValue-988.test.ts`:
- Around line 45-46: Extract a small helper to return the last emitted form
state instead of repeating spy.mock.calls[...] — add a local function like last
= () => spy.mock.calls.at(-1)[0] and update assertions that currently compute
lastCall (e.g., the occurrences using spy.mock.calls[spy.mock.calls.length -
1][0] and subsequent expect(...initialValues).toEqual(...)) to use last() (e.g.,
expect(last().initialValues).toEqual(...)); apply the same replacement for the
other occurrences referenced (around the other assertions).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: d48f0f8b-d5e0-4b9c-92ff-830026af0b57
📒 Files selected for processing (2)
src/FinalForm.field-initialValue-988.test.tssrc/FinalForm.ts
CodeRabbit Review Comments AddressedPushed commit Changes MadeFile: Issue: CodeRabbit flagged repeated use of Fix: Extracted a local const last = () => spy.mock.calls.at(-1)[0]Then replaced all occurrences of the verbose patterns:
Applied consistently across all 3 test cases (lines 45–48, 64–70, 82–85, 101–104, 109–112, 124–131). Test ResultsAll 367 tests pass ✅ |
f5c6a47
|
Published in |
Fix #988: Update initialValues when field initialValue prop changes
Problem
When using Field-level
initialValueprops (instead of Form-levelinitialValues), if the field'sinitialValueprop changes after submit (e.g., to reflect a newly saved value), the form'sinitialValuesdoesn't update. This causes the field to remain dirty even though the current value matches the new "saved" initialValue.Example Scenario (Radio Buttons)
initialValue="A"→ ForminitialValues = {field: "A"}values = {field: "B"},dirty = trueinitialValue="B"(reflecting saved value)initialValuesstays as{field: "A"}dirtycalculation:"B" !== "A"= still dirty! ❌Root Cause
In
registerField(line 1002), the condition for updatinginitialValueswas:This fails when:
noValueInFormStateis FALSE (value exists)currentValue ("B") !== currentInitialValue ("A")is FALSESo the new
initialValueprop is ignored!Solution
Added a second condition that updates
initialValueswhen the field'sinitialValueprop changes, without overwriting the user's current input:Tests
Added comprehensive test coverage in
src/FinalForm.field-initialValue-988.test.ts:initialValuechanges after user input →initialValuesupdates,dirty = false✅initialValuechanges but user has different value →initialValuesupdates butvaluesunchanged ✅All existing tests still pass (367 total).
Related Issue
Fixes final-form/react-final-form#988
Checklist
Summary by CodeRabbit
Bug Fixes
Tests
Chores