diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp index e4ad212de108..b58a5d1bc99d 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp @@ -49,6 +49,54 @@ static std::string convertFontWeight( return convertRawProp(context, rawProps, "fontWeight", sourceValue, {}); } +static AndroidTextInputEvents convertRawProp( + const PropsParserContext& context, + const RawProps& rawProps, + const AndroidTextInputEvents& sourceValue) { + using Offset = AndroidTextInputEvents::Offset; + AndroidTextInputEvents result{}; + result.set( + Offset::KeyPress, + convertRawProp( + context, + rawProps, + "onKeyPress", + sourceValue[Offset::KeyPress], + false)); + result.set( + Offset::SelectionChange, + convertRawProp( + context, + rawProps, + "onSelectionChange", + sourceValue[Offset::SelectionChange], + false)); + result.set( + Offset::ContentSizeChange, + convertRawProp( + context, + rawProps, + "onContentSizeChange", + sourceValue[Offset::ContentSizeChange], + false)); + result.set( + Offset::Scroll, + convertRawProp( + context, rawProps, "onScroll", sourceValue[Offset::Scroll], false)); + return result; +} + +static void updateEventProp( + folly::dynamic& result, + const AndroidTextInputEvents& newEvents, + const AndroidTextInputEvents& oldEvents, + AndroidTextInputEvents::Offset offset, + const char* name) { + if (newEvents[offset] != oldEvents[offset]) { + result[name] = newEvents[offset]; + } +} + AndroidTextInputProps::AndroidTextInputProps( const PropsParserContext &context, const AndroidTextInputProps &sourceProps, @@ -167,6 +215,7 @@ AndroidTextInputProps::AndroidTextInputProps( {false})), fontWeight(convertFontWeight(context, rawProps, sourceProps.fontWeight)), fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})), + textInputEvents(convertRawProp(context, rawProps, sourceProps.textInputEvents)), // See AndroidTextInputComponentDescriptor for usage // TODO T63008435: can these, and this feature, be removed entirely? hasPadding(hasValue(rawProps, sourceProps.hasPadding, "padding")), @@ -198,6 +247,16 @@ AndroidTextInputProps::AndroidTextInputProps( hasPaddingEnd(hasValue(rawProps, sourceProps.hasPaddingEnd, "paddingEnd")) { } +#define TEXT_INPUT_EVENT_CASE(eventType) \ + case CONSTEXPR_RAW_PROPS_KEY_HASH("on" #eventType): { \ + bool res = false; \ + if (value.hasValue()) { \ + fromRawValue(context, value, res); \ + } \ + textInputEvents.set(AndroidTextInputEvents::Offset::eventType, res); \ + return; \ + } + void AndroidTextInputProps::setProp( const PropsParserContext& context, RawPropsPropNameHash hash, @@ -242,6 +301,10 @@ void AndroidTextInputProps::setProp( RAW_SET_PROP_SWITCH_CASE_BASIC(textAlign); RAW_SET_PROP_SWITCH_CASE_BASIC(includeFontPadding); RAW_SET_PROP_SWITCH_CASE_BASIC(fontFamily); + TEXT_INPUT_EVENT_CASE(KeyPress); + TEXT_INPUT_EVENT_CASE(SelectionChange); + TEXT_INPUT_EVENT_CASE(ContentSizeChange); + TEXT_INPUT_EVENT_CASE(Scroll); case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): { fromRawValue(context, value, this->value, {}); @@ -651,6 +714,27 @@ folly::dynamic AndroidTextInputProps::getDiffProps( : folly::dynamic(nullptr); } + if (textInputEvents != oldProps->textInputEvents) { + using Offset = AndroidTextInputEvents::Offset; + const auto& oldEvents = oldProps->textInputEvents; + updateEventProp( + result, textInputEvents, oldEvents, Offset::KeyPress, "onKeyPress"); + updateEventProp( + result, + textInputEvents, + oldEvents, + Offset::SelectionChange, + "onSelectionChange"); + updateEventProp( + result, + textInputEvents, + oldEvents, + Offset::ContentSizeChange, + "onContentSizeChange"); + updateEventProp( + result, textInputEvents, oldEvents, Offset::Scroll, "onScroll"); + } + return result; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h index 8bfc984e2529..0c4508649496 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h @@ -16,6 +16,7 @@ #include #include #include +#include #include namespace facebook::react { @@ -60,6 +61,35 @@ inline folly::dynamic toDynamic(const AndroidTextInputTextShadowOffsetStruct &va return dynamicValue; } +/* + * Whether JS listens to these events. ReactTextInputManager only sets up the + * native watchers for them when their props are sent. Stored in a uint8_t + * because a std::bitset takes at least a full machine word, even for 4 bits. + */ +struct AndroidTextInputEvents { + uint8_t bits{}; + + enum class Offset : uint8_t { + KeyPress = 0, + SelectionChange = 1, + ContentSizeChange = 2, + Scroll = 3, + }; + + constexpr bool operator[](const Offset offset) const + { + return ((bits >> static_cast(offset)) & 1) != 0; + } + + constexpr void set(const Offset offset, const bool value) + { + const auto mask = static_cast(1 << static_cast(offset)); + bits = static_cast(value ? (bits | mask) : (bits & ~mask)); + } + + bool operator==(const AndroidTextInputEvents &rhs) const = default; +}; + class AndroidTextInputProps final : public BaseTextInputProps { public: AndroidTextInputProps() = default; @@ -109,6 +139,9 @@ class AndroidTextInputProps final : public BaseTextInputProps { std::string fontWeight{}; std::string fontFamily{}; + // Named so it doesn't hide BaseViewProps::events. + AndroidTextInputEvents textInputEvents{}; + /** * Auxiliary information to detect if these props are set or not. * See AndroidTextInputComponentDescriptor for usage. diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/tests/AndroidTextInputPropsDiffTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/tests/AndroidTextInputPropsDiffTest.cpp new file mode 100644 index 000000000000..dcd7ba82b16a --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/tests/AndroidTextInputPropsDiffTest.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +#if defined(ANDROID) && defined(RN_SERIALIZABLE_STATE) + +#include +#include + +namespace facebook::react { + +// ReactTextInputManager only sets up the event watchers when these props are +// sent. JS sends a handler as `true`, and false resets it like null does. +TEST(AndroidTextInputPropsDiffTest, eventHandlerPropsAreSent) { + using folly::dynamic; + expectPayloadsMatchRawProps({ + {dynamic::object("onKeyPress", true)("onSelectionChange", true)( + "onContentSizeChange", true)("onScroll", true)}, + {dynamic::object("onKeyPress", nullptr)("onSelectionChange", nullptr)( + "onContentSizeChange", nullptr)("onScroll", nullptr), + dynamic::object("onKeyPress", false)("onSelectionChange", false)( + "onContentSizeChange", false)("onScroll", false)}, + }); +} + +} // namespace facebook::react + +#endif diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index f1811b25c34a..f5c61e289aff 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -1190,6 +1190,7 @@ class facebook::react::AndroidTextInputProps : public facebook::react::BaseTextI public bool secureTextEntry; public bool selectTextOnFocus; public bool showSoftInputOnFocus; + public facebook::react::AndroidTextInputEvents textInputEvents; public facebook::react::AndroidTextInputTextShadowOffsetStruct textShadowOffset; public facebook::react::Float fontSize; public facebook::react::Float letterSpacing; @@ -5565,6 +5566,20 @@ struct facebook::react::ActivePointer { public facebook::react::PointerEvent event; } +struct facebook::react::AndroidTextInputEvents { + public bool operator==(const facebook::react::AndroidTextInputEvents& rhs) const = default; + public constexpr bool operator[](const facebook::react::AndroidTextInputEvents::Offset offset) const; + public constexpr void set(const facebook::react::AndroidTextInputEvents::Offset offset, const bool value); + public uint8_t bits; +} + +enum facebook::react::AndroidTextInputEvents::Offset : uint8_t { + ContentSizeChange, + KeyPress, + Scroll, + SelectionChange, +} + struct facebook::react::AndroidTextInputTextShadowOffsetStruct { public double height; public double width;