From f249d21da073baff0fb22067a023f6015f448fa1 Mon Sep 17 00:00:00 2001 From: Liron Yahdav Date: Thu, 11 Nov 2021 13:33:52 -0800 Subject: [PATCH] Native changes for TextInput.setSelection method Summary: Native changes in preparation for adding a `setSelection` imperative method to `TextInput`. Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D31590771 fbshipit-source-id: eed40d1c2803fec713f2008ab8053a2812249715 --- Libraries/Text/TextInput/RCTBaseTextInputViewManager.m | 4 +++- .../TextInput/RCTTextInputComponentView.mm | 2 +- .../react/views/textinput/ReactTextInputManager.java | 9 +++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m index 34994ca4fd115c..77d70d14c769c8 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m @@ -132,7 +132,9 @@ - (void)setBridge:(RCTBridge *)bridge } RCTExecuteOnUIManagerQueue(^{ RCTBaseTextInputShadowView *shadowView = (RCTBaseTextInputShadowView *)[self.bridge.uiManager shadowViewForReactTag:viewTag]; - [shadowView setText:value]; + if (value) { + [shadowView setText:value]; + } [self.bridge.uiManager setNeedsLayout]; RCTExecuteOnMainQueue(^{ [view setSelectionStart:start selectionEnd:end]; diff --git a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index bbe622eebfc163..589ff99ac7cc38 100644 --- a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -420,7 +420,7 @@ - (void)setTextAndSelection:(NSInteger)eventCount return; } _comingFromJS = YES; - if (![value isEqualToString:_backedTextInputView.attributedText.string]) { + if (value && ![value isEqualToString:_backedTextInputView.attributedText.string]) { NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:value attributes:_backedTextInputView.defaultTextAttributes]; [self _setAttributedString:attributedString]; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index 0b6298fdd1f489..9581583b6cb00b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -288,16 +288,17 @@ public void receiveCommand( return; } - String text = args.getString(1); - int start = args.getInt(2); int end = args.getInt(3); if (end == UNSET) { end = start; } - reactEditText.maybeSetTextFromJS( - getReactTextUpdate(text, mostRecentEventCount, start, end)); + if (!args.isNull(1)) { + String text = args.getString(1); + reactEditText.maybeSetTextFromJS( + getReactTextUpdate(text, mostRecentEventCount, start, end)); + } reactEditText.maybeSetSelection(mostRecentEventCount, start, end); break; }