react / react/react-native

TextInput `setSelection` "ignored" when TextInput has gone out of focus

Đang mở
#35,005 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Component: TextInput Needs: Triage :mag: Never gets stale
Ngôn ngữ chính
C++
Star
127k
Fork
25.3k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
4

Mô tả

Description

When assigning a ref to TextInput there is a setSelection method, which in general works great for changing the selection of the input, without needing to set a selection 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:

https://user-images.githubusercontent.com/16821682/196180861-27adfd53-41da-4a76-8c5b-f69b0889e43f.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:

https://user-images.githubusercontent.com/16821682/196181111-91472e22-f28d-4854-8c31-afe46bde403c.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 call setSelection, 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):

https://user-images.githubusercontent.com/16821682/196187268-7d14c6da-8269-4156-b5a1-12473ae9add5.mov

Version

0.70.3

Output of npx react-native info
System:
    OS: macOS 12.6
    CPU: (20) x64 Intel(R) Core(TM) i9-10910 CPU @ 3.60GHz
    Memory: 480.50 MB / 48.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.1/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
    Watchman: 2022.09.26.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 16.0, macOS 12.3, tvOS 16.0, watchOS 9.0
    Android SDK:
      API Levels: 26, 28, 29, 30, 31, 32
      Build Tools: 26.0.3, 28.0.3, 29.0.2, 29.0.3, 30.0.2, 30.0.3, 31.0.0, 32.0.0, 33.0.0
      System Images: android-26 | Google APIs Intel x86 Atom_64, android-27 | Google Play Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-30 | ARM 64 v8a, android-30 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom
      Android NDK: 21.4.7075529
  IDEs:
    Android Studio: Dolphin 2021.3.1 Patch 1 Dolphin 2021.3.1 Patch 1
    Xcode: 14.0.1/14A400 - /usr/bin/xcodebuild
  Languages:
    Java: javac 17 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.1.0 => 18.1.0 
    react-native: 0.70.3 => 0.70.3 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found
Steps to reproduce
  • Clone the repository
  • Run yarn
  • Run yarn android
  • When the app opens, click inside the TextInput and select any text
  • Then click on "Open emoji modal"
  • Click on "Add emoji"

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với repository tái hiện tại https://github.com/hannojg/TextInputSetSelectionReproduction. Làm theo các bước đã liệt kê cho yarn, Android, TextInput, emoji modal và Add emoji, sau đó so sánh hành vi của Android và iOS khi mất focus. Được xem là hoàn tất khi setSelection áp dụng đáng tin cậy phạm vi được yêu cầu sau khi văn bản thay đổi, bao gồm cả trường hợp mất focus đã được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
react-native
Lĩnh vực
mobile
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.