TextInput `setSelection` method was moved to base class

Summary: This method was identical between two subclasses, so it was moved to baseclass.

Reviewed By: javache

Differential Revision: D5297401

fbshipit-source-id: 8f56bef33f9ab0184f69da76177b5e8da10d7514
This commit is contained in:
Valentin Shergin 2017-07-18 14:33:33 -07:00 committed by Facebook Github Bot
parent ee9697e515
commit da9a183e81
6 changed files with 29 additions and 46 deletions

View File

@ -21,7 +21,6 @@
@property (nonatomic, assign) BOOL caretHidden; @property (nonatomic, assign) BOOL caretHidden;
@property (nonatomic, assign) BOOL selectTextOnFocus; @property (nonatomic, assign) BOOL selectTextOnFocus;
@property (nonatomic, assign) BOOL blurOnSubmit; @property (nonatomic, assign) BOOL blurOnSubmit;
@property (nonatomic, assign) NSInteger mostRecentEventCount;
@property (nonatomic, strong) NSNumber *maxLength; @property (nonatomic, strong) NSNumber *maxLength;
@property (nonatomic, copy) RCTDirectEventBlock onSelectionChange; @property (nonatomic, copy) RCTDirectEventBlock onSelectionChange;

View File

@ -27,9 +27,7 @@
@implementation RCTTextField @implementation RCTTextField
{ {
RCTUITextField *_backedTextInput; RCTUITextField *_backedTextInput;
NSInteger _nativeEventCount;
BOOL _submitted; BOOL _submitted;
UITextRange *_previousSelectionRange;
NSString *_finalText; NSString *_finalText;
CGSize _previousContentSize; CGSize _previousContentSize;
} }
@ -71,26 +69,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
#pragma mark - Properties #pragma mark - Properties
- (void)setSelection:(RCTTextSelection *)selection
{
if (!selection) {
return;
}
UITextRange *currentSelection = _backedTextInput.selectedTextRange;
UITextPosition *start = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.start];
UITextPosition *end = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.end];
UITextRange *selectedTextRange = [_backedTextInput textRangeFromPosition:start toPosition:end];
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) {
_previousSelectionRange = selectedTextRange;
_backedTextInput.selectedTextRange = selectedTextRange;
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag);
}
}
- (NSString *)text - (NSString *)text
{ {
return _backedTextInput.text; return _backedTextInput.text;

View File

@ -20,6 +20,9 @@
@protected @protected
RCTBridge *_bridge; RCTBridge *_bridge;
RCTEventDispatcher *_eventDispatcher; RCTEventDispatcher *_eventDispatcher;
UITextRange *_previousSelectionRange;
NSInteger _nativeEventCount;
NSInteger _mostRecentEventCount;
} }
- (instancetype)initWithBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER; - (instancetype)initWithBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER;
@ -36,6 +39,8 @@
@property (nonatomic, copy) RCTDirectEventBlock onContentSizeChange; @property (nonatomic, copy) RCTDirectEventBlock onContentSizeChange;
@property (nonatomic, assign) NSInteger mostRecentEventCount;
- (void)invalidateContentSize; - (void)invalidateContentSize;
@end @end

View File

@ -16,6 +16,8 @@
#import <React/RCTUIManager.h> #import <React/RCTUIManager.h>
#import <React/UIView+React.h> #import <React/UIView+React.h>
#import "RCTTextSelection.h"
@implementation RCTTextInput { @implementation RCTTextInput {
CGSize _previousContentSize; CGSize _previousContentSize;
} }
@ -60,6 +62,28 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
[self setNeedsLayout]; [self setNeedsLayout];
} }
- (void)setSelection:(RCTTextSelection *)selection
{
if (!selection) {
return;
}
id<RCTBackedTextInputViewProtocol> backedTextInput = self.backedTextInputView;
UITextRange *currentSelection = backedTextInput.selectedTextRange;
UITextPosition *start = [backedTextInput positionFromPosition:backedTextInput.beginningOfDocument offset:selection.start];
UITextPosition *end = [backedTextInput positionFromPosition:backedTextInput.beginningOfDocument offset:selection.end];
UITextRange *selectedTextRange = [backedTextInput textRangeFromPosition:start toPosition:end];
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) {
_previousSelectionRange = selectedTextRange;
backedTextInput.selectedTextRange = selectedTextRange;
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", backedTextInput.text, eventLag);
}
}
#pragma mark - Content Size (in Yoga terms, without any insets) #pragma mark - Content Size (in Yoga terms, without any insets)
- (CGSize)contentSize - (CGSize)contentSize

View File

@ -28,7 +28,6 @@
@property (nonatomic, strong) UIColor *placeholderTextColor; @property (nonatomic, strong) UIColor *placeholderTextColor;
@property (nonatomic, copy) NSString *placeholder; @property (nonatomic, copy) NSString *placeholder;
@property (nonatomic, strong) UIFont *font; @property (nonatomic, strong) UIFont *font;
@property (nonatomic, assign) NSInteger mostRecentEventCount;
@property (nonatomic, strong) NSNumber *maxLength; @property (nonatomic, strong) NSNumber *maxLength;
@property (nonatomic, copy) RCTDirectEventBlock onChange; @property (nonatomic, copy) RCTDirectEventBlock onChange;

View File

@ -30,12 +30,10 @@
RCTText *_richTextView; RCTText *_richTextView;
NSAttributedString *_pendingAttributedText; NSAttributedString *_pendingAttributedText;
UITextRange *_previousSelectionRange;
NSString *_predictedText; NSString *_predictedText;
BOOL _blockTextShouldChange; BOOL _blockTextShouldChange;
BOOL _nativeUpdatesInFlight; BOOL _nativeUpdatesInFlight;
NSInteger _nativeEventCount;
} }
- (instancetype)initWithBridge:(RCTBridge *)bridge - (instancetype)initWithBridge:(RCTBridge *)bridge
@ -208,26 +206,6 @@ static NSAttributedString *removeReactTagFromString(NSAttributedString *string)
[self setNeedsLayout]; [self setNeedsLayout];
} }
- (void)setSelection:(RCTTextSelection *)selection
{
if (!selection) {
return;
}
UITextRange *currentSelection = _backedTextInput.selectedTextRange;
UITextPosition *start = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.start];
UITextPosition *end = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.end];
UITextRange *selectedTextRange = [_backedTextInput textRangeFromPosition:start toPosition:end];
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) {
_previousSelectionRange = selectedTextRange;
_backedTextInput.selectedTextRange = selectedTextRange;
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag);
}
}
- (NSString *)text - (NSString *)text
{ {
return _backedTextInput.text; return _backedTextInput.text;