From fb8f5ea07f9fb5899e0c848792bd78792798a2dd Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Fri, 7 Feb 2020 14:08:20 -0800 Subject: [PATCH] Implement setTextAndSelection as view command in TextInput Summary: Changelog: Add view command `TextInput.setTextAndSelection` Reviewed By: JoshuaGross Differential Revision: D19789675 fbshipit-source-id: 8eb19bd21b1e153c9639507016a9f452c5f835b0 --- .../Text/TextInput/RCTBaseTextInputView.h | 4 ++++ .../Text/TextInput/RCTBaseTextInputView.m | 19 +++++++++++++++++++ .../TextInput/RCTBaseTextInputViewManager.m | 13 +++++++++++++ 3 files changed, 36 insertions(+) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.h b/Libraries/Text/TextInput/RCTBaseTextInputView.h index 99ccfa1e3a0..4acb4147ae6 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.h +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.h @@ -51,6 +51,10 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy) NSString *inputAccessoryViewID; @property (nonatomic, assign) UIKeyboardType keyboardType; +- (void)setText:(NSString *__nullable)text + selectionStart:(NSInteger)start + selectionEnd:(NSInteger)end; + @end NS_ASSUME_NONNULL_END diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index e01bd43b67c..0f0e285b277 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -193,6 +193,25 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) } } +- (void)setText:(NSString *__nullable)text + selectionStart:(NSInteger)start + selectionEnd:(NSInteger)end +{ + if (text) { + NSMutableAttributedString *mutableString = + [[NSMutableAttributedString alloc] initWithAttributedString:self.backedTextInputView.attributedText]; + [mutableString replaceCharactersInRange:NSMakeRange(0, mutableString.string.length) withString:text]; + self.backedTextInputView.attributedText = mutableString; + } + + UITextPosition *startPosition = [self.backedTextInputView positionFromPosition:self.backedTextInputView.beginningOfDocument + offset:start]; + UITextPosition *endPosition = [self.backedTextInputView positionFromPosition:self.backedTextInputView.beginningOfDocument + offset:end]; + UITextRange *range = [self.backedTextInputView textRangeFromPosition:startPosition toPosition:endPosition]; + [self.backedTextInputView setSelectedTextRange:range notifyDelegate:NO]; +} + - (void)setTextContentType:(NSString *)type { #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m index 412e33b980f..695d0f6c8fe 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m @@ -125,6 +125,19 @@ RCT_EXPORT_METHOD(setMostRecentEventCount : (nonnull NSNumber *)viewTag eventCou }]; } +RCT_EXPORT_METHOD(setTextAndSelection : (nonnull NSNumber *)viewTag + mostRecentEventCount : (NSInteger)mostRecentEventCount + value : (NSString *)value + start : (NSInteger)start + end : (NSInteger)end) +{ + [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { + RCTBaseTextInputView *view = (RCTBaseTextInputView *)viewRegistry[viewTag]; + view.mostRecentEventCount = mostRecentEventCount; + [view setText:value selectionStart:start selectionEnd:end]; + }]; +} + #pragma mark - RCTUIManagerObserver - (void)uiManagerWillPerformMounting:(__unused RCTUIManager *)uiManager