Skip to content

Commit

Permalink
Merge pull request #53566 from dominictb/fix/48122-scroll-cursor-into…
Browse files Browse the repository at this point in the history
…-view-when-focus

scroll cursor into view when focus
  • Loading branch information
deetergp authored Dec 6, 2024
2 parents 34f3e8a + 5c853cb commit d0629ce
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
diff --git a/node_modules/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/node_modules/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm
index db7cba4..6937720 100644
--- a/node_modules/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm
+++ b/node_modules/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm
@@ -88,6 +88,7 @@ - (void)didMoveToWindow
const auto &props = static_cast<const TextInputProps &>(*_props);
if (props.autoFocus) {
[_backedTextInputView becomeFirstResponder];
+ [self scrollCursorIntoView];
}
_didMoveToWindow = YES;
[self initializeReturnKeyType];
@@ -301,6 +302,7 @@ - (void)textInputDidBeginEditing
[self textInputDidChangeSelection];
}

+ [self scrollCursorIntoView];
if (_eventEmitter) {
static_cast<const TextInputEventEmitter &>(*_eventEmitter).onFocus([self _textInputMetrics]);
}
@@ -674,6 +676,15 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
_lastStringStateWasUpdatedWith = attributedString;
}

+- (void)scrollCursorIntoView {
+ UITextRange *selectedRange = _backedTextInputView.selectedTextRange;
+ if (selectedRange.empty) {
+ NSInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
+ toPosition:selectedRange.start];
+ [_backedTextInputView scrollRangeToVisible:NSMakeRange(offsetStart, 0)];
+ }
+}
+
- (void)_setMultiline:(BOOL)multiline
{
[_backedTextInputView removeFromSuperview];

0 comments on commit d0629ce

Please sign in to comment.