Skip to content

Commit

Permalink
Selection prop is applied for TextInput when component is mounting
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]
TextInput's predefined "selection" prop is now applied when view did move to window, and when attributed string is set.

Reviewed By: sammy-SC

Differential Revision: D30045271

fbshipit-source-id: e5495171b07a25e1e822421ff1627a8686cd0904
  • Loading branch information
Dmitry Rykun authored and facebook-github-bot committed Aug 23, 2021
1 parent 8434177 commit 9187e20
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ - (void)didMoveToWindow
}
_didMoveToWindow = YES;
}
[self _restoreTextSelection];
}

#pragma mark - RCTViewComponentView overrides
Expand Down Expand Up @@ -525,13 +526,27 @@ - (void)_updateState
return AttributedString::Range{(int)start, (int)(end - start)};
}

- (void)_restoreTextSelection
{
const auto selection = std::dynamic_pointer_cast<TextInputProps const>(_props)->selection.get_pointer();
if (selection == nullptr) {
return;
}
auto start = [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
offset:selection->start];
auto end = [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument offset:selection->end];
auto range = [_backedTextInputView textRangeFromPosition:start toPosition:end];
[_backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

- (void)_setAttributedString:(NSAttributedString *)attributedString
{
if ([self _textOf:attributedString equals:_backedTextInputView.attributedText]) {
return;
}
UITextRange *selectedRange = _backedTextInputView.selectedTextRange;
NSInteger oldTextLength = _backedTextInputView.attributedText.string.length;
if (![self _textOf:attributedString equals:_backedTextInputView.attributedText]) {
_backedTextInputView.attributedText = attributedString;
}
_backedTextInputView.attributedText = attributedString;
if (selectedRange.empty) {
// Maintaining a cursor position relative to the end of the old text.
NSInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
Expand All @@ -543,6 +558,7 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
[_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
notifyDelegate:YES];
}
[self _restoreTextSelection];
_lastStringStateWasUpdatedWith = attributedString;
}

Expand Down

0 comments on commit 9187e20

Please sign in to comment.