Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocorrect onChangeText firing (#22691) #23666

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,11 @@ - (void)textFieldProbablyDidChangeSelection

#pragma mark - RCTBackedTextViewDelegateAdapter (for UITextView)

@interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate>
@interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate, NSTextStorageDelegate>
@end

@implementation RCTBackedTextViewDelegateAdapter {
__weak UITextView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
BOOL _textDidChangeIsComing;
UITextRange *_previousSelectedTextRange;
}

Expand All @@ -155,6 +154,14 @@ - (instancetype)initWithTextView:(UITextView<RCTBackedTextInputViewProtocol> *)b
return self;
}

#pragma mark - NSTextStorageDelegate
- (void)textStorage:(NSTextStorage *)textStorage
didProcessEditing:(__unused NSTextStorageEditActions)editedMask
range:(__unused NSRange)editedRange
changeInLength:(__unused NSInteger)delta {
[_backedTextInputView.textInputDelegate textInputDidChange];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried a bit about over-firing. So, e.g. if only text attributes were changed, we don't want to send an event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check if this is the case & fix it if that does occur.

}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(__unused UITextView *)textView
Expand All @@ -174,13 +181,6 @@ - (BOOL)textViewShouldEndEditing:(__unused UITextView *)textView

- (void)textViewDidEndEditing:(__unused UITextView *)textView
{
if (_textDidChangeIsComing) {
// iOS does't call `textViewDidChange:` delegate method if the change was happened because of autocorrection
// which was triggered by losing focus. So, we call it manually.
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
}

[_backedTextInputView.textInputDelegate textInputDidEndEditing];
}

Expand All @@ -196,18 +196,9 @@ - (BOOL)textView:(__unused UITextView *)textView shouldChangeTextInRange:(NSRang
}

BOOL result = [_backedTextInputView.textInputDelegate textInputShouldChangeTextInRange:range replacementText:text];
if (result) {
_textDidChangeIsComing = YES;
}
return result;
}

- (void)textViewDidChange:(__unused UITextView *)textView
{
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
}

- (void)textViewDidChangeSelection:(__unused UITextView *)textView
{
[self textViewProbablyDidChangeSelection];
Expand Down