Fix selection getting lost on Android on the new arch #335
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.
Details
The commit hook is updating the native state of the decorated text input on every shadow tree commit to override the default measurement method. Since the attributed string with the content of the input is also kept in the state, this was causing the state of the native
ReactEditText
to be updated on every commit, which effectively meant creating a new spannable on every commit and in turn losing the selection spans.This PR changes the update to also set the event counter to
-1
when it doesn't change between updates, which effectively makes the java part of the state updates a no-op in those cases, similarly to how it's done in the core of RN: https://github.com/facebook/react-native/blob/67392cef3c8e036adfd9f77808419221d70f8f18/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp#L155-L163(-1 instead of 0, like above, because 0 would be the most recent counter during the first render)
Always setting the event counter to
-1
also seemed to work fine but after quite a while of figuring out issues due to lost state updates in commit hooks, I'm not sure if we want to go that far now. It's something we may want to investigate in the future though.Screen.Recording.2024-04-26.at.16.43.06.mov
Screen.Recording.2024-04-26.at.15.34.11.mov
Manual Tests
This is testable in the example app, simply select some text. The handles should appear and be draggable. The text editing part should also work correctly.