Skip to content

Commit deee037

Browse files
RSNarafacebook-github-bot
authored andcommitted
Back out "chore: Remove deprecated onTextInput callback"
Summary: Original commit changeset: 89101fa53cdc Original Phabricator Diff: D56804590 Changelog: [IOS] [Added] - Un-removed deprecated onTextInput callback Reviewed By: realsoelynn Differential Revision: D57082228 fbshipit-source-id: 30d62164788b94a9f3193bf78a7bee0c7ce464f6
1 parent 9da6546 commit deee037

11 files changed

Lines changed: 151 additions & 1 deletion

File tree

packages/react-native/Libraries/Components/TextInput/AndroidTextInputNativeComponent.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@ export type NativeProps = $ReadOnly<{|
385385
|}>,
386386
>,
387387

388+
onTextInput?: ?BubblingEventHandler<
389+
$ReadOnly<{|
390+
target: Int32,
391+
text: string,
392+
previousText: string,
393+
range: $ReadOnly<{|start: Double, end: Double|}>,
394+
|}>,
395+
>,
396+
388397
/**
389398
* Callback that is called when text input ends.
390399
*/
@@ -653,6 +662,12 @@ export const __INTERNAL_VIEW_CONFIG: PartialViewConfig = {
653662
captured: 'onSubmitEditingCapture',
654663
},
655664
},
665+
topTextInput: {
666+
phasedRegistrationNames: {
667+
bubbled: 'onTextInput',
668+
captured: 'onTextInputCapture',
669+
},
670+
},
656671
},
657672
directEventTypes: {
658673
topScroll: {

packages/react-native/Libraries/Components/TextInput/RCTTextInputViewConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const RCTTextInputViewConfig = {
7676
},
7777
},
7878
directEventTypes: {
79+
topTextInput: {
80+
registrationName: 'onTextInput',
81+
},
7982
topScroll: {
8083
registrationName: 'onScroll',
8184
},
@@ -150,6 +153,7 @@ const RCTTextInputViewConfig = {
150153
onSelectionChange: true,
151154
onContentSizeChange: true,
152155
onScroll: true,
156+
onTextInput: true,
153157
}),
154158
},
155159
};

packages/react-native/Libraries/Components/TextInput/TextInput.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,15 @@ export interface TextInputSubmitEditingEventData {
485485
text: string;
486486
}
487487

488+
/**
489+
* @see TextInputProps.onTextInput
490+
*/
491+
export interface TextInputTextInputEventData {
492+
text: string;
493+
previousText: string;
494+
range: {start: number; end: number};
495+
}
496+
488497
/**
489498
* @see https://reactnative.dev/docs/textinput#props
490499
*/
@@ -778,6 +787,16 @@ export interface TextInputProps
778787
| ((e: NativeSyntheticEvent<TextInputSubmitEditingEventData>) => void)
779788
| undefined;
780789

