Skip to content

Commit d32ea66

Browse files
Olivier Bouilletfacebook-github-bot
Olivier Bouillet
authored andcommitted
fix(ios): selection range not respected when changing text or selection when selection is forced (#50166)
Summary: fix: #50132 The goal of this PR is to ensure selected TextInput scrolls to the selected range when text or selection change. The background of this feature check is to implement a rich text editor. ## Changelog: [IOS][FIXED] - Selection range not respected when changing text or selection when selection is forced Pull Request resolved: #50166 Test Plan: Tested with the sample linked to this pull request. As TextInput is a controlled component Here is a video of the sample with the patch: https://drive.google.com/file/d/1lS9_70quNqND_E8MjLFcRG6HoHcDkfmv/view?usp=drive_link First TextInput shows the initial issue reported in the ticket. Second TextInput shows the global behavior of the controlled component, the 2 buttons allows to force focus and the force text values I have also backport this part on 0.77.1 and test it in my app, it works fine for me (let's see if I have QA feedback) Reviewed By: javache Differential Revision: D71544064 Pulled By: cipolleschi fbshipit-source-id: ca49a3a2ca0f5f87307054efda31b0c779c31496
1 parent ddbb5fd commit d32ea66

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

+15-12
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ - (void)setTextAndSelection:(NSInteger)eventCount
547547
if (startPosition && endPosition) {
548548
UITextRange *range = [_backedTextInputView textRangeFromPosition:startPosition toPosition:endPosition];
549549
[_backedTextInputView setSelectedTextRange:range notifyDelegate:NO];
550+
// ensure we scroll to the selected position
551+
NSInteger offsetEnd = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
552+
toPosition:range.end];
553+
[_backedTextInputView scrollRangeToVisible:NSMakeRange(offsetEnd, 0)];
550554
}
551555
_comingFromJS = NO;
552556
}
@@ -721,18 +725,17 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
721725
// Updating the UITextView attributedText, for example changing the lineHeight, the color or adding
722726
// a new paragraph with \n, causes the cursor to move to the end of the Text and scroll.
723727
// This is fixed by restoring the cursor position and scrolling to that position (iOS issue 652653).
724-
if (selectedRange.empty) {
725-
// Maintaining a cursor position relative to the end of the old text.
726-
NSInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
727-
toPosition:selectedRange.start];
728-
NSInteger offsetFromEnd = oldTextLength - offsetStart;
729-
NSInteger newOffset = attributedString.string.length - offsetFromEnd;
730-
UITextPosition *position = [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
731-
offset:newOffset];
732-
[_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
733-
notifyDelegate:YES];
734-
[_backedTextInputView scrollRangeToVisible:NSMakeRange(offsetStart, 0)];
735-
}
728+
// Maintaining a cursor position relative to the end of the old text.
729+
NSInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
730+
toPosition:selectedRange.start];
731+
NSInteger offsetFromEnd = oldTextLength - offsetStart;
732+
NSInteger newOffset = attributedString.string.length - offsetFromEnd;
733+
UITextPosition *position = [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
734+
offset:newOffset];
735+
[_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
736+
notifyDelegate:YES];
737+
[_backedTextInputView scrollRangeToVisible:NSMakeRange(offsetStart, 0)];
738+
736739
[self _restoreTextSelection];
737740
[self _updateTypingAttributes];
738741
_lastStringStateWasUpdatedWith = attributedString;

0 commit comments

Comments
 (0)