Skip to content

Commit bbee45b

Browse files
authored
feat: support react native 0.81 (#14)
* Refactor text input state sync and fix event type * Refactor text input state sync and fix event type
1 parent d094e2f commit bbee45b

File tree

3 files changed

+11
-100
lines changed

3 files changed

+11
-100
lines changed

android/src/main/java/com/textinput/TextInputViewManager.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import so.onekey.app.wallet.pasteinput.PasteWatcher
1515
@ReactModule(name = ReactTextInputManager.REACT_CLASS)
1616
class TextInputViewManager() : ReactTextInputManager() {
1717

18-
companion object {
18+
companion object {
1919
const val REACT_CLASS = "OneKeyTextInput"
2020
}
2121

@@ -33,9 +33,12 @@ class TextInputViewManager() : ReactTextInputManager() {
3333
return editText
3434
}
3535

36-
override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
37-
val baseEventTypeConstants = super.getExportedCustomDirectEventTypeConstants()?.toMutableMap()
38-
baseEventTypeConstants?.put("topPaste", MapBuilder.of("registrationName", "onPaste"))
36+
// Fix return type to match ReactTextInputManager's definition, which is Map<String, Any>
37+
override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any> {
38+
val baseEventTypeConstants =
39+
(super.getExportedCustomDirectEventTypeConstants() as? MutableMap<String, Any>)
40+
?: mutableMapOf()
41+
baseEventTypeConstants["topPaste"] = MapBuilder.of("registrationName", "onPaste")
3942
return baseEventTypeConstants
4043
}
4144

@@ -55,7 +58,8 @@ class TextInputViewManager() : ReactTextInputManager() {
5558

5659
init {
5760
val reactContext = UIManagerHelper.getReactContext(editText)
58-
mEventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, editText.id)!!
61+
mEventDispatcher =
62+
UIManagerHelper.getEventDispatcherForReactTag(reactContext, editText.id)!!
5963
mSurfaceId = UIManagerHelper.getSurfaceId(reactContext)
6064
}
6165

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@onekeyfe/react-native-text-input",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"description": "text-input",
55
"source": "./src/index.tsx",
66
"types": "./lib/typescript/src/index.d.ts",

src/Input.js

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import type {
2020
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
2121
import type {TextInputInstance, TextInputType} from 'react-native/Libraries/Components/TextInput/TextInput.flow';
2222

23-
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags.js';
2423
import usePressability from 'react-native/Libraries/Pressability/usePressability.js';
2524
import flattenStyle from 'react-native/Libraries/StyleSheet/flattenStyle.js';
2625
import StyleSheet, {
@@ -1010,7 +1009,7 @@ const emptyFunctionThatReturnsTrue = () => true;
10101009
* in native and in JavaScript. This is necessary due to the asynchronous nature
10111010
* of text input events.
10121011
*/
1013-
function useTextInputStateSynchronization_STATE({
1012+
function useTextInputStateSynchronization({
10141013
props,
10151014
mostRecentEventCount,
10161015
selection,
@@ -1086,94 +1085,6 @@ function useTextInputStateSynchronization_STATE({
10861085
return {setLastNativeText, setLastNativeSelection};
10871086
}
10881087

1089-
/**
1090-
* This hook handles the synchronization between the state of the text input
1091-
* in native and in JavaScript. This is necessary due to the asynchronous nature
1092-
* of text input events.
1093-
*/
1094-
function useTextInputStateSynchronization_REFS({
1095-
props,
1096-
mostRecentEventCount,
1097-
selection,
1098-
inputRef,
1099-
text,
1100-
viewCommands,
1101-
}: {
1102-
props: TextInputProps,
1103-
mostRecentEventCount: number,
1104-
selection: ?Selection,
1105-
inputRef: React.RefObject<null | TextInputInstance>,
1106-
text?: string,
1107-
viewCommands: ViewCommands,
1108-
}): {
1109-
setLastNativeText: string => void,
1110-
setLastNativeSelection: LastNativeSelection => void,
1111-
} {
1112-
const lastNativeTextRef = useRef<?Stringish>(props.value);
1113-
const lastNativeSelectionRef = useRef<LastNativeSelection>({
1114-
selection: {start: -1, end: -1},
1115-
mostRecentEventCount: mostRecentEventCount,
1116-
});
1117-
1118-
// This is necessary in case native updates the text and JS decides
1119-
// that the update should be ignored and we should stick with the value
1120-
// that we have in JS.
1121-
useLayoutEffect(() => {
1122-
const nativeUpdate: {text?: string, selection?: Selection} = {};
1123-
1124-
const lastNativeSelection = lastNativeSelectionRef.current.selection;
1125-
1126-
if (
1127-
lastNativeTextRef.current !== props.value &&
1128-
typeof props.value === 'string'
1129-
) {
1130-
nativeUpdate.text = props.value;
1131-
lastNativeTextRef.current = props.value;
1132-
}
1133-
1134-
if (
1135-
selection &&
1136-
lastNativeSelection &&
1137-
(lastNativeSelection.start !== selection.start ||
1138-
lastNativeSelection.end !== selection.end)
1139-
) {
1140-
nativeUpdate.selection = selection;
1141-
lastNativeSelectionRef.current = {selection, mostRecentEventCount};
1142-
}
1143-
1144-
if (Object.keys(nativeUpdate).length === 0) {
1145-
return;
1146-
}
1147-
1148-
if (inputRef.current != null) {
1149-
viewCommands.setTextAndSelection(
1150-
inputRef.current,
1151-
mostRecentEventCount,
1152-
text,
1153-
selection?.start ?? -1,
1154-
selection?.end ?? -1,
1155-
);
1156-
}
1157-
}, [
1158-
mostRecentEventCount,
1159-
inputRef,
1160-
props.value,
1161-
props.defaultValue,
1162-
selection,
1163-
text,
1164-
viewCommands,
1165-
]);
1166-
1167-
return {
1168-
setLastNativeText: lastNativeText => {
1169-
lastNativeTextRef.current = lastNativeText;
1170-
},
1171-
setLastNativeSelection: lastNativeSelection => {
1172-
lastNativeSelectionRef.current = lastNativeSelection;
1173-
},
1174-
};
1175-
}
1176-
11771088
/**
11781089
* A foundational component for inputting text into the app via a
11791090
* keyboard. Props provide configurability for several features, such as
@@ -1326,10 +1237,6 @@ function InternalTextInput(props: TextInputProps): React.Node {
13261237
: RCTSinglelineTextInputNativeCommands);
13271238

13281239
const [mostRecentEventCount, setMostRecentEventCount] = useState<number>(0);
1329-
const useTextInputStateSynchronization =
1330-
ReactNativeFeatureFlags.useRefsForTextInputState()
1331-
? useTextInputStateSynchronization_REFS
1332-
: useTextInputStateSynchronization_STATE;
13331240
const {setLastNativeText, setLastNativeSelection} =
13341241
useTextInputStateSynchronization({
13351242
props,

0 commit comments

Comments
 (0)