From 051a0c5be5c1e1393b5c8f32ce2b156180f9cfde Mon Sep 17 00:00:00 2001 From: Dawid Malecki Date: Wed, 4 Nov 2026 13:19:13 +0100 Subject: [PATCH 1/2] Send TextInput event props when props diffing is enabled Summary: `ReactTextInputManager` only sets up the watchers behind `onKeyPress`, `onSelectionChange`, `onContentSizeChange` and `onScroll` once it receives the matching prop. `AndroidTextInputProps` had no fields for these props, so `getDiffProps` never sent them and the events never fired with props diffing on. `TextInput.js` always passes `onScroll` and `onSelectionChange`, so every TextInput would be affected. This adds the fields and sends them. JS sends a handler as `true`. Removing a handler sends `false`, which the setters treat the same as the `null` sent by raw props. TextInput doesn't use props diffing yet: `getDiffPropsImplementationTarget()` returns `TextInput`, which doesn't match the `AndroidTextInput` component name. So the problem above only appears once that is enabled, and this change has no runtime effect until then. Changelog: [Internal] - Send TextInput event props when props diffing is enabled Test Plan: `AndroidTextInputPropsDiffTest.eventHandlerPropsAreSent` compares the props diffing payload with the raw props payload. It fails without this change and passes with it. `python -m scripts.cxx-api.parser --validate` passes at this commit. Co-Authored-By: Claude Opus 5.5 --- .../AndroidTextInputProps.cpp | 30 +++++++++++++++++ .../androidtextinput/AndroidTextInputProps.h | 9 +++++ .../tests/AndroidTextInputPropsDiffTest.cpp | 33 +++++++++++++++++++ .../ReactAndroidFrameworksCxx.api | 4 +++ 4 files changed, 76 insertions(+) create mode 100644 packages/react-native/ReactCommon/react/renderer/components/textinput/tests/AndroidTextInputPropsDiffTest.cpp 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..2a07516513e3 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 @@ -167,6 +167,16 @@ AndroidTextInputProps::AndroidTextInputProps( {false})), fontWeight(convertFontWeight(context, rawProps, sourceProps.fontWeight)), fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})), + onKeyPress(convertRawProp(context, rawProps, "onKeyPress", sourceProps.onKeyPress, {false})), + onSelectionChange(convertRawProp(context, rawProps, + "onSelectionChange", + sourceProps.onSelectionChange, + {false})), + onContentSizeChange(convertRawProp(context, rawProps, + "onContentSizeChange", + sourceProps.onContentSizeChange, + {false})), + onScroll(convertRawProp(context, rawProps, "onScroll", sourceProps.onScroll, {false})), // See AndroidTextInputComponentDescriptor for usage // TODO T63008435: can these, and this feature, be removed entirely? hasPadding(hasValue(rawProps, sourceProps.hasPadding, "padding")), @@ -242,6 +252,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); + RAW_SET_PROP_SWITCH_CASE_BASIC(onKeyPress); + RAW_SET_PROP_SWITCH_CASE_BASIC(onSelectionChange); + RAW_SET_PROP_SWITCH_CASE_BASIC(onContentSizeChange); + RAW_SET_PROP_SWITCH_CASE_BASIC(onScroll); case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): { fromRawValue(context, value, this->value, {}); @@ -651,6 +665,22 @@ folly::dynamic AndroidTextInputProps::getDiffProps( : folly::dynamic(nullptr); } + if (onKeyPress != oldProps->onKeyPress) { + result["onKeyPress"] = onKeyPress; + } + + if (onSelectionChange != oldProps->onSelectionChange) { + result["onSelectionChange"] = onSelectionChange; + } + + if (onContentSizeChange != oldProps->onContentSizeChange) { + result["onContentSizeChange"] = onContentSizeChange; + } + + if (onScroll != oldProps->onScroll) { + result["onScroll"] = 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..32b3070d33e2 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 @@ -109,6 +109,15 @@ class AndroidTextInputProps final : public BaseTextInputProps { std::string fontWeight{}; std::string fontFamily{}; + /* + * Whether JS listens to these events. ReactTextInputManager only sets up the + * native watchers for them when these props are sent. + */ + bool onKeyPress{false}; + bool onSelectionChange{false}; + bool onContentSizeChange{false}; + bool onScroll{false}; + /** * 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..8b628e21dfa7 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -1187,6 +1187,10 @@ class facebook::react::AndroidTextInputProps : public facebook::react::BaseTextI public bool hasPaddingTop; public bool hasPaddingVertical; public bool includeFontPadding; + public bool onContentSizeChange; + public bool onKeyPress; + public bool onScroll; + public bool onSelectionChange; public bool secureTextEntry; public bool selectTextOnFocus; public bool showSoftInputOnFocus; From cfefc1af2bc5f5baffb1b2a720206e07ec254139 Mon Sep 17 00:00:00 2001 From: Dawid Malecki Date: Tue, 1 Dec 2026 13:25:41 +0100 Subject: [PATCH 2/2] flatten callbacks bools --- .../AndroidTextInputProps.cpp | 110 +++++++++++++----- .../androidtextinput/AndroidTextInputProps.h | 40 +++++-- .../ReactAndroidFrameworksCxx.api | 19 ++- 3 files changed, 129 insertions(+), 40 deletions(-) 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 2a07516513e3..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,16 +215,7 @@ AndroidTextInputProps::AndroidTextInputProps( {false})), fontWeight(convertFontWeight(context, rawProps, sourceProps.fontWeight)), fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})), - onKeyPress(convertRawProp(context, rawProps, "onKeyPress", sourceProps.onKeyPress, {false})), - onSelectionChange(convertRawProp(context, rawProps, - "onSelectionChange", - sourceProps.onSelectionChange, - {false})), - onContentSizeChange(convertRawProp(context, rawProps, - "onContentSizeChange", - sourceProps.onContentSizeChange, - {false})), - onScroll(convertRawProp(context, rawProps, "onScroll", sourceProps.onScroll, {false})), + 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")), @@ -208,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, @@ -252,10 +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); - RAW_SET_PROP_SWITCH_CASE_BASIC(onKeyPress); - RAW_SET_PROP_SWITCH_CASE_BASIC(onSelectionChange); - RAW_SET_PROP_SWITCH_CASE_BASIC(onContentSizeChange); - RAW_SET_PROP_SWITCH_CASE_BASIC(onScroll); + 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, {}); @@ -665,20 +714,25 @@ folly::dynamic AndroidTextInputProps::getDiffProps( : folly::dynamic(nullptr); } - if (onKeyPress != oldProps->onKeyPress) { - result["onKeyPress"] = onKeyPress; - } - - if (onSelectionChange != oldProps->onSelectionChange) { - result["onSelectionChange"] = onSelectionChange; - } - - if (onContentSizeChange != oldProps->onContentSizeChange) { - result["onContentSizeChange"] = onContentSizeChange; - } - - if (onScroll != oldProps->onScroll) { - result["onScroll"] = onScroll; + 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 32b3070d33e2..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,14 +139,8 @@ class AndroidTextInputProps final : public BaseTextInputProps { std::string fontWeight{}; std::string fontFamily{}; - /* - * Whether JS listens to these events. ReactTextInputManager only sets up the - * native watchers for them when these props are sent. - */ - bool onKeyPress{false}; - bool onSelectionChange{false}; - bool onContentSizeChange{false}; - bool onScroll{false}; + // Named so it doesn't hide BaseViewProps::events. + AndroidTextInputEvents textInputEvents{}; /** * Auxiliary information to detect if these props are set or not. diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index 8b628e21dfa7..f5c61e289aff 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -1187,13 +1187,10 @@ class facebook::react::AndroidTextInputProps : public facebook::react::BaseTextI public bool hasPaddingTop; public bool hasPaddingVertical; public bool includeFontPadding; - public bool onContentSizeChange; - public bool onKeyPress; - public bool onScroll; - public bool onSelectionChange; 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; @@ -5569,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;