Merge PR #45: Add updateOnPristine option#62
Conversation
Resolved conflicts: - src/decorator.ts: Merged updateOnPristine logic into current TypeScript version - src/index.d.ts: Added updateOnPristine to Calculation type - src/types.ts: Added updateOnPristine to Calculation type - Removed src/types.js.flow (replaced by TypeScript in v2.0.0) The updateOnPristine option allows preventing calculations from running on pristine (initial) field values, fixing data corruption issues when loading form data from a database.
📝 WalkthroughWalkthroughAdds an optional Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@src/decorator.test.ts`:
- Around line 53-98: Add a new spec mirroring the existing "array of field
names" test but using a regex for the field matcher: call createForm and
createDecorator with field: /^foo$/ and updateOnPristine: false,
subscribe/register the same spies (spy, foo, bar), assert the initial
subscription is called once with initialValues unchanged, then call
form.change('foo', 'baz') and assert the decorator computes bar to 'bazbar' and
subscription calls reflect the intermediate and final values; place this test
alongside the existing it(...) block and use the same helpers createForm,
createDecorator, form.registerField and form.change to match the pattern in the
diff.
In `@src/decorator.ts`:
- Around line 62-68: Replace the bitwise NOT trick in the matches initializer
with a clearer existence check: in the Array.isArray(field) branch (the matches
function defined using field and name), change the "~field.indexOf(name)"
expression to a readable check such as "field.indexOf(name) !== -1" or use
"field.includes(name)" for exact string matches, keeping the existing
findIndex(RegExp) fallback intact so RegExp elements are still tested via f
instanceof RegExp and (f as RegExp).test(name).
In `@src/types.ts`:
- Around line 14-19: The Calculation type's property order is inconsistent with
src/index.d.ts; update the Calculation type definition so properties appear in
the same order as in index.d.ts: field, updates, updateOnPristine, isEqual
(preserve existing types and optionality for isEqual and updateOnPristine) to
keep type declarations consistent across the codebase.
1. src/types.ts: Reorder Calculation properties to match index.d.ts (field, updates, updateOnPristine, isEqual) 2. src/decorator.ts: Replace bitwise NOT (~) with clearer indexOf check (field.indexOf(name) !== -1) 3. src/decorator.test.ts: Add regex test case for updateOnPristine=false with regex field matcher
|
✅ All CodeRabbit comments addressed Per the updated review policy, I've addressed all three requested changes:
Changes pushed to |
This PR resolves the merge conflicts in #45 and brings the
updateOnPristinefeature to the current TypeScript codebase.Summary
Adds an optional
updateOnPristineflag to the Calculation type (defaults totruefor backward compatibility). When set tofalse, calculations will not run on pristine (initial/unchanged) field values.Problem Solved
Fixes data corruption issue when loading form data from a database - calculations would overwrite DB values on initial render. See #26 for detailed discussion.
Changes
updateOnPristineparameterupdateOnPristine?: booleanto Calculation typeupdateOnPristine?: booleanto type definitionBackward Compatibility
✅ No breaking changes -
updateOnPristinedefaults totrue, preserving current behavior.Example Usage
Testing
Related
Author
Merge conflicts resolved by @erikras-dinesh-agent
Original feature by community contributor (PR #45, Dec 2020)
Summary by CodeRabbit
New Features
updateOnPristinesetting for calculations to control whether derived updates run on initial pristine fields (defaults to true for compatibility).Tests
updateOnPristinebehavior across single fields, arrays of fields, and regex field matchers.