TextInput setSelection
"ignored" when TextInput has gone out of focus
#35005
Labels
Component: TextInput
Related to the TextInput component.
Needs: Triage 🔍
Never gets stale
Prevent those issues and PRs from getting stale
Description
When assigning a ref to
TextInput
there is asetSelection
method, which in general works great for changing the selection of the input, without needing to set aselection
prop.However, on android, there is an edge case where when the TextInput has gone out of focus while calling
setSelection
the new selection is ignored.See this video, where we have simple code that adds an emoji in the place of the selection:
expected.mov
Now we changed the code a bit and the
TextInput
will actually lose focus while we are changing the text. You can see that the selection is wrong. We said the selection to have a range of 0, but it still has the previous range:actual.mov
I see how I could fix this by using a
selection
prop. But this would cause other issues. I could see how I can hack my way around it by further waiting before I callsetSelection
, however, this can create UI glitches.I would just assume it to work when I called
setSelection
.On iOS something similar can be observed. Here the selection cursor gets set to the end (although
onSelectionChange
isn't reporting necessarily a change, there is one):ios_actual.mov
Version
0.70.3
Output of
npx react-native info
Steps to reproduce
yarn
yarn android
Important note: I assume this bug is a race condition as it doesn't happen 100% of the time you try!
Snack, code example, screenshot, or link to a repository
https://github.com/hannojg/TextInputSetSelectionReproduction
The text was updated successfully, but these errors were encountered: