Fix <Form> ignores resetOptions when record changes
#11099
+136
−2
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
When using
<Form>or<SimpleForm>withresetOptions={{ keepDirtyValues: true }}(typically for AutoSave), dirty form values were lost when the record was refetched.Scenario:
This is a follow-up fix to #8911. While that PR forwarded
resetOptionstouseForm, it didn't pass it to the manualreset()call in theuseEffect, causingkeepDirtyValuesto be ignored when the record is refetched.Solution
useAugmentedForm.tsto passresetOptionstoreset()when the record changesresetOptions(e.g.,keepDirtyValues: true)resetOptionsto theUseFormOwnPropsinterfaceAdded unit tests in
Form.spec.tsxcovering:keepDirtyValues: truepreserves user input during refetch (same ID)keepDirtyValues, server values overwrite user inputHow To Test
resetOptions={{ keepDirtyValues: true }}and AutoSave enabledmake test- Unit tests pass (2595 passed, 9 unrelated failures on master)npx jest packages/ra-core/src/form/Form.spec.tsx- All 40 tests passAdditional Checks
masterfor a bugfixForm.spec.tsx)Notes
useAugmentedFormtouseForm#8911 forwardedresetOptionstouseForm, but theuseEffectthat manually callsreset()when the record changes didn't passresetOptionsto thereset()call.resetOptions. This prevents dirty values from record A being incorrectly preserved when viewing record B.