From 8434177722f70a9325f9a6adf46b5315b1f4ffa4 Mon Sep 17 00:00:00 2001 From: Dmitry Rykun Date: Mon, 23 Aug 2021 06:02:24 -0700 Subject: [PATCH] Added Selection prop to TextInputProps Summary: Changelog: [iOS][Added] 1. Added new primitive type "Selection" to C++ 2. Added property "selection" to TextInputProps 3. Added parser for that Reviewed By: sammy-SC Differential Revision: D30043256 fbshipit-source-id: eefa67ca23759761901cba1d2ab3052877a153a7 --- .../TextInput/RCTTextInputComponentView.mm | 1 - .../textinput/iostextinput/TextInputProps.cpp | 14 ++++---- .../textinput/iostextinput/TextInputProps.h | 5 +-- .../textinput/iostextinput/primitives.h | 6 ++++ .../textinput/iostextinput/propsConversions.h | 33 +++++++++++++++++++ 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index 9379c20cb6cb79..cb72b837cac3e9 100644 --- a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -208,7 +208,6 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const & if (newTextInputProps.inputAccessoryViewID != oldTextInputProps.inputAccessoryViewID) { _backedTextInputView.inputAccessoryViewID = RCTNSStringFromString(newTextInputProps.inputAccessoryViewID); } - [super updateProps:props oldProps:oldProps]; [self setDefaultInputAccessoryView]; diff --git a/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.cpp b/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.cpp index d7e4c3f93f195f..9e13cb97bb0574 100644 --- a/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.cpp +++ b/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.cpp @@ -82,6 +82,12 @@ TextInputProps::TextInputProps( "autoFocus", sourceProps.autoFocus, {})), + selection(convertRawProp( + context, + rawProps, + "selection", + sourceProps.selection, + better::optional())), inputAccessoryViewID(convertRawProp( context, rawProps, @@ -115,13 +121,5 @@ ParagraphAttributes TextInputProps::getEffectiveParagraphAttributes() const { return result; } -#ifdef ANDROID -folly::dynamic TextInputProps::getDynamic() const { - folly::dynamic props = folly::dynamic::object(); - props["value"] = value; - return props; -} -#endif - } // namespace react } // namespace facebook diff --git a/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.h b/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.h index 50b054e0b3ebb8..9fb44a4b2c1724 100644 --- a/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.h +++ b/ReactCommon/react/renderer/components/textinput/iostextinput/TextInputProps.h @@ -58,6 +58,7 @@ class TextInputProps final : public ViewProps, public BaseTextProps { int const mostRecentEventCount{0}; bool autoFocus{false}; + better::optional selection{}; std::string const inputAccessoryViewID{}; @@ -66,10 +67,6 @@ class TextInputProps final : public ViewProps, public BaseTextProps { */ TextAttributes getEffectiveTextAttributes(Float fontSizeMultiplier) const; ParagraphAttributes getEffectiveParagraphAttributes() const; - -#ifdef ANDROID - folly::dynamic getDynamic() const; -#endif }; } // namespace react diff --git a/ReactCommon/react/renderer/components/textinput/iostextinput/primitives.h b/ReactCommon/react/renderer/components/textinput/iostextinput/primitives.h index f13e9b44a32e92..f3b0fc2dfc5899 100644 --- a/ReactCommon/react/renderer/components/textinput/iostextinput/primitives.h +++ b/ReactCommon/react/renderer/components/textinput/iostextinput/primitives.h @@ -77,6 +77,12 @@ enum class KeyboardType { VisiblePassword, }; +class Selection final { + public: + int start{0}; + int end{0}; +}; + /* * Controls features of text inputs. */ diff --git a/ReactCommon/react/renderer/components/textinput/iostextinput/propsConversions.h b/ReactCommon/react/renderer/components/textinput/iostextinput/propsConversions.h index cfd0675c407c81..162e989112474c 100644 --- a/ReactCommon/react/renderer/components/textinput/iostextinput/propsConversions.h +++ b/ReactCommon/react/renderer/components/textinput/iostextinput/propsConversions.h @@ -145,5 +145,38 @@ static TextInputTraits convertRawProp( return traits; } +inline void fromRawValue( + const PropsParserContext &context, + const RawValue &value, + Selection &result) { + if (value.hasType>()) { + auto map = (better::map)value; + for (const auto &pair : map) { + if (pair.first == "start") { + result.start = pair.second; + } else if (pair.first == "end") { + result.end = pair.second; + } else { + LOG(ERROR) << "Unsupported Selection map key: " << pair.first; + react_native_assert(false); + } + } + return; + } + + react_native_assert(value.hasType>()); + if (value.hasType>()) { + auto array = (std::vector)value; + react_native_assert(array.size() == 2); + if (array.size() >= 2) { + result = {array.at(0), array.at(1)}; + } else { + result = {0, 0}; + LOG(ERROR) << "Unsupported Selection vector size: " << array.size(); + } + } else { + LOG(ERROR) << "Unsupported Selection type"; + } +} } // namespace react } // namespace facebook