kirillzyusko / react-native-keyboard-controller

Keyboard manager which works in identical way on both iOS and Android
https://kirillzyusko.github.io/react-native-keyboard-controller/
MIT License
1.38k stars 55 forks source link

fix: incorrect selection coordinates when input grows #491

Closed kirillzyusko closed 2 days ago

kirillzyusko commented 4 days ago

📜 Description

Fixed incorrect selection coordinates when text gets moved to a new line and text input grows.

💡 Motivation and Context

The problem happens because our text is bigger and can not fit into current layout. If we read coordinates we get +Inf and of course it's not reasonable to send such events to JS.

To fix this problem we need to wait for layout update (next frame) and send event only after that. This is exactly what i did in this PR:

if textView.canSelectionFitIntoLayout {
  updateSelectionPosition(textInput: textView, sendEvent: onSelectionChange)
} else {
  // when multiline input grows we need to wait for layout to be updated
  // otherwise start/end positions will be incorrect (0/-1)
  DispatchQueue.main.asyncAfter(deadline: .now() + UIUtils.nextFrame) {
    updateSelectionPosition(textInput: textView, sendEvent: self.onSelectionChange)
  }
}

Closes https://github.com/kirillzyusko/react-native-keyboard-controller/issues/489

📢 Changelog

iOS

🤔 How Has This Been Tested?

Tested manually using code sample provided in the issue.

📸 Screenshots (if appropriate):

Before After
image image

📝 Checklist

github-actions[bot] commented 4 days ago

📊 Package size report

Current size Target Size Difference
144389 bytes 144183 bytes 206 bytes 📈