Skip to content

Commit

Permalink
Switch order of onSelectionChange and onChange events on iOS (#35603)
Browse files Browse the repository at this point in the history
Summary:
Switched order of onSelectionChange and onChange events on iOS

This was already fixed but for fabric only https://github.com/facebook/react-native/blob/main/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm#L36-L46

This PR is a complementary of 7b48899

Fixes #28865

## Changelog

[IOS] [FIXED] - onSelectionChange() is fired before onChange() on multiline TextInput

Pull Request resolved: #35603

Test Plan:
Reproduce the minimal example provided here #28865
Verify that the events order is onChange then onSelectionChange with no duplicates

Reviewed By: cipolleschi

Differential Revision: D41947673

Pulled By: dmytrorykun

fbshipit-source-id: cf452a6101400b1b54295c83fa7735449d91e781
  • Loading branch information
s77rt authored and facebook-github-bot committed Dec 14, 2022
1 parent 33324b8 commit 64475ae
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ @interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate>

@implementation RCTBackedTextViewDelegateAdapter {
__weak UITextView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
NSAttributedString *_lastStringStateWasUpdatedWith;
BOOL _ignoreNextTextInputCall;
BOOL _textDidChangeIsComing;
UITextRange *_previousSelectedTextRange;
}
Expand Down Expand Up @@ -254,12 +256,21 @@ - (BOOL)textView:(__unused UITextView *)textView shouldChangeTextInRange:(NSRang

- (void)textViewDidChange:(__unused UITextView *)textView
{
if (_ignoreNextTextInputCall && [_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) {
_ignoreNextTextInputCall = NO;
return;
}
_lastStringStateWasUpdatedWith = _backedTextInputView.attributedText;
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
}

- (void)textViewDidChangeSelection:(__unused UITextView *)textView
{
if (![_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) {
[self textViewDidChange:_backedTextInputView];
_ignoreNextTextInputCall = YES;
}
[self textViewProbablyDidChangeSelection];
}

Expand Down

0 comments on commit 64475ae

Please sign in to comment.