-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Fix: Controlled form inputs reset on form submission #35359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
2280543 to
95ae85f
Compare
|
Hi @rdanciu11 There is a bug "similar" to this #30580 |
|
Hi @albr74. Actually, my implementation should already take care of that too because it checks React's fiber tree for control props ( I will write some tests right now to make sure. I wasn't aware of issue #30580. Thanks for the heads up. |
Forms were incorrectly being reset even when form actions threw errors. This was because requestFormReset was called before the action executed. Now requestFormReset is only called if the action completes without throwing a synchronous error. For async actions, the form reset is scheduled immediately after the action is called (before it resolves), which is the correct behavior. Added test to verify forms remain dirty when actions fail. Fixes facebook#35295
- Modified resetFormInstance to check React fiber for controlled inputs - Skips reset for inputs controlled by 'checked' or 'value' props - Added tests for controlled and uncontrolled checkbox/radio behavior - Fixes facebook#31695
- Add select element tests (controlled + uncontrolled) - Add textarea tests (controlled + uncontrolled) - Update tests to use outer container variable - Fixes facebook#30580
95ae85f to
0018d36
Compare
|
Update: I've confirmed the fix works for select elements and added tests I've updated this PR to include tests for:
The fix works across all these input types because it checks React's internal fiber tree for control props ( |
Summary
Fixes controlled form inputs incorrectly resetting after form submission with form actions.
Fixes
<select>component is subject to automatic form reset #30580The Bug
When a form with a function
actionis submitted, controlled form inputs (those withcheckedorvalueprops) are incorrectly reset to their default values. This breaks the expected behavior where controlled inputs should maintain their state.Affected input types:
<input type="text">)<select>)<textarea>)<input type="checkbox">)<input type="radio">)The Fix
Enhanced
resetFormInstance()inReactFiberConfigDOM.jsto:memoizedPropsfor control props:valueprop for text inputs, select elements, and textareascheckedprop for checkboxes and radio buttonsThis ensures controlled inputs maintain their state after form submission while uncontrolled inputs still reset as expected.
Tests
Added comprehensive test coverage in
ReactDOMForm-test.js:Controlled inputs (should NOT reset):
Uncontrolled inputs (should still reset):
All existing tests continue to pass
How Has This Been Tested?
yarn test ReactDOMForm-test