Fix: Wrap commit() in flushSync on Enter in NumberField#9683
Fix: Wrap commit() in flushSync on Enter in NumberField#9683tariqrasheed172001 wants to merge 2 commits intoadobe:mainfrom
Conversation
… is pressed in NumberField, commit() was called without flushSync, unlike the blur handler which wraps commit() in flushSync. This caused controlled form libraries reading React state immediately after Enter to receive stale values instead of the committed value. The browser synthesizes a trusted click on the form's submit button when Enter is pressed, firing onSubmit before the unguarded commit() has flushed to React state. This change wraps commit() in flushSync on Enter, making it consistent with the blur behavior and ensuring the value is flushed synchronously before any consumer reads form state. Fixes adobe#9671
|
@tariqrasheed172001 Hi, thanks for the PR! Would you mind signing the CLA as mentioned here? |
|
@LFDanLu Sure. |
|
Just closed/re-open switch for Adobe CLA re-check. |
|
Thanks for signing the CLA! I wasn't able to reproduce the original issue you described in your issue via https://stackblitz.com/edit/hjdh3uup?file=src%2FNumberField.tsx,src%2FExample.tsx, but I'm not very familiar with TanStack Form so I could be doing something wrong. Mind adding a story or a codesandbox that reproduces the original issue so I can validate the change as well? |
@LFDanLu I tried to create a reproduction but couldn't. The bug seems to be environment sensitive and only appears in our application. |
Fixes the inconsistency where
commit()is wrapped influshSyncon blur but not on Enter keydown inuseNumberField.Closes #9671
Problem
When a user types a value in NumberField and presses Enter:
commit()withoutflushSynconSubmitfires beforecommit()has flushed to React stateThis works correctly with native HTML forms because they read from the hidden input after
commit()has updatedstate.numberValue. But controlled form libraries that read React state directly are exposed to this race condition.Solution
Wrap
commit()influshSync()on Enter, consistent with how the blur handler already does it. This ensures the value is flushed synchronously before any consumer reads form state.✅ Pull Request Checklist:
📝 Test Instructions:
Before the fix:
defaultValue(e.g., 1)After the fix:
Existing behavior preserved:
🧢 Your Project:
Cryptlex (SaaS license management platform)