Skip to content

Commit 601fae6

Browse files
coadoclaude
andcommitted
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 <noreply@anthropic.com>
1 parent fe398e5 commit 601fae6

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ AndroidTextInputProps::AndroidTextInputProps(
151151
{false})),
152152
fontWeight(convertRawProp(context, rawProps, "fontWeight", sourceProps.fontWeight, {})),
153153
fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
154+
onKeyPress(convertRawProp(context, rawProps, "onKeyPress", sourceProps.onKeyPress, {false})),
155+
onSelectionChange(convertRawProp(context, rawProps,
156+
"onSelectionChange",
157+
sourceProps.onSelectionChange,
158+
{false})),
159+
onContentSizeChange(convertRawProp(context, rawProps,
160+
"onContentSizeChange",
161+
sourceProps.onContentSizeChange,
162+
{false})),
163+
onScroll(convertRawProp(context, rawProps, "onScroll", sourceProps.onScroll, {false})),
154164
// See AndroidTextInputComponentDescriptor for usage
155165
// TODO T63008435: can these, and this feature, be removed entirely?
156166
hasPadding(hasValue(rawProps, sourceProps.hasPadding, "padding")),
@@ -227,6 +237,10 @@ void AndroidTextInputProps::setProp(
227237
RAW_SET_PROP_SWITCH_CASE_BASIC(includeFontPadding);
228238
RAW_SET_PROP_SWITCH_CASE_BASIC(fontWeight);
229239
RAW_SET_PROP_SWITCH_CASE_BASIC(fontFamily);
240+
RAW_SET_PROP_SWITCH_CASE_BASIC(onKeyPress);
241+
RAW_SET_PROP_SWITCH_CASE_BASIC(onSelectionChange);
242+
RAW_SET_PROP_SWITCH_CASE_BASIC(onContentSizeChange);
243+
RAW_SET_PROP_SWITCH_CASE_BASIC(onScroll);
230244

231245
case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): {
232246
fromRawValue(context, value, this->value, {});
@@ -628,6 +642,22 @@ folly::dynamic AndroidTextInputProps::getDiffProps(
628642
: folly::dynamic(nullptr);
629643
}
630644

645+
if (onKeyPress != oldProps->onKeyPress) {
646+
result["onKeyPress"] = onKeyPress;
647+
}
648+
649+
if (onSelectionChange != oldProps->onSelectionChange) {
650+
result["onSelectionChange"] = onSelectionChange;
651+
}
652+
653+
if (onContentSizeChange != oldProps->onContentSizeChange) {
654+
result["onContentSizeChange"] = onContentSizeChange;
655+
}
656+
657+
if (onScroll != oldProps->onScroll) {
658+
result["onScroll"] = onScroll;
659+
}
660+
631661
return result;
632662
}
633663

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ class AndroidTextInputProps final : public BaseTextInputProps {
109109
std::string fontWeight{};
110110
std::string fontFamily{};
111111

112+
/*
113+
* Whether JS listens to these events. ReactTextInputManager only sets up the
114+
* native watchers for them when these props are sent.
115+
*/
116+
bool onKeyPress{false};
117+
bool onSelectionChange{false};
118+
bool onContentSizeChange{false};
119+
bool onScroll{false};
120+
112121
/**
113122
* Auxiliary information to detect if these props are set or not.
114123
* See AndroidTextInputComponentDescriptor for usage.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <gtest/gtest.h>
9+
10+
#if defined(ANDROID) && defined(RN_SERIALIZABLE_STATE)
11+
12+
#include <react/renderer/components/androidtextinput/AndroidTextInputProps.h>
13+
#include <react/test_utils/PropsPayloads.h>
14+
15+
namespace facebook::react {
16+
17+
// ReactTextInputManager only sets up the event watchers when these props are
18+
// sent. JS sends a handler as `true`, and false resets it like null does.
19+
TEST(AndroidTextInputPropsDiffTest, eventHandlerPropsAreSent) {
20+
using folly::dynamic;
21+
expectPayloadsMatchRawProps<AndroidTextInputProps>({
22+
{dynamic::object("onKeyPress", true)("onSelectionChange", true)(
23+
"onContentSizeChange", true)("onScroll", true)},
24+
{dynamic::object("onKeyPress", nullptr)("onSelectionChange", nullptr)(
25+
"onContentSizeChange", nullptr)("onScroll", nullptr),
26+
dynamic::object("onKeyPress", false)("onSelectionChange", false)(
27+
"onContentSizeChange", false)("onScroll", false)},
28+
});
29+
}
30+
31+
} // namespace facebook::react
32+
33+
#endif

‎scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,10 @@ class facebook::react::AndroidTextInputProps : public facebook::react::BaseTextI
11841184
public bool hasPaddingTop;
11851185
public bool hasPaddingVertical;
11861186
public bool includeFontPadding;
1187+
public bool onContentSizeChange;
1188+
public bool onKeyPress;
1189+
public bool onScroll;
1190+
public bool onSelectionChange;
11871191
public bool secureTextEntry;
11881192
public bool selectTextOnFocus;
11891193
public bool showSoftInputOnFocus;

0 commit comments

Comments
 (0)