|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#import "RCTBackedTextInputDelegateAdapter.h" |
| 11 | + |
| 12 | +#pragma mark - RCTBackedTextFieldDelegateAdapter (for UITextField) |
| 13 | + |
| 14 | +static void *TextFieldSelectionObservingContext = &TextFieldSelectionObservingContext; |
| 15 | + |
| 16 | +@interface RCTBackedTextFieldDelegateAdapter () <UITextFieldDelegate> |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation RCTBackedTextFieldDelegateAdapter { |
| 20 | + __weak UITextField<RCTBackedTextInputViewProtocol> *_backedTextInput; |
| 21 | + __unsafe_unretained UITextField<RCTBackedTextInputViewProtocol> *_unsafeBackedTextInput; |
| 22 | +} |
| 23 | + |
| 24 | +- (instancetype)initWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *)backedTextInput |
| 25 | +{ |
| 26 | + if (self = [super init]) { |
| 27 | + _backedTextInput = backedTextInput; |
| 28 | + _unsafeBackedTextInput = backedTextInput; |
| 29 | + backedTextInput.delegate = self; |
| 30 | + |
| 31 | + [_backedTextInput addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged]; |
| 32 | + [_backedTextInput addTarget:self action:@selector(textFieldDidEndEditingOnExit) forControlEvents:UIControlEventEditingDidEndOnExit]; |
| 33 | + |
| 34 | + // We have to use `unsafe_unretained` pointer to `backedTextInput` for subscribing (and especially unsubscribing) for it |
| 35 | + // because `weak` pointers do not KVO complient, unfortunately. |
| 36 | + [_unsafeBackedTextInput addObserver:self forKeyPath:@"selectedTextRange" options:0 context:TextFieldSelectionObservingContext]; |
| 37 | + } |
| 38 | + |
| 39 | + return self; |
| 40 | +} |
| 41 | + |
| 42 | +- (void)dealloc |
| 43 | +{ |
| 44 | + [_backedTextInput removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged]; |
| 45 | + [_backedTextInput removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit]; |
| 46 | + [_unsafeBackedTextInput removeObserver:self forKeyPath:@"selectedTextRange" context:TextFieldSelectionObservingContext]; |
| 47 | +} |
| 48 | + |
| 49 | +#pragma mark - UITextFieldDelegate |
| 50 | + |
| 51 | +- (BOOL)textFieldShouldBeginEditing:(__unused UITextField *)textField |
| 52 | +{ |
| 53 | + return [_backedTextInput.textInputDelegate textInputShouldBeginEditing]; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)textFieldDidBeginEditing:(__unused UITextField *)textField |
| 57 | +{ |
| 58 | + [_backedTextInput.textInputDelegate textInputDidBeginEditing]; |
| 59 | +} |
| 60 | + |
| 61 | +- (BOOL)textFieldShouldEndEditing:(__unused UITextField *)textField |
| 62 | +{ |
| 63 | + return [_backedTextInput.textInputDelegate textInputShouldEndEditing]; |
| 64 | +} |
| 65 | + |
| 66 | +- (void)textFieldDidEndEditing:(__unused UITextField *)textField |
| 67 | +{ |
| 68 | + [_backedTextInput.textInputDelegate textInputDidEndEditing]; |
| 69 | +} |
| 70 | + |
| 71 | +- (BOOL)textField:(__unused UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string |
| 72 | +{ |
| 73 | + return [_backedTextInput.textInputDelegate textInputShouldChangeTextInRange:range replacementText:string]; |
| 74 | +} |
| 75 | + |
| 76 | +#pragma mark - Key Value Observing |
| 77 | + |
| 78 | +- (void)observeValueForKeyPath:(NSString *)keyPath |
| 79 | + ofObject:(nullable id)object |
| 80 | + change:(NSDictionary *)change |
| 81 | + context:(void *)context |
| 82 | +{ |
| 83 | + if (context == TextFieldSelectionObservingContext) { |
| 84 | + if ([keyPath isEqualToString:@"selectedTextRange"]) { |
| 85 | + [_backedTextInput.textInputDelegate textInputDidChangeSelection]; |
| 86 | + } |
| 87 | + |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + [super observeValueForKeyPath:keyPath |
| 92 | + ofObject:object |
| 93 | + change:change |
| 94 | + context:context]; |
| 95 | +} |
| 96 | + |
| 97 | +#pragma mark - UIControlEventEditing* Family Events |
| 98 | + |
| 99 | +- (void)textFieldDidChange |
| 100 | +{ |
| 101 | + [_backedTextInput.textInputDelegate textInputDidChange]; |
| 102 | +} |
| 103 | + |
| 104 | +- (void)textFieldDidEndEditingOnExit |
| 105 | +{ |
| 106 | + [_backedTextInput.textInputDelegate textInputDidEndEditingOnExit]; |
| 107 | +} |
| 108 | + |
| 109 | +#pragma mark - UIKeyboardInput (private UIKit protocol) |
| 110 | + |
| 111 | +// This method allows us to detect a [Backspace] `keyPress` |
| 112 | +// even when there is no more text in the `UITextField`. |
| 113 | +- (BOOL)keyboardInputShouldDelete:(__unused UITextField *)textField |
| 114 | +{ |
| 115 | + [_backedTextInput.textInputDelegate textInputShouldChangeTextInRange:NSMakeRange(0, 0) replacementText:@""]; |
| 116 | + return YES; |
| 117 | +} |
| 118 | + |
| 119 | +@end |
| 120 | + |
| 121 | +#pragma mark - RCTBackedTextViewDelegateAdapter (for UITextView) |
| 122 | + |
| 123 | +@interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate> |
| 124 | +@end |
| 125 | + |
| 126 | +@implementation RCTBackedTextViewDelegateAdapter { |
| 127 | + __weak UITextView<RCTBackedTextInputViewProtocol> *_backedTextInput; |
| 128 | +} |
| 129 | + |
| 130 | +- (instancetype)initWithTextView:(UITextView<RCTBackedTextInputViewProtocol> *)backedTextInput |
| 131 | +{ |
| 132 | + if (self = [super init]) { |
| 133 | + _backedTextInput = backedTextInput; |
| 134 | + backedTextInput.delegate = self; |
| 135 | + } |
| 136 | + |
| 137 | + return self; |
| 138 | +} |
| 139 | + |
| 140 | +#pragma mark - UITextViewDelegate |
| 141 | + |
| 142 | +- (BOOL)textViewShouldBeginEditing:(__unused UITextView *)textView |
| 143 | +{ |
| 144 | + return [_backedTextInput.textInputDelegate textInputShouldBeginEditing]; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)textViewDidBeginEditing:(__unused UITextView *)textView |
| 148 | +{ |
| 149 | + [_backedTextInput.textInputDelegate textInputDidBeginEditing]; |
| 150 | +} |
| 151 | + |
| 152 | +- (BOOL)textViewShouldEndEditing:(__unused UITextView *)textView |
| 153 | +{ |
| 154 | + return [_backedTextInput.textInputDelegate textInputShouldEndEditing]; |
| 155 | +} |
| 156 | + |
| 157 | +- (void)textViewDidEndEditing:(__unused UITextView *)textView |
| 158 | +{ |
| 159 | + [_backedTextInput.textInputDelegate textInputDidEndEditing]; |
| 160 | +} |
| 161 | + |
| 162 | +- (BOOL)textView:(__unused UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text |
| 163 | +{ |
| 164 | + return [_backedTextInput.textInputDelegate textInputShouldChangeTextInRange:range replacementText:text]; |
| 165 | +} |
| 166 | + |
| 167 | +- (void)textViewDidChange:(__unused UITextView *)textView |
| 168 | +{ |
| 169 | + [_backedTextInput.textInputDelegate textInputDidChange]; |
| 170 | +} |
| 171 | + |
| 172 | +- (void)textViewDidChangeSelection:(__unused UITextView *)textView |
| 173 | +{ |
| 174 | + [_backedTextInput.textInputDelegate textInputDidChangeSelection]; |
| 175 | +} |
| 176 | + |
| 177 | +@end |
0 commit comments