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
This commit is contained in:
zhongwuzw
2025-06-17 03:23:48 -07:00
committed by Facebook GitHub Bot
parent 742ffa39fb
commit 3bfbaa8455
2 changed files with 13 additions and 8 deletions
@@ -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(
@@ -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,