Skip to content

Commit

Permalink
Revert "Fix autocorrect onChangeText firing (facebook#22691)"
Browse files Browse the repository at this point in the history
This reverts commit 9bfa4fa.
  • Loading branch information
ericlewis committed Feb 28, 2019
1 parent 9bfa4fa commit 3a66142
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @interface RCTBackedTextFieldDelegateAdapter () <UITextFieldDelegate>

@implementation RCTBackedTextFieldDelegateAdapter {
__weak UITextField<RCTBackedTextInputViewProtocol> *_backedTextInputView;
BOOL _textDidChangeIsComing;
UITextRange *_previousSelectedTextRange;
}

Expand Down Expand Up @@ -57,12 +58,22 @@ - (BOOL)textFieldShouldEndEditing:(__unused UITextField *)textField

- (void)textFieldDidEndEditing:(__unused UITextField *)textField
{
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];
}

- (BOOL)textField:(__unused UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
BOOL result = [_backedTextInputView.textInputDelegate textInputShouldChangeTextInRange:range replacementText:string];
if (result) {
_textDidChangeIsComing = YES;
}
return result;
}

Expand All @@ -75,7 +86,9 @@ - (BOOL)textFieldShouldReturn:(__unused UITextField *)textField

- (void)textFieldDidChange
{
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];

// `selectedTextRangeWasSet` isn't triggered during typing.
[self textFieldProbablyDidChangeSelection];
}
Expand Down Expand Up @@ -123,11 +136,12 @@ - (void)textFieldProbablyDidChangeSelection

#pragma mark - RCTBackedTextViewDelegateAdapter (for UITextView)

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

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

Expand All @@ -136,21 +150,11 @@ - (instancetype)initWithTextView:(UITextView<RCTBackedTextInputViewProtocol> *)b
if (self = [super init]) {
_backedTextInputView = backedTextInputView;
backedTextInputView.delegate = self;
backedTextInputView.textStorage.delegate = self;
}

return self;
}

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


#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(__unused UITextView *)textView
Expand All @@ -170,6 +174,13 @@ - (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 @@ -185,9 +196,18 @@ - (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

0 comments on commit 3a66142

Please sign in to comment.