790+
/**
791+
* Callback that is called on new text input with the argument
792+
* `{ nativeEvent: { text, previousText, range: { start, end } } }`.
793+
*
794+
* This prop requires multiline={true} to be set.
795+
*/
796+
onTextInput?:
797+
| ((e: NativeSyntheticEvent<TextInputTextInputEventData>) => void)
798+
| undefined;
799+
781800
/**
782801
* Invoked on content scroll with
783802
* `{ nativeEvent: { contentOffset: { x, y } } }`.

packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
3636
@property (nonatomic, copy, nullable) RCTDirectEventBlock onSelectionChange;
3737
@property (nonatomic, copy, nullable) RCTDirectEventBlock onChange;
3838
@property (nonatomic, copy, nullable) RCTDirectEventBlock onChangeSync;
39+
@property (nonatomic, copy, nullable) RCTDirectEventBlock onTextInput;
3940
@property (nonatomic, copy, nullable) RCTDirectEventBlock onScroll;
4041

4142
@property (nonatomic, assign) NSInteger mostRecentEventCount;

packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputView.mm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,26 @@ - (NSString *)textInputShouldChangeText:(NSString *)text inRange:(NSRange)range
493493
}
494494
}
495495

496+
NSString *previousText = [backedTextInputView.attributedText.string copy] ?: @"";
497+
496498
if (range.location + range.length > backedTextInputView.attributedText.string.length) {
497499
_predictedText = backedTextInputView.attributedText.string;
498500
} else if (text != nil) {
499501
_predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range
500502
withString:text];
501503
}
502504

505+
if (_onTextInput) {
506+
_onTextInput(@{
507+
// We copy the string here because if it's a mutable string it may get released before we stop using it on a
508+
// different thread, causing a crash.
509+
@"text" : [text copy],
510+
@"previousText" : previousText,
511+
@"range" : @{@"start" : @(range.location), @"end" : @(range.location + range.length)},
512+
@"eventCount" : @(_nativeEventCount),
513+
});
514+
}
515+
503516
return text; // Accepting the change.
504517
}
505518

packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputViewManager.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ @implementation RCTBaseTextInputViewManager {
6464
RCT_EXPORT_VIEW_PROPERTY(onKeyPressSync, RCTDirectEventBlock)
6565
RCT_EXPORT_VIEW_PROPERTY(onChangeSync, RCTDirectEventBlock)
6666
RCT_EXPORT_VIEW_PROPERTY(onSelectionChange, RCTDirectEventBlock)
67+
RCT_EXPORT_VIEW_PROPERTY(onTextInput, RCTDirectEventBlock)
6768
RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
6869

6970
RCT_EXPORT_VIEW_PROPERTY(mostRecentEventCount, NSInteger)

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,14 @@ export type NativeProps = $ReadOnly<{|
22812281
contentSize: $ReadOnly<{| width: Double, height: Double |}>,
22822282
|}>,
22832283
>,
2284+
onTextInput?: ?BubblingEventHandler<
2285+
$ReadOnly<{|
2286+
target: Int32,
2287+
text: string,
2288+
previousText: string,
2289+
range: $ReadOnly<{| start: Double, end: Double |}>,
2290+
|}>,
2291+
>,
22842292
onEndEditing?: ?BubblingEventHandler<
22852293
$ReadOnly<{| target: Int32, text: string |}>,
22862294
>,

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7662,6 +7662,15 @@ public class com/facebook/react/views/textinput/ReactTextChangedEvent : com/face
76627662
public fun getEventName ()Ljava/lang/String;
76637663
}
76647664

7665+
public class com/facebook/react/views/textinput/ReactTextInputEvent : com/facebook/react/uimanager/events/Event {
7666+
public static final field EVENT_NAME Ljava/lang/String;
7667+
public fun <init> (IILjava/lang/String;Ljava/lang/String;II)V
7668+
public fun <init> (ILjava/lang/String;Ljava/lang/String;II)V
7669+
public fun canCoalesce ()Z
7670+
protected fun getEventData ()Lcom/facebook/react/bridge/WritableMap;
7671+
public fun getEventName ()Ljava/lang/String;
7672+
}
7673+
76657674
public final class com/facebook/react/views/textinput/ReactTextInputLocalData {
76667675
public fun <init> (Landroid/widget/EditText;)V
76677676
public fun apply (Landroid/widget/EditText;)V
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
package com.facebook.react.views.textinput;
9+
10+
import androidx.annotation.Nullable;
11+
import com.facebook.react.bridge.Arguments;
12+
import com.facebook.react.bridge.WritableMap;
13+
import com.facebook.react.uimanager.common.ViewUtil;
14+
import com.facebook.react.uimanager.events.Event;
15+
16+
/**
17+
* Event emitted by EditText native view when text changes. VisibleForTesting from {@link
18+
* TextInputEventsTestCase}.
19+
*/
20+
public class ReactTextInputEvent extends Event<ReactTextInputEvent> {
21+
22+
public static final String EVENT_NAME = "topTextInput";
23+
24+
private String mText;
25+
private String mPreviousText;
26+
private int mRangeStart;
27+
private int mRangeEnd;
28+
29+
@Deprecated
30+
public ReactTextInputEvent(
31+
int viewId, String text, String previousText, int rangeStart, int rangeEnd) {
32+
this(ViewUtil.NO_SURFACE_ID, viewId, text, previousText, rangeStart, rangeEnd);
33+
}
34+
35+
public ReactTextInputEvent(
36+
int surfaceId, int viewId, String text, String previousText, int rangeStart, int rangeEnd) {
37+
super(surfaceId, viewId);
38+
mText = text;
39+
mPreviousText = previousText;
40+
mRangeStart = rangeStart;
41+
mRangeEnd = rangeEnd;
42+
}
43+
44+
@Override
45+
public String getEventName() {
46+
return EVENT_NAME;
47+
}
48+
49+
@Override
50+
public boolean canCoalesce() {
51+
// We don't want to miss any textinput event, as event data is incremental.
52+
return false;
53+
}
54+
55+
@Nullable
56+
@Override
57+
protected WritableMap getEventData() {
58+
WritableMap eventData = Arguments.createMap();
59+
WritableMap range = Arguments.createMap();
60+
range.putDouble("start", mRangeStart);
61+
range.putDouble("end", mRangeEnd);
62+
63+
eventData.putString("text", mText);
64+
eventData.putString("previousText", mPreviousText);
65+
eventData.putMap("range", range);
66+
67+
eventData.putInt("target", getViewTag());
68+
return eventData;
69+
}
70+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputKeyPressEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.facebook.react.uimanager.events.Event;
1515

1616
/** Event emitted by EditText native view when key pressed */
17-
/* package */ class ReactTextInputKeyPressEvent extends Event<ReactTextInputKeyPressEvent> {
17+
class ReactTextInputKeyPressEvent extends Event<ReactTextInputEvent> {
1818

1919
public static final String EVENT_NAME = "topKeyPress";
2020

0 commit comments

Comments
 (0)