From 3bfbaa8455a2893f683be1a9669cf7e8205bb434 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 17 Jun 2025 03:23:48 -0700 Subject: [PATCH] Fabric: Only add selection of textinput payload when event is selectionChange (#51051) Summary: Fixes https://github.com/facebook/react-native/issues/51020: TextInputChangeEventData does not contain selection (see [here](https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Components/TextInput/TextInput.d.ts#L515-L518)). Additionally, Android does not have selection either (see [here](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextChangedEvent.kt#L33-L37)) (iOS old arch also not contains it). We can consolidate these to prevent any misleading information. ## Changelog: [IOS] [FIXED] - Fabric: Only add selection of textinput payload when event is selectionChange Pull Request resolved: https://github.com/facebook/react-native/pull/51051 Test Plan: TextInput `onChange`'s event should not contains `selection`. Reviewed By: javache Differential Revision: D76253999 Pulled By: cipolleschi fbshipit-source-id: 8a889cce4998be0e31f4aa33961759e7d228e06f --- .../textinput/TextInputEventEmitter.cpp | 18 +++++++++++------- .../textinput/TextInputEventEmitter.h | 3 ++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.cpp b/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.cpp index d5a7beab466..a9bc219f8bf 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.cpp @@ -11,7 +11,8 @@ namespace facebook::react { static jsi::Value textInputMetricsPayload( jsi::Runtime& runtime, - const TextInputEventEmitter::Metrics& textInputMetrics) { + const TextInputEventEmitter::Metrics& textInputMetrics, + bool includeSelectionState) { auto payload = jsi::Object(runtime); payload.setProperty( @@ -23,7 +24,7 @@ static jsi::Value textInputMetricsPayload( payload.setProperty(runtime, "eventCount", textInputMetrics.eventCount); - { + if (includeSelectionState) { auto selection = jsi::Object(runtime); selection.setProperty( runtime, "start", textInputMetrics.selectionRange.location); @@ -150,7 +151,7 @@ void TextInputEventEmitter::onContentSizeChange( void TextInputEventEmitter::onSelectionChange( const Metrics& textInputMetrics) const { - dispatchTextInputEvent("selectionChange", textInputMetrics); + dispatchTextInputEvent("selectionChange", textInputMetrics, true); } void TextInputEventEmitter::onEndEditing( @@ -178,10 +179,13 @@ void TextInputEventEmitter::onScroll(const Metrics& textInputMetrics) const { void TextInputEventEmitter::dispatchTextInputEvent( const std::string& name, - const Metrics& textInputMetrics) const { - dispatchEvent(name, [textInputMetrics](jsi::Runtime& runtime) { - return textInputMetricsPayload(runtime, textInputMetrics); - }); + const Metrics& textInputMetrics, + bool includeSelectionState) const { + dispatchEvent( + name, [includeSelectionState, textInputMetrics](jsi::Runtime& runtime) { + return textInputMetricsPayload( + runtime, textInputMetrics, includeSelectionState); + }); } void TextInputEventEmitter::dispatchTextInputContentSizeChangeEvent( diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.h index dae4755e796..dbce5751593 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/TextInputEventEmitter.h @@ -48,7 +48,8 @@ class TextInputEventEmitter : public ViewEventEmitter { private: void dispatchTextInputEvent( const std::string& name, - const Metrics& textInputMetrics) const; + const Metrics& textInputMetrics, + bool includeSelectionState = false) const; void dispatchTextInputContentSizeChangeEvent( const std::string& name,