-
Notifications
You must be signed in to change notification settings - Fork 24.6k
Synchronizing selection of TextInput from onSelectionChange causes cursor to jump #50641
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
Comments
FYI: it works fine for us with RN 0.74.3. This may be a regression associated with recent revamps of TextInput. |
Hi @z4pf1sh , could you please verify this on the latest React Native version |
Hi, @devanshsaini11, I have tested with RN 0.79.0 and am sorry to report that this issue still persists in RN 0.79.0. However I do notice a community patch in this thread. I've cherry-picked the patch and it fixed the issue. Maybe consider taking a look into it? ;) |
Summary: Avoid incorrectly updating caret position Pull Request resolved: #50641 The caret position is updated incorrectly when a user is first typing if an zero-length selection is set. [IOS] [CHANGED] - Typing into TextInput now will not cause the caret position to update to the beginning when a zero-length selection is set. Pull Request resolved: #50680 Test Plan: Tested with the following code(a simplified version from the code in #50641) ```js const [selection, setSelection] = useState({start: -1, end: -1}); const onSelectionChange = ( evt: NativeSyntheticEvent<TextInputSelectionChangeEventData>, ) => { const {selection} = evt.nativeEvent; const {start, end} = selection; console.log('selection change: ', start, end); setSelection(selection); }; return ( <View style={{ position: 'absolute', top: 50, left: 30 }}> <TextInput placeholder="test" selection={selection} onSelectionChange={onSelectionChange} /> </View> ); ``` When using the main branch, the caret position will jump back to the beginning after the first typing. It works fine after applying this commit. Reviewed By: fabriziocucci Differential Revision: D72957245 Pulled By: cipolleschi fbshipit-source-id: 3586797332b35e86b17f386a35e7d192ff758f7e
Summary: Avoid incorrectly updating caret position Pull Request resolved: facebook#50641 The caret position is updated incorrectly when a user is first typing if an zero-length selection is set. ## Changelog: [IOS] [CHANGED] - Typing into TextInput now will not cause the caret position to update to the beginning when a zero-length selection is set. Pull Request resolved: facebook#50680 Test Plan: Tested with the following code(a simplified version from the code in facebook#50641) ```js const [selection, setSelection] = useState({start: -1, end: -1}); const onSelectionChange = ( evt: NativeSyntheticEvent<TextInputSelectionChangeEventData>, ) => { const {selection} = evt.nativeEvent; const {start, end} = selection; console.log('selection change: ', start, end); setSelection(selection); }; return ( <View style={{ position: 'absolute', top: 50, left: 30 }}> <TextInput placeholder="test" selection={selection} onSelectionChange={onSelectionChange} /> </View> ); ``` When using the main branch, the caret position will jump back to the beginning after the first typing. It works fine after applying this commit. Reviewed By: fabriziocucci Differential Revision: D72957245 Pulled By: cipolleschi fbshipit-source-id: 3586797332b35e86b17f386a35e7d192ff758f7e
Description
We're using React Native 0.77.2, and are trying to synchronize
onSelectionChange
event's data to a React State, bound toTextInput
'sselection
property. With our implementation, when the first letter is typed into theTextInput
, the cursor jumps to the beginning of theTextInput
. Subsequent inputs are fine without any problem.Steps to reproduce
npx @react-native-community/cli@latest init AwesomeProject --version 0.77.2
ios
folder, runpod install
, as one normally wouldAwesomeProject.xcworkspace
yarn run start
in the root folder to start Metro serverTextInput
widget and type anything into itTextInput
React Native Version
0.77.2
Affected Platforms
Runtime - iOS
Output of
npx @react-native-community/cli info
Stacktrace or Logs
Reproducer
https://github.com/z4pf1sh/repro-rn-textinput
Screenshots and Videos
Screen.Recording.2025-04-11.at.15.48.32.mov
The text was updated successfully, but these errors were encountered: