react-native-nitro-input 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -0
- package/LICENSE +21 -0
- package/NitroInput.podspec +54 -0
- package/README.md +524 -0
- package/android/CMakeLists.txt +52 -0
- package/android/build.gradle +158 -0
- package/android/fix-prefab.gradle +51 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/JAmountFormatter.cpp +70 -0
- package/android/src/main/cpp/JAmountFormatter.hpp +45 -0
- package/android/src/main/cpp/JMaskEngine.cpp +81 -0
- package/android/src/main/cpp/JMaskEngine.hpp +52 -0
- package/android/src/main/cpp/JMorphEngine.cpp +132 -0
- package/android/src/main/cpp/JMorphEngine.hpp +54 -0
- package/android/src/main/cpp/JNitroInputWorklets.cpp +83 -0
- package/android/src/main/cpp/JNitroInputWorklets.hpp +40 -0
- package/android/src/main/cpp/JOutlineGeometry.cpp +58 -0
- package/android/src/main/cpp/JOutlineGeometry.hpp +55 -0
- package/android/src/main/cpp/cpp-adapter.cpp +19 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/AmountFormatter.kt +35 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +730 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +50 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/MorphEngine.kt +47 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +24 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +2176 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputWorklets.kt +34 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/OutlineGeometry.kt +71 -0
- package/cpp/AmountFormatter.cpp +346 -0
- package/cpp/AmountFormatter.hpp +88 -0
- package/cpp/MaskEngine.cpp +596 -0
- package/cpp/MaskEngine.hpp +136 -0
- package/cpp/MorphEngine.cpp +904 -0
- package/cpp/MorphEngine.hpp +213 -0
- package/cpp/OutlineGeometry.cpp +159 -0
- package/cpp/OutlineGeometry.hpp +107 -0
- package/cpp/worklets/HybridNitroInputWorklets.cpp +30 -0
- package/cpp/worklets/HybridNitroInputWorklets.hpp +30 -0
- package/cpp/worklets/NitroInputWorkletsBridge.cpp +203 -0
- package/cpp/worklets/NitroInputWorkletsBridge.hpp +52 -0
- package/cpp/worklets/NitroInputWorkletsBridgeJSI.hpp +23 -0
- package/ios/HybridMorphInputView.swift +710 -0
- package/ios/NitroInputCommands.mm +66 -0
- package/ios/NitroInputView.swift +2600 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +101 -0
- package/lib/commonjs/src/MorphInput.js +42 -0
- package/lib/commonjs/src/NitroInput.js +623 -0
- package/lib/commonjs/src/index.js +12 -0
- package/lib/commonjs/src/specs/NitroInput.nitro.js +2 -0
- package/lib/commonjs/src/specs/NitroInputWorklets.nitro.js +2 -0
- package/lib/commonjs/src/useNitroInputState.js +58 -0
- package/lib/commonjs/src/worklets.js +226 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +101 -0
- package/lib/module/package.json +1 -0
- package/lib/module/src/MorphInput.js +6 -0
- package/lib/module/src/NitroInput.js +584 -0
- package/lib/module/src/index.js +4 -0
- package/lib/module/src/specs/NitroInput.nitro.js +1 -0
- package/lib/module/src/specs/NitroInputWorklets.nitro.js +1 -0
- package/lib/module/src/useNitroInputState.js +55 -0
- package/lib/module/src/worklets.js +214 -0
- package/lib/typescript/src/MorphInput.d.ts +18 -0
- package/lib/typescript/src/NitroInput.d.ts +411 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +390 -0
- package/lib/typescript/src/specs/NitroInputWorklets.nitro.d.ts +15 -0
- package/lib/typescript/src/useNitroInputState.d.ts +55 -0
- package/lib/typescript/src/worklets.d.ts +117 -0
- package/nitro.json +37 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +84 -0
- package/nitrogen/generated/android/NitroInput+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +78 -0
- package/nitrogen/generated/android/NitroInputOnLoad.hpp +34 -0
- package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_double.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string.hpp +76 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string_double.hpp +76 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string_std__string_std__string_bool.hpp +76 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +1073 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +254 -0
- package/nitrogen/generated/android/c++/JNitroInputAffixAlign.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputAutoCapitalize.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputEasing.hpp +67 -0
- package/nitrogen/generated/android/c++/JNitroInputEffect.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputKeyboardAppearance.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputKeyboardType.hpp +79 -0
- package/nitrogen/generated/android/c++/JNitroInputLabelBehavior.hpp +58 -0
- package/nitrogen/generated/android/c++/JNitroInputMode.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputNotation.hpp +65 -0
- package/nitrogen/generated/android/c++/JNitroInputReturnKeyType.hpp +70 -0
- package/nitrogen/generated/android/c++/JNitroInputSignPlacement.hpp +58 -0
- package/nitrogen/generated/android/c++/JNitroInputSubmitBehavior.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputTextAlign.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputTextAlignVertical.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputVariant.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +529 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_bool.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double_double.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_std__string.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_std__string_double.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_std__string_std__string_std__string_bool.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +711 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputAffixAlign.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputAutoCapitalize.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputEasing.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputEffect.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputKeyboardAppearance.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputKeyboardType.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputLabelBehavior.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMode.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputNotation.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputReturnKeyType.kt +27 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSignPlacement.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSubmitBehavior.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextAlign.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextAlignVertical.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputVariant.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroInputViewManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroInputViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/NitroInput+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +81 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +348 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +92 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +43 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +26 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +769 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +690 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_double_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string_std__string_std__string_bool.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +154 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +1454 -0
- package/nitrogen/generated/ios/swift/NitroInputAffixAlign.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputAutoCapitalize.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputEasing.swift +52 -0
- package/nitrogen/generated/ios/swift/NitroInputEffect.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroInputKeyboardAppearance.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroInputKeyboardType.swift +68 -0
- package/nitrogen/generated/ios/swift/NitroInputLabelBehavior.swift +40 -0
- package/nitrogen/generated/ios/swift/NitroInputMode.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroInputNotation.swift +39 -0
- package/nitrogen/generated/ios/swift/NitroInputReturnKeyType.swift +56 -0
- package/nitrogen/generated/ios/swift/NitroInputSignPlacement.swift +40 -0
- package/nitrogen/generated/ios/swift/NitroInputSubmitBehavior.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroInputTextAlign.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputTextAlignVertical.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputVariant.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +213 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +300 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputWorkletsSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputWorkletsSpec.hpp +62 -0
- package/nitrogen/generated/shared/c++/NitroInputAffixAlign.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputAutoCapitalize.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputEasing.hpp +88 -0
- package/nitrogen/generated/shared/c++/NitroInputEffect.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroInputKeyboardAppearance.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroInputKeyboardType.hpp +104 -0
- package/nitrogen/generated/shared/c++/NitroInputLabelBehavior.hpp +76 -0
- package/nitrogen/generated/shared/c++/NitroInputMode.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroInputNotation.hpp +91 -0
- package/nitrogen/generated/shared/c++/NitroInputReturnKeyType.hpp +92 -0
- package/nitrogen/generated/shared/c++/NitroInputSignPlacement.hpp +76 -0
- package/nitrogen/generated/shared/c++/NitroInputSubmitBehavior.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroInputTextAlign.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputTextAlignVertical.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputVariant.hpp +80 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +216 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +374 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +101 -0
- package/package.json +141 -0
- package/react-native.config.js +16 -0
- package/src/MorphInput.tsx +30 -0
- package/src/NitroInput.tsx +1364 -0
- package/src/index.ts +55 -0
- package/src/specs/NitroInput.nitro.ts +436 -0
- package/src/specs/NitroInputWorklets.nitro.ts +13 -0
- package/src/useNitroInputState.ts +101 -0
- package/src/worklets.ts +300 -0
|
@@ -0,0 +1,1364 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
useEffect,
|
|
4
|
+
useImperativeHandle,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from 'react'
|
|
9
|
+
import {
|
|
10
|
+
I18nManager,
|
|
11
|
+
processColor,
|
|
12
|
+
StyleSheet,
|
|
13
|
+
type ColorValue,
|
|
14
|
+
type TextStyle,
|
|
15
|
+
type ViewProps,
|
|
16
|
+
} from 'react-native'
|
|
17
|
+
import {
|
|
18
|
+
callback,
|
|
19
|
+
getHostComponent,
|
|
20
|
+
type HybridRef,
|
|
21
|
+
} from 'react-native-nitro-modules'
|
|
22
|
+
import NitroInputViewConfig from '../nitrogen/generated/shared/json/NitroInputViewConfig.json'
|
|
23
|
+
import type {
|
|
24
|
+
NitroInputAffixAlign,
|
|
25
|
+
NitroInputAutoCapitalize,
|
|
26
|
+
NitroInputEasing,
|
|
27
|
+
NitroInputEffect,
|
|
28
|
+
NitroInputKeyboardAppearance,
|
|
29
|
+
NitroInputKeyboardType,
|
|
30
|
+
NitroInputLabelBehavior,
|
|
31
|
+
NitroInputMethods,
|
|
32
|
+
NitroInputMode,
|
|
33
|
+
NitroInputProps as NativeNitroInputProps,
|
|
34
|
+
NitroInputReturnKeyType,
|
|
35
|
+
NitroInputSubmitBehavior,
|
|
36
|
+
NitroInputTextAlign,
|
|
37
|
+
NitroInputTextAlignVertical,
|
|
38
|
+
NitroInputSignPlacement,
|
|
39
|
+
NitroInputVariant,
|
|
40
|
+
NitroInputNotation,
|
|
41
|
+
} from './specs/NitroInput.nitro'
|
|
42
|
+
import {
|
|
43
|
+
allocateWorkletId,
|
|
44
|
+
isWorklet,
|
|
45
|
+
registerFocusChange,
|
|
46
|
+
registerKeyPress,
|
|
47
|
+
registerSelectionChange,
|
|
48
|
+
registerTextEvent,
|
|
49
|
+
registerCallback,
|
|
50
|
+
registerTransform,
|
|
51
|
+
unregisterWorklet,
|
|
52
|
+
type NitroInputSelection,
|
|
53
|
+
type NitroInputTransform,
|
|
54
|
+
} from './worklets'
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* React Native's registry of mounted text inputs. `TextInput.State` only
|
|
58
|
+
* exposes the read side publicly, but `Keyboard.dismiss()`,
|
|
59
|
+
* `keyboardShouldPersistTaps` and a ScrollView's auto-blur all go through
|
|
60
|
+
* `currentlyFocusedInput()`, so a field that never registers is invisible to
|
|
61
|
+
* them. The internal module is the only way in; it is imported behind a guard
|
|
62
|
+
* so a React Native version that moves it degrades instead of throwing.
|
|
63
|
+
*/
|
|
64
|
+
interface TextInputRegistry {
|
|
65
|
+
currentlyFocusedInput(): unknown
|
|
66
|
+
focusInput(input: unknown): void
|
|
67
|
+
blurInput(input: unknown): void
|
|
68
|
+
registerInput(input: unknown): void
|
|
69
|
+
unregisterInput(input: unknown): void
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface TextInputCommands {
|
|
73
|
+
focusTextInput(input: unknown): void
|
|
74
|
+
blurTextInput(input: unknown): void
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const textInputRegistry: (TextInputRegistry & Partial<TextInputCommands>) | null = (() => {
|
|
78
|
+
try {
|
|
79
|
+
// The registry has no public surface: `TextInput.State` exposes the focus
|
|
80
|
+
// commands but not `registerInput`, and registering is the whole point -
|
|
81
|
+
// it is how `ref.focus()`, `Keyboard.dismiss()` and a ScrollView's
|
|
82
|
+
// `keyboardShouldPersistTaps` recognise a text input at all. React Native's
|
|
83
|
+
// Babel preset logs a deprecation for every *literal* `react-native/...`
|
|
84
|
+
// import, in every app that bundles this package, and the guard around
|
|
85
|
+
// this call already is the deprecation policy: a version that moves the
|
|
86
|
+
// module degrades to plain focus rather than throwing. A template literal
|
|
87
|
+
// says the same path; Metro evaluates it, the warning plugin only matches
|
|
88
|
+
// string literals.
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
90
|
+
const mod = require(`react-native/Libraries/Components/TextInput/TextInputState`)
|
|
91
|
+
const state = (mod?.default ?? mod) as Partial<TextInputRegistry> | undefined
|
|
92
|
+
return typeof state?.focusInput === 'function' && typeof state?.blurInput === 'function'
|
|
93
|
+
? (state as TextInputRegistry & Partial<TextInputCommands>)
|
|
94
|
+
: null
|
|
95
|
+
} catch {
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
})()
|
|
99
|
+
|
|
100
|
+
/** React Native's `inputMode`, the HTML-aligned alias for `keyboardType`. */
|
|
101
|
+
export type InputMode =
|
|
102
|
+
| 'none'
|
|
103
|
+
| 'text'
|
|
104
|
+
| 'decimal'
|
|
105
|
+
| 'numeric'
|
|
106
|
+
| 'tel'
|
|
107
|
+
| 'search'
|
|
108
|
+
| 'email'
|
|
109
|
+
| 'url'
|
|
110
|
+
|
|
111
|
+
/** React Native's `enterKeyHint`, the HTML-aligned alias for `returnKeyType`. */
|
|
112
|
+
export type EnterKeyHint = 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send'
|
|
113
|
+
|
|
114
|
+
// Both tables are React Native's own (`TextInput.js`), so an app that swaps a
|
|
115
|
+
// `TextInput` for this gets the same keyboard from the same prop.
|
|
116
|
+
const INPUT_MODE_TO_KEYBOARD: Record<InputMode, NitroInputKeyboardType> = {
|
|
117
|
+
none: 'default',
|
|
118
|
+
text: 'default',
|
|
119
|
+
decimal: 'decimal-pad',
|
|
120
|
+
numeric: 'number-pad',
|
|
121
|
+
tel: 'phone-pad',
|
|
122
|
+
// RN picks iOS `web-search` here; this component has no such keyboard type,
|
|
123
|
+
// and Android already falls back to `default`.
|
|
124
|
+
search: 'default',
|
|
125
|
+
email: 'email-address',
|
|
126
|
+
url: 'url',
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const ENTER_KEY_HINT_TO_RETURN_KEY: Record<EnterKeyHint, NitroInputReturnKeyType> = {
|
|
130
|
+
enter: 'default',
|
|
131
|
+
done: 'done',
|
|
132
|
+
go: 'go',
|
|
133
|
+
next: 'next',
|
|
134
|
+
// RN maps `previous` to an Android-only return key this component does not
|
|
135
|
+
// expose, so it falls back to the default on both.
|
|
136
|
+
previous: 'default',
|
|
137
|
+
search: 'search',
|
|
138
|
+
send: 'send',
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The react tag of a host instance, or 0 when there is none. Fabric's
|
|
143
|
+
* `ReactNativeElement` keeps it on `__nativeTag`; the pre-Fabric name was
|
|
144
|
+
* `_nativeTag`. Android's Nitro view hands out no instance at all, so 0.
|
|
145
|
+
*/
|
|
146
|
+
function reactTagOf(host: unknown): number {
|
|
147
|
+
const instance = host as { _nativeTag?: number; __nativeTag?: number } | null
|
|
148
|
+
const tag = instance?.__nativeTag ?? instance?._nativeTag
|
|
149
|
+
return typeof tag === 'number' ? tag : 0
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const EMPTY_NOTATIONS: NitroInputNotation[] = []
|
|
153
|
+
|
|
154
|
+
/** Mounted fields, by the host instance React Native's registry stores. */
|
|
155
|
+
const mountedFields = new WeakMap<object, { focus(): void; blur(): void }>()
|
|
156
|
+
let patchedRegistry = false
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* `TextInputState.focusTextInput` / `blurTextInput` — which is how
|
|
160
|
+
* `Keyboard.dismiss()`, `keyboardShouldPersistTaps` and a ScrollView's
|
|
161
|
+
* auto-blur reach the focused input — dispatch a codegen `focus` / `blur`
|
|
162
|
+
* *view command*. A Nitro view has no such command on Android, so the call
|
|
163
|
+
* would be dropped and the keyboard would stay up. The two functions are
|
|
164
|
+
* wrapped once, on the registry module, to route a NitroInput to its own
|
|
165
|
+
* native focus/blur; every other input is passed straight through untouched.
|
|
166
|
+
*
|
|
167
|
+
* The wrap reaches every caller that reads the two off the module at call
|
|
168
|
+
* time, which `Keyboard.dismiss()` and `ScrollView` do. It does not reach
|
|
169
|
+
* `TextInput.State.focusTextInput` / `blurTextInput`: `TextInput.js` copies
|
|
170
|
+
* those references when it is evaluated, before this runs, so a call through
|
|
171
|
+
* them still dispatches the view command. On iOS `ios/NitroInputCommands.mm`
|
|
172
|
+
* answers that command; on Android it is dropped.
|
|
173
|
+
*/
|
|
174
|
+
function patchRegistryOnce() {
|
|
175
|
+
if (patchedRegistry || textInputRegistry == null) return
|
|
176
|
+
patchedRegistry = true
|
|
177
|
+
const registry = textInputRegistry
|
|
178
|
+
const originalFocus = registry.focusTextInput
|
|
179
|
+
const originalBlur = registry.blurTextInput
|
|
180
|
+
if (typeof originalFocus === 'function') {
|
|
181
|
+
registry.focusTextInput = (input: unknown) => {
|
|
182
|
+
const field = input != null ? mountedFields.get(input as object) : undefined
|
|
183
|
+
if (field) return field.focus()
|
|
184
|
+
return originalFocus.call(registry, input)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (typeof originalBlur === 'function') {
|
|
188
|
+
registry.blurTextInput = (input: unknown) => {
|
|
189
|
+
const field = input != null ? mountedFields.get(input as object) : undefined
|
|
190
|
+
if (field) return field.blur()
|
|
191
|
+
return originalBlur.call(registry, input)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The raw Nitro host component. Prefer {@link NitroInput}, which adds the
|
|
198
|
+
* controlled-value handshake, auto-sizing, color/weight conversion and a
|
|
199
|
+
* convenient handle.
|
|
200
|
+
*/
|
|
201
|
+
export const NativeNitroInputView = getHostComponent<
|
|
202
|
+
NativeNitroInputProps,
|
|
203
|
+
NitroInputMethods
|
|
204
|
+
>('NitroInputView', () => NitroInputViewConfig)
|
|
205
|
+
|
|
206
|
+
/** The native Nitro `HybridObject` behind a mounted {@link NitroInput}. */
|
|
207
|
+
export type NitroInputRef = HybridRef<NativeNitroInputProps, NitroInputMethods>
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The events this component hands to its callbacks.
|
|
211
|
+
*
|
|
212
|
+
* Each one is a superset of what `TextInput` passes: `nativeEvent` is there
|
|
213
|
+
* with the same fields under the same names, so a handler written against a
|
|
214
|
+
* `TextInput` keeps working when the component is swapped, and the fields are
|
|
215
|
+
* repeated at the top level so new code can destructure them instead of
|
|
216
|
+
* reaching through `nativeEvent`.
|
|
217
|
+
*/
|
|
218
|
+
export interface NitroInputTextEvent {
|
|
219
|
+
text: string
|
|
220
|
+
/** The host view's react tag, as `TextInput` reports for itself. */
|
|
221
|
+
target: number
|
|
222
|
+
nativeEvent: { text: string; target: number }
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface NitroInputFocusEvent extends NitroInputTextEvent {
|
|
226
|
+
/** The native edit counter, as in `TextInput`. */
|
|
227
|
+
eventCount: number
|
|
228
|
+
nativeEvent: { text: string; target: number; eventCount: number }
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export interface NitroInputSelectionEvent {
|
|
232
|
+
/** The selection, in code points. */
|
|
233
|
+
selection: NitroInputSelection
|
|
234
|
+
/** The selection's bounds, repeated so `({ start, end }) => …` works. */
|
|
235
|
+
start: number
|
|
236
|
+
end: number
|
|
237
|
+
target: number
|
|
238
|
+
nativeEvent: { selection: NitroInputSelection; target: number }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface NitroInputKeyPressEvent {
|
|
242
|
+
/** The character, `'Backspace'` or `'Enter'`. */
|
|
243
|
+
key: string
|
|
244
|
+
eventCount: number
|
|
245
|
+
target: number
|
|
246
|
+
nativeEvent: { key: string; eventCount: number; target: number }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus' | 'onBlur'> {
|
|
250
|
+
/**
|
|
251
|
+
* The text to show (controlled). The native side formats and shows what the
|
|
252
|
+
* user types on its own; this prop is applied when it changes to something
|
|
253
|
+
* other than what the field already shows, so echoing `onChangeText` back
|
|
254
|
+
* into state never fights the user or shows an unformatted frame.
|
|
255
|
+
*/
|
|
256
|
+
value?: string
|
|
257
|
+
/** The initial text (uncontrolled). */
|
|
258
|
+
defaultValue?: string
|
|
259
|
+
/**
|
|
260
|
+
* The field's frame. `'outlined'` strokes a rounded rectangle notched around
|
|
261
|
+
* the floating label - the notch is a real hole in the path, so whatever is
|
|
262
|
+
* behind the field shows through. `'filled'` tints the box instead.
|
|
263
|
+
* Default: `'none'`, leaving the border to `style` as before.
|
|
264
|
+
*/
|
|
265
|
+
variant?: NitroInputVariant
|
|
266
|
+
/** Floating label text. Requires a `variant` other than `'none'`. */
|
|
267
|
+
label?: string
|
|
268
|
+
/** Whether the label floats on focus/content or stays floated. Default: `'float'`. */
|
|
269
|
+
labelBehavior?: NitroInputLabelBehavior
|
|
270
|
+
/** Label colour at rest; defaults to `placeholderTextColor`. */
|
|
271
|
+
labelColor?: ColorValue
|
|
272
|
+
/** Label colour while focused; defaults to `focusedStrokeColor`. */
|
|
273
|
+
labelFocusedColor?: ColorValue
|
|
274
|
+
/** Label size when floated, in points; omit to derive it from `fontSize`. */
|
|
275
|
+
labelFontSize?: number
|
|
276
|
+
/** Outline colour; defaults to a platform hairline grey. */
|
|
277
|
+
strokeColor?: ColorValue
|
|
278
|
+
/** Outline colour while focused; defaults to `strokeColor`. */
|
|
279
|
+
focusedStrokeColor?: ColorValue
|
|
280
|
+
/** Outline stroke width in points, doubled while focused. Default: `1`. */
|
|
281
|
+
strokeWidth?: number
|
|
282
|
+
/** Corner radius of the frame. Default: `8`. */
|
|
283
|
+
cornerRadius?: number
|
|
284
|
+
/** `'filled'`: the box tint. */
|
|
285
|
+
fillColor?: ColorValue
|
|
286
|
+
/**
|
|
287
|
+
* What the field holds. `'number'` formats natively as you type: grouping
|
|
288
|
+
* separators, one decimal separator, up to `fractionDigits` decimals, a
|
|
289
|
+
* currency `prefix` / `suffix`. `'mask'` applies a fixed pattern (`mask`).
|
|
290
|
+
* `'text'` is a plain field. Default: `'text'`.
|
|
291
|
+
*/
|
|
292
|
+
mode?: NitroInputMode
|
|
293
|
+
/**
|
|
294
|
+
* React Native's alias for `keyboardType`, following the HTML attribute.
|
|
295
|
+
* Ignored when `keyboardType` is given. `'none'` also suppresses the soft
|
|
296
|
+
* keyboard, as it does in `TextInput`.
|
|
297
|
+
*/
|
|
298
|
+
inputMode?: InputMode
|
|
299
|
+
/** React Native's alias for `returnKeyType`, following the HTML attribute. Ignored when `returnKeyType` is given. */
|
|
300
|
+
enterKeyHint?: EnterKeyHint
|
|
301
|
+
/**
|
|
302
|
+
* `'mask'`: the pattern, e.g. `'+1 ([000]) [000]-[0000]'`.
|
|
303
|
+
*
|
|
304
|
+
* `[0]` mandatory digit, `[9]` optional digit, `[A]` mandatory letter,
|
|
305
|
+
* `[a]` optional letter, `[_]` mandatory alphanumeric, `[-]` optional
|
|
306
|
+
* alphanumeric, `[…]` an unbounded run of the preceding type. `{…}` is a
|
|
307
|
+
* fixed block whose characters count towards the extracted value; a literal
|
|
308
|
+
* outside brackets is shown but not extracted. `\\` escapes.
|
|
309
|
+
*
|
|
310
|
+
* An invalid pattern leaves the field unmasked rather than breaking it.
|
|
311
|
+
*/
|
|
312
|
+
mask?: string
|
|
313
|
+
/** `'mask'`: extra slot characters beyond the built-in ones. */
|
|
314
|
+
maskNotations?: NitroInputNotation[]
|
|
315
|
+
/** `'mask'`: fill in constants as the caret reaches them. Default: `true`. */
|
|
316
|
+
maskAutocomplete?: boolean
|
|
317
|
+
/** `'mask'`: backspace walks back over autocompleted constants. Default: `false`. */
|
|
318
|
+
maskAutoSkip?: boolean
|
|
319
|
+
/**
|
|
320
|
+
* `'mask'` mode: called whenever the text changes, from a keystroke,
|
|
321
|
+
* `setText` / `clear` or the `value` prop alike, with the masked text, the
|
|
322
|
+
* characters the user contributed, what is still missing, and whether every
|
|
323
|
+
* mandatory slot is filled. A mount with an unchanged text does not report.
|
|
324
|
+
*/
|
|
325
|
+
onChangeMask?: (
|
|
326
|
+
formatted: string,
|
|
327
|
+
extracted: string,
|
|
328
|
+
tailPlaceholder: string,
|
|
329
|
+
complete: boolean
|
|
330
|
+
) => void
|
|
331
|
+
/** `'number'`: most digits allowed after the decimal separator; `0` disables the decimal. Default: `2`. */
|
|
332
|
+
fractionDigits?: number
|
|
333
|
+
/** `'number'`: most integer digits accepted. Default: `15`. */
|
|
334
|
+
maxIntegerDigits?: number
|
|
335
|
+
/** `'number'`: inserted between every three integer digits; `''` disables grouping. Default: `','`. */
|
|
336
|
+
groupingSeparator?: string
|
|
337
|
+
/** `'number'`: placed between integer and fraction digits. Default: `'.'`. */
|
|
338
|
+
decimalSeparator?: string
|
|
339
|
+
/** Static text drawn before the field's text, e.g. `'$'`. */
|
|
340
|
+
prefix?: string
|
|
341
|
+
/** Static text drawn after the field's text, e.g. `' USD'`. */
|
|
342
|
+
suffix?: string
|
|
343
|
+
/** Font size of `prefix`, e.g. a smaller currency symbol. Defaults to `fontSize`. */
|
|
344
|
+
prefixFontSize?: number
|
|
345
|
+
/** Font size of `suffix`. Defaults to `fontSize`. */
|
|
346
|
+
suffixFontSize?: number
|
|
347
|
+
/**
|
|
348
|
+
* How prefix and suffix line up with the text: `'top'` pins glyph tops,
|
|
349
|
+
* `'bottom'` the bottom of the ink, `'baseline'` shares the baseline,
|
|
350
|
+
* `'center'` centres. Default: `'baseline'`.
|
|
351
|
+
*/
|
|
352
|
+
affixAlign?: NitroInputAffixAlign
|
|
353
|
+
/**
|
|
354
|
+
* Where a negative amount's sign sits relative to `prefix`. `'beforeAffix'`
|
|
355
|
+
* (the default) reads `-$1,234.56`; `'afterAffix'` reads `$-1,234.56`, which
|
|
356
|
+
* suits a symbol styled as an ornament rather than read as part of the
|
|
357
|
+
* number. Only the morph honours it - a plain field's affixes are accessory
|
|
358
|
+
* views outside the text, so it is always `'afterAffix'`.
|
|
359
|
+
*/
|
|
360
|
+
signPlacement?: NitroInputSignPlacement
|
|
361
|
+
/** Alignment of `prefix` only. Defaults to `affixAlign`. */
|
|
362
|
+
prefixAlign?: NitroInputAffixAlign
|
|
363
|
+
/** Alignment of `suffix` only. Defaults to `affixAlign`. */
|
|
364
|
+
suffixAlign?: NitroInputAffixAlign
|
|
365
|
+
/** Shown while the field is empty and morphed away by the first character. In `'number'` mode `'0'` reads well. */
|
|
366
|
+
placeholder?: string
|
|
367
|
+
/** Color of the placeholder. Defaults to the platform placeholder color. */
|
|
368
|
+
placeholderTextColor?: ColorValue
|
|
369
|
+
/** Duration in ms of the morph played on every change. `0` snaps. Default: `400`. */
|
|
370
|
+
duration?: number
|
|
371
|
+
/** Timing curve of the morph. Default: `'expo'` (Torph's `cubic-bezier(0.19, 1, 0.22, 1)`). */
|
|
372
|
+
easing?: NitroInputEasing
|
|
373
|
+
/** Overshoot of the `'spring'` easing, `0`–`1`. Default: `0.15`. */
|
|
374
|
+
bounce?: number
|
|
375
|
+
/**
|
|
376
|
+
* How characters enter and leave. `'auto'`: digits and separators slide
|
|
377
|
+
* through the line box (digits from above, separators from below), other
|
|
378
|
+
* characters fade and scale. Default: `'auto'`.
|
|
379
|
+
*/
|
|
380
|
+
effect?: NitroInputEffect
|
|
381
|
+
/** Font size in points. Default: `32`. */
|
|
382
|
+
fontSize?: number
|
|
383
|
+
/**
|
|
384
|
+
* Height of the line box in points — the CSS meaning, the total height a
|
|
385
|
+
* line occupies rather than extra leading. Omit to use the font's own.
|
|
386
|
+
*
|
|
387
|
+
* Unlike `TextInput`, a line height *tighter* than the font is centred
|
|
388
|
+
* correctly too, and it applies to single-line and morphing fields, not only
|
|
389
|
+
* wrapped ones.
|
|
390
|
+
*/
|
|
391
|
+
lineHeight?: number
|
|
392
|
+
/** Font weight, like `Text`'s `fontWeight`. Default: `'normal'`. */
|
|
393
|
+
fontWeight?: TextStyle['fontWeight']
|
|
394
|
+
/** Font family name, like `Text`'s `fontFamily`. Defaults to the system font. */
|
|
395
|
+
fontFamily?: string
|
|
396
|
+
/** Text color. Defaults to the platform's primary label color. */
|
|
397
|
+
color?: ColorValue
|
|
398
|
+
/**
|
|
399
|
+
* Where the text sits when the view is wider than it. Default: `'auto'`: the
|
|
400
|
+
* start edge of the layout direction, as `TextInput`. `'left'` and `'right'`
|
|
401
|
+
* are absolute.
|
|
402
|
+
*/
|
|
403
|
+
textAlign?: NitroInputTextAlign
|
|
404
|
+
/** Caret color. Defaults to the platform tint. */
|
|
405
|
+
cursorColor?: ColorValue
|
|
406
|
+
/** Selection highlight color. Defaults to the platform tint. */
|
|
407
|
+
selectionColor?: ColorValue
|
|
408
|
+
/** Hides the caret. Default: `false`. */
|
|
409
|
+
caretHidden?: boolean
|
|
410
|
+
/**
|
|
411
|
+
* Scale the text down when it is wider than the view (give the view a fixed
|
|
412
|
+
* `width` in `style`), and back up when it fits again. Default: `false`.
|
|
413
|
+
*/
|
|
414
|
+
adjustsFontSizeToFit?: boolean
|
|
415
|
+
/** Smallest scale `adjustsFontSizeToFit` may apply, `0`–`1`. Default: `0.5`. */
|
|
416
|
+
minimumFontScale?: number
|
|
417
|
+
/** Scale the fonts with the system text size, like `Text`. Default: `false`. */
|
|
418
|
+
allowFontScaling?: boolean
|
|
419
|
+
/** Upper bound for `allowFontScaling`, e.g. `1.3`. `0` = no cap. Default: `0`. */
|
|
420
|
+
maxFontSizeMultiplier?: number
|
|
421
|
+
/** Keyboard to show. Defaults to `'decimal-pad'` / `'number-pad'` in `'number'` mode, `'default'` otherwise. */
|
|
422
|
+
keyboardType?: NitroInputKeyboardType
|
|
423
|
+
/** Label of the return key. Default: `'default'`. */
|
|
424
|
+
returnKeyType?: NitroInputReturnKeyType
|
|
425
|
+
/** Auto-capitalisation in `'text'` mode. Default: `'sentences'`. */
|
|
426
|
+
autoCapitalize?: NitroInputAutoCapitalize
|
|
427
|
+
/** Auto-correction in `'text'` mode. Default: `true`. */
|
|
428
|
+
autoCorrect?: boolean
|
|
429
|
+
/** Whether the user can edit the field. Default: `true`. */
|
|
430
|
+
editable?: boolean
|
|
431
|
+
/**
|
|
432
|
+
* Let the text wrap onto more than one line.
|
|
433
|
+
*
|
|
434
|
+
* A multiline field is always drawn by the system view and is always
|
|
435
|
+
* `'text'` mode: the glyph engine lays one run out on one baseline, so it
|
|
436
|
+
* cannot morph wrapped text, and an amount or a mask is a single-line idea.
|
|
437
|
+
* `morph`, `mode="number"`, `mode="mask"` and the `prefix` / `suffix`
|
|
438
|
+
* affixes are ignored alongside it, with one warning each in development.
|
|
439
|
+
* The return key inserts a line break unless `submitBehavior` says otherwise.
|
|
440
|
+
*/
|
|
441
|
+
multiline?: boolean
|
|
442
|
+
/** `multiline`: lines tall before it scrolls. Omit to grow with the content. */
|
|
443
|
+
numberOfLines?: number
|
|
444
|
+
/** Alias for {@link numberOfLines}, matching `TextInput`. */
|
|
445
|
+
rows?: number
|
|
446
|
+
/** `multiline`: where the text sits in the box. Default: `'auto'` (top). */
|
|
447
|
+
textAlignVertical?: NitroInputTextAlignVertical
|
|
448
|
+
/** `multiline`: whether it scrolls once the text outgrows it. Default: `true`. */
|
|
449
|
+
scrollEnabled?: boolean
|
|
450
|
+
/** Focus the field when it mounts. Default: `false`. */
|
|
451
|
+
autoFocus?: boolean
|
|
452
|
+
/**
|
|
453
|
+
* What the return key does. `'blurAndSubmit'` fires `onSubmitEditing` and
|
|
454
|
+
* dismisses the keyboard; `'submit'` fires it and keeps focus, so a form can
|
|
455
|
+
* move to the next field itself; `'newline'` inserts a line break instead
|
|
456
|
+
* (`multiline` only). Defaults as `TextInput`'s: `'blurAndSubmit'` for a
|
|
457
|
+
* single-line field, `'newline'` for a multiline one.
|
|
458
|
+
*/
|
|
459
|
+
submitBehavior?: NitroInputSubmitBehavior
|
|
460
|
+
/**
|
|
461
|
+
* Deprecated alias of `submitBehavior`, resolved the way `TextInput` resolves
|
|
462
|
+
* it: `false` means `'submit'` on a single-line field, and `true` means
|
|
463
|
+
* `'blurAndSubmit'` on a multiline one (whose return key otherwise inserts a
|
|
464
|
+
* line break). Ignored when `submitBehavior` is given.
|
|
465
|
+
*/
|
|
466
|
+
blurOnSubmit?: boolean
|
|
467
|
+
/** Masks the drawn glyphs with bullets and turns off autocorrect. Default: `false`. */
|
|
468
|
+
secureTextEntry?: boolean
|
|
469
|
+
/** Light or dark keyboard (iOS). Default: `'default'`. */
|
|
470
|
+
keyboardAppearance?: NitroInputKeyboardAppearance
|
|
471
|
+
/** iOS `textContentType` — the autofill kind (`'username'`, `'password'`, `'oneTimeCode'`, …). */
|
|
472
|
+
textContentType?: string
|
|
473
|
+
/** Cross-platform autofill hint; used as `textContentType` when that is not set. */
|
|
474
|
+
autoComplete?: string
|
|
475
|
+
/** Disables the return key until the field has text. Default: `false`. */
|
|
476
|
+
enablesReturnKeyAutomatically?: boolean
|
|
477
|
+
/** Show the soft keyboard on focus. `false` keeps focus and caret without it. Default: `true`. */
|
|
478
|
+
showSoftInputOnFocus?: boolean
|
|
479
|
+
/** Select all the text when the field gains focus. Default: `false`. */
|
|
480
|
+
selectTextOnFocus?: boolean
|
|
481
|
+
/** Empty the field when it gains focus. Default: `false`. */
|
|
482
|
+
clearTextOnFocus?: boolean
|
|
483
|
+
/** Hides the Cut/Copy/Paste menu. Default: `false`. */
|
|
484
|
+
contextMenuHidden?: boolean
|
|
485
|
+
/** Spell checking in `'text'` mode. Defaults to `autoCorrect`. */
|
|
486
|
+
spellCheck?: boolean
|
|
487
|
+
/** Read-only alias of `editable={false}`, like React Native's. */
|
|
488
|
+
readOnly?: boolean
|
|
489
|
+
/** The caret/selection to apply, in code points. */
|
|
490
|
+
selection?: { start: number; end?: number }
|
|
491
|
+
/**
|
|
492
|
+
* Run the glyph engine, so text morphs as it changes rather than simply
|
|
493
|
+
* appearing. Off by default: this is a text field first, and a `TextInput`
|
|
494
|
+
* does not animate its characters. {@link MorphInput} is this component with
|
|
495
|
+
* it on, which is what an amount field wants.
|
|
496
|
+
*/
|
|
497
|
+
morph?: boolean
|
|
498
|
+
/**
|
|
499
|
+
* Whether the field sizes itself to its content.
|
|
500
|
+
*
|
|
501
|
+
* `false` (the default) takes no width of its own, so flexbox stretches it to
|
|
502
|
+
* its parent the way a `TextInput` is stretched — that is what makes this a
|
|
503
|
+
* drop-in. `true` always sizes to content. `'auto'` infers it: on unless
|
|
504
|
+
* `style` gives a `width` or `flex`, which is what lets an amount grow as
|
|
505
|
+
* digits arrive, and is what {@link MorphInput} uses.
|
|
506
|
+
*/
|
|
507
|
+
autoWidth?: boolean | 'auto'
|
|
508
|
+
/** `'text'` mode: most characters accepted. Default: unlimited. */
|
|
509
|
+
maxLength?: number
|
|
510
|
+
/**
|
|
511
|
+
* A worklet that rewrites the text after every edit, synchronously on the UI
|
|
512
|
+
* thread before a frame is drawn (needs `react-native-worklets`): masks,
|
|
513
|
+
* custom formats, anything JS can express. It receives the edited text, the
|
|
514
|
+
* previous text and both selections (code point offsets) and returns the
|
|
515
|
+
* new text and optionally where the caret should go; `null` keeps the edit.
|
|
516
|
+
* In `'number'` mode it runs after the native formatter. Create it once
|
|
517
|
+
* (module scope or `useCallback`): a new function re-registers the worklet.
|
|
518
|
+
*/
|
|
519
|
+
transform?: NitroInputTransform
|
|
520
|
+
/**
|
|
521
|
+
* Called after every edit with the field's (formatted) text. Mark it with
|
|
522
|
+
* `'worklet'` (with `react-native-worklets` installed) and it runs
|
|
523
|
+
* synchronously on the UI thread instead, where it can write shared values
|
|
524
|
+
* before the next frame.
|
|
525
|
+
*/
|
|
526
|
+
onChangeText?: (text: string) => void
|
|
527
|
+
/**
|
|
528
|
+
* React Native's other change callback, fired alongside `onChangeText` with
|
|
529
|
+
* the same text. `nativeEvent.eventCount` is the native edit counter, as in
|
|
530
|
+
* `TextInput`.
|
|
531
|
+
*/
|
|
532
|
+
onChange?: (event: {
|
|
533
|
+
nativeEvent: { text: string; eventCount: number; target: number }
|
|
534
|
+
}) => void
|
|
535
|
+
/** `'number'` mode: called after every edit with the numeric value, `NaN` when empty. A `'worklet'` runs on the UI thread. */
|
|
536
|
+
onChangeValue?: (value: number) => void
|
|
537
|
+
/**
|
|
538
|
+
* Focused. The event is `TextInput`'s, so a handler written for one works
|
|
539
|
+
* here unchanged - and the same fields are repeated at the top level, so
|
|
540
|
+
* `({ text }) => …` reads better than `(e) => e.nativeEvent.text`.
|
|
541
|
+
*/
|
|
542
|
+
onFocus?: (event: NitroInputFocusEvent) => void
|
|
543
|
+
/** Blurred. Same event as {@link NitroInputProps.onFocus}. */
|
|
544
|
+
onBlur?: (event: NitroInputFocusEvent) => void
|
|
545
|
+
/** The return key was pressed. */
|
|
546
|
+
onSubmitEditing?: (event: NitroInputTextEvent) => void
|
|
547
|
+
/** Editing finished (focus lost or keyboard dismissed), like `TextInput`'s `onEndEditing`. */
|
|
548
|
+
onEndEditing?: (event: NitroInputTextEvent) => void
|
|
549
|
+
/** The caret or selection moved, in code points. */
|
|
550
|
+
onSelectionChange?: (event: NitroInputSelectionEvent) => void
|
|
551
|
+
/** A key was pressed, before the text changes: the character, `'Backspace'` or `'Enter'`. */
|
|
552
|
+
onKeyPress?: (event: NitroInputKeyPressEvent) => void
|
|
553
|
+
/** Receives the native Nitro object once the view is mounted. */
|
|
554
|
+
onNativeRef?: (ref: NitroInputRef) => void
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** Imperative handle exposed through `ref`. Methods are no-ops before mount. */
|
|
558
|
+
export interface NitroInputHandle {
|
|
559
|
+
focus(): void
|
|
560
|
+
blur(): void
|
|
561
|
+
/** Empties the field, morphing the characters away. */
|
|
562
|
+
clear(): void
|
|
563
|
+
/** Replaces the text (formatted in `'number'` mode), caret at the end. */
|
|
564
|
+
setText(text: string): void
|
|
565
|
+
/** `'number'` mode: shows `value` formatted; `NaN` empties the field. */
|
|
566
|
+
setValue(value: number): void
|
|
567
|
+
/** The field's current (formatted) text. */
|
|
568
|
+
getText(): string
|
|
569
|
+
/** `'number'` mode: the numeric value, `NaN` when empty. */
|
|
570
|
+
getValue(): number
|
|
571
|
+
isFocused(): boolean
|
|
572
|
+
/** Moves the caret / selection; code points into the (formatted) text. */
|
|
573
|
+
setSelection(start: number, end?: number): void
|
|
574
|
+
/** The native Nitro object, or `null` before mount. */
|
|
575
|
+
readonly native: NitroInputRef | null
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// shared-helpers:start
|
|
579
|
+
// Kept byte-for-byte identical with packages/react-native-nitro-rolling-number/src/RollingNumber.tsx
|
|
580
|
+
// (a test compares the two blocks); change both together.
|
|
581
|
+
const FONT_WEIGHTS: Record<string, number> = {
|
|
582
|
+
normal: 400,
|
|
583
|
+
regular: 400,
|
|
584
|
+
bold: 700,
|
|
585
|
+
ultralight: 100,
|
|
586
|
+
thin: 200,
|
|
587
|
+
light: 300,
|
|
588
|
+
medium: 500,
|
|
589
|
+
semibold: 600,
|
|
590
|
+
condensedBold: 700,
|
|
591
|
+
condensed: 400,
|
|
592
|
+
heavy: 800,
|
|
593
|
+
black: 900,
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function toNumericWeight(weight: TextStyle['fontWeight']): number | undefined {
|
|
597
|
+
if (weight == null) return undefined
|
|
598
|
+
if (typeof weight === 'number') return weight
|
|
599
|
+
const parsed = Number(weight)
|
|
600
|
+
if (!Number.isNaN(parsed)) return parsed
|
|
601
|
+
return FONT_WEIGHTS[weight]
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function toProcessedColor(color: ColorValue | undefined): number | undefined {
|
|
605
|
+
if (color == null) return undefined
|
|
606
|
+
const processed = processColor(color)
|
|
607
|
+
return typeof processed === 'number' ? processed : undefined
|
|
608
|
+
}
|
|
609
|
+
// shared-helpers:end
|
|
610
|
+
|
|
611
|
+
interface Size {
|
|
612
|
+
width: number
|
|
613
|
+
height: number
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
const warnedIncompatible = new Set<string>()
|
|
617
|
+
/** Once per offending prop, not once per render. */
|
|
618
|
+
function warnIncompatible(what: string): void {
|
|
619
|
+
if (warnedIncompatible.has(what)) return
|
|
620
|
+
warnedIncompatible.add(what)
|
|
621
|
+
console.warn(
|
|
622
|
+
`[NitroInput] \`${what}\` is ignored on a multiline field: a wrapping ` +
|
|
623
|
+
'field is drawn by the system view, and an amount, a mask and their ' +
|
|
624
|
+
'affixes are single-line. Drop one of the two.'
|
|
625
|
+
)
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* A native text input: a system field (`UITextField` / `EditText`) that owns
|
|
630
|
+
* the keyboard, editing, selection, paste and accessibility, with native
|
|
631
|
+
* formatting (`mode="number"`), native masking (`mode="mask"`), an outlined
|
|
632
|
+
* or filled frame with a floating label, and optionally the glyph engine
|
|
633
|
+
* (`morph`), under which characters that stay glide to their new place, new
|
|
634
|
+
* ones slide or fade in and removed ones leave alongside their neighbours.
|
|
635
|
+
* In `'number'` mode every edit is formatted on the native side before a
|
|
636
|
+
* frame is drawn, with the caret kept in place, so there is never an
|
|
637
|
+
* unformatted frame and never a round trip through JS.
|
|
638
|
+
*
|
|
639
|
+
* Like a `TextInput`, it takes its width from its parent; `autoWidth`
|
|
640
|
+
* (`'auto'` in {@link MorphInput}) sizes it to its content instead.
|
|
641
|
+
*/
|
|
642
|
+
export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
643
|
+
function NitroInput(
|
|
644
|
+
{
|
|
645
|
+
value,
|
|
646
|
+
defaultValue,
|
|
647
|
+
mode,
|
|
648
|
+
fractionDigits,
|
|
649
|
+
maxIntegerDigits,
|
|
650
|
+
groupingSeparator,
|
|
651
|
+
decimalSeparator,
|
|
652
|
+
prefix,
|
|
653
|
+
suffix,
|
|
654
|
+
prefixFontSize,
|
|
655
|
+
suffixFontSize,
|
|
656
|
+
affixAlign,
|
|
657
|
+
signPlacement,
|
|
658
|
+
prefixAlign,
|
|
659
|
+
suffixAlign,
|
|
660
|
+
placeholder,
|
|
661
|
+
placeholderTextColor,
|
|
662
|
+
duration,
|
|
663
|
+
easing,
|
|
664
|
+
bounce,
|
|
665
|
+
effect,
|
|
666
|
+
fontSize,
|
|
667
|
+
lineHeight,
|
|
668
|
+
fontWeight,
|
|
669
|
+
fontFamily,
|
|
670
|
+
color,
|
|
671
|
+
textAlign,
|
|
672
|
+
cursorColor,
|
|
673
|
+
selectionColor,
|
|
674
|
+
caretHidden,
|
|
675
|
+
adjustsFontSizeToFit,
|
|
676
|
+
minimumFontScale,
|
|
677
|
+
allowFontScaling,
|
|
678
|
+
maxFontSizeMultiplier,
|
|
679
|
+
variant,
|
|
680
|
+
label,
|
|
681
|
+
labelBehavior,
|
|
682
|
+
labelColor,
|
|
683
|
+
labelFocusedColor,
|
|
684
|
+
labelFontSize,
|
|
685
|
+
strokeColor,
|
|
686
|
+
focusedStrokeColor,
|
|
687
|
+
strokeWidth,
|
|
688
|
+
cornerRadius,
|
|
689
|
+
fillColor,
|
|
690
|
+
keyboardType,
|
|
691
|
+
inputMode,
|
|
692
|
+
enterKeyHint,
|
|
693
|
+
mask,
|
|
694
|
+
maskNotations,
|
|
695
|
+
maskAutocomplete,
|
|
696
|
+
maskAutoSkip,
|
|
697
|
+
returnKeyType,
|
|
698
|
+
autoCapitalize,
|
|
699
|
+
autoCorrect,
|
|
700
|
+
editable,
|
|
701
|
+
multiline,
|
|
702
|
+
numberOfLines,
|
|
703
|
+
rows,
|
|
704
|
+
textAlignVertical,
|
|
705
|
+
scrollEnabled,
|
|
706
|
+
autoFocus,
|
|
707
|
+
morph,
|
|
708
|
+
autoWidth: autoWidthProp,
|
|
709
|
+
submitBehavior,
|
|
710
|
+
blurOnSubmit,
|
|
711
|
+
secureTextEntry,
|
|
712
|
+
keyboardAppearance,
|
|
713
|
+
textContentType,
|
|
714
|
+
autoComplete,
|
|
715
|
+
enablesReturnKeyAutomatically,
|
|
716
|
+
showSoftInputOnFocus,
|
|
717
|
+
selectTextOnFocus,
|
|
718
|
+
clearTextOnFocus,
|
|
719
|
+
contextMenuHidden,
|
|
720
|
+
spellCheck,
|
|
721
|
+
readOnly,
|
|
722
|
+
selection,
|
|
723
|
+
onEndEditing,
|
|
724
|
+
onSelectionChange,
|
|
725
|
+
onKeyPress,
|
|
726
|
+
maxLength,
|
|
727
|
+
transform,
|
|
728
|
+
onChangeText,
|
|
729
|
+
onChange,
|
|
730
|
+
onChangeValue,
|
|
731
|
+
onChangeMask,
|
|
732
|
+
onFocus,
|
|
733
|
+
onBlur,
|
|
734
|
+
onSubmitEditing,
|
|
735
|
+
onNativeRef,
|
|
736
|
+
style,
|
|
737
|
+
...viewProps
|
|
738
|
+
},
|
|
739
|
+
ref
|
|
740
|
+
) {
|
|
741
|
+
const nativeRef = useRef<NitroInputRef | null>(null)
|
|
742
|
+
// What React Native's registry stores for this field. The React host
|
|
743
|
+
// instance when there is one (iOS), otherwise a stable per-instance token:
|
|
744
|
+
// on Android the Nitro host component hands out no instance, and the
|
|
745
|
+
// registry only ever compares identity or passes the value back to us.
|
|
746
|
+
const hostRef = useRef<unknown>(null)
|
|
747
|
+
const registryKeyRef = useRef<object | null>(null)
|
|
748
|
+
// The last text and edit counter the native side reported. `focus`, `blur`
|
|
749
|
+
// and `keyPress` carry both but are not themselves edits, so they have
|
|
750
|
+
// nothing of their own to read them from.
|
|
751
|
+
const textRef = useRef(defaultValue ?? '')
|
|
752
|
+
const eventCountRef = useRef(0)
|
|
753
|
+
const latest = useRef({
|
|
754
|
+
onNativeRef,
|
|
755
|
+
onChangeText,
|
|
756
|
+
onChange,
|
|
757
|
+
onChangeValue,
|
|
758
|
+
onChangeMask,
|
|
759
|
+
onFocus,
|
|
760
|
+
onBlur,
|
|
761
|
+
onSubmitEditing,
|
|
762
|
+
onEndEditing,
|
|
763
|
+
onSelectionChange,
|
|
764
|
+
onKeyPress,
|
|
765
|
+
})
|
|
766
|
+
latest.current = {
|
|
767
|
+
onNativeRef,
|
|
768
|
+
onChangeText,
|
|
769
|
+
onChange,
|
|
770
|
+
onChangeValue,
|
|
771
|
+
onChangeMask,
|
|
772
|
+
onFocus,
|
|
773
|
+
onBlur,
|
|
774
|
+
onSubmitEditing,
|
|
775
|
+
onEndEditing,
|
|
776
|
+
onSelectionChange,
|
|
777
|
+
onKeyPress,
|
|
778
|
+
}
|
|
779
|
+
// A controlled field's text is the prop, not the last edit this saw.
|
|
780
|
+
if (value != null) textRef.current = value
|
|
781
|
+
|
|
782
|
+
const [size, setSize] = useState<Size | null>(null)
|
|
783
|
+
// Worklets are registered on the UI runtime and referenced by id; a
|
|
784
|
+
// handler marked 'worklet' runs there instead of on the JS thread.
|
|
785
|
+
const transformId = useWorkletId(transform, registerTransform)
|
|
786
|
+
const onChangeTextWorklet = isWorklet(onChangeText) ? onChangeText : undefined
|
|
787
|
+
const onChangeValueWorklet = isWorklet(onChangeValue) ? onChangeValue : undefined
|
|
788
|
+
const onChangeTextId = useWorkletId(onChangeTextWorklet, registerCallback)
|
|
789
|
+
const onChangeValueId = useWorkletId(onChangeValueWorklet, registerCallback)
|
|
790
|
+
// Marking any of these `'worklet'` moves it to the UI thread; the JS
|
|
791
|
+
// handler is then skipped, exactly as `onChangeText` already worked.
|
|
792
|
+
const onFocusId = useWorkletPairId(
|
|
793
|
+
isWorklet(onFocus) ? (onFocus as never) : undefined,
|
|
794
|
+
isWorklet(onBlur) ? (onBlur as never) : undefined,
|
|
795
|
+
registerFocusChange
|
|
796
|
+
)
|
|
797
|
+
const onSelectionChangeId = useWorkletId(
|
|
798
|
+
isWorklet(onSelectionChange) ? (onSelectionChange as never) : undefined,
|
|
799
|
+
registerSelectionChange as never
|
|
800
|
+
)
|
|
801
|
+
const onSubmitEditingId = useWorkletId(
|
|
802
|
+
isWorklet(onSubmitEditing) ? (onSubmitEditing as never) : undefined,
|
|
803
|
+
registerTextEvent as never
|
|
804
|
+
)
|
|
805
|
+
const onEndEditingId = useWorkletId(
|
|
806
|
+
isWorklet(onEndEditing) ? (onEndEditing as never) : undefined,
|
|
807
|
+
registerTextEvent as never
|
|
808
|
+
)
|
|
809
|
+
const onKeyPressId = useWorkletId(
|
|
810
|
+
isWorklet(onKeyPress) ? (onKeyPress as never) : undefined,
|
|
811
|
+
registerKeyPress as never
|
|
812
|
+
)
|
|
813
|
+
// The latest native event count JS has processed: sent back with `text` so
|
|
814
|
+
// native can tell a stale controlled value (the user typed since) from a
|
|
815
|
+
// deliberate change. It lives in `eventCountRef` and is read from there at
|
|
816
|
+
// render; only a controlled field takes the state update that forces one,
|
|
817
|
+
// because only a controlled field's `text` prop can ever change. So an
|
|
818
|
+
// uncontrolled (or worklet-driven) field never re-renders on a keystroke,
|
|
819
|
+
// and a field made controlled later reads the fresh count on that render.
|
|
820
|
+
const [, setEventCountState] = useState(0)
|
|
821
|
+
const controlledRef = useRef(value != null)
|
|
822
|
+
controlledRef.current = value != null
|
|
823
|
+
// The initial text of an uncontrolled field never changes afterwards, so
|
|
824
|
+
// native applies it exactly once.
|
|
825
|
+
const [initialText] = useState(() => defaultValue ?? '')
|
|
826
|
+
|
|
827
|
+
// Nitro callbacks must be wrapped with `callback()` and the wrapper object
|
|
828
|
+
// has to be referentially stable, otherwise every render re-sets the prop.
|
|
829
|
+
// Handlers are read through a ref so inline arrows don't re-set them either.
|
|
830
|
+
// Commands sent before the native view attaches (from a ref callback, a
|
|
831
|
+
// layout effect or an effect on mount) are kept and replayed the moment it
|
|
832
|
+
// does, as React Native queues a `TextInput`'s view commands. Without this
|
|
833
|
+
// the common `useEffect(() => ref.current?.focus(), [])` was a silent no-op.
|
|
834
|
+
const pendingCommands = useRef<Array<(native: NitroInputRef) => void>>([])
|
|
835
|
+
const hybridRef = useMemo(
|
|
836
|
+
() =>
|
|
837
|
+
callback((instance: NitroInputRef) => {
|
|
838
|
+
nativeRef.current = instance
|
|
839
|
+
const queued = pendingCommands.current
|
|
840
|
+
pendingCommands.current = []
|
|
841
|
+
for (const command of queued) command(instance)
|
|
842
|
+
latest.current.onNativeRef?.(instance)
|
|
843
|
+
}),
|
|
844
|
+
[]
|
|
845
|
+
)
|
|
846
|
+
useEffect(() => {
|
|
847
|
+
if (textInputRegistry == null) return
|
|
848
|
+
const key = (hostRef.current as object | null) ?? { nitroInput: true }
|
|
849
|
+
registryKeyRef.current = key
|
|
850
|
+
patchRegistryOnce()
|
|
851
|
+
mountedFields.set(key, {
|
|
852
|
+
focus: () => nativeRef.current?.focus(),
|
|
853
|
+
blur: () => nativeRef.current?.blur(),
|
|
854
|
+
})
|
|
855
|
+
textInputRegistry.registerInput(key)
|
|
856
|
+
return () => {
|
|
857
|
+
if (textInputRegistry.currentlyFocusedInput() === key) textInputRegistry.blurInput(key)
|
|
858
|
+
textInputRegistry.unregisterInput(key)
|
|
859
|
+
mountedFields.delete(key)
|
|
860
|
+
registryKeyRef.current = null
|
|
861
|
+
}
|
|
862
|
+
}, [])
|
|
863
|
+
|
|
864
|
+
// A field with a width of its own (the usual case) never needs a React
|
|
865
|
+
// commit per keystroke: only the height (font-dependent) is taken from native.
|
|
866
|
+
const flat = useMemo(
|
|
867
|
+
() =>
|
|
868
|
+
StyleSheet.flatten(style) as
|
|
869
|
+
| { width?: unknown; flex?: unknown; height?: unknown; direction?: unknown }
|
|
870
|
+
| undefined,
|
|
871
|
+
[style]
|
|
872
|
+
)
|
|
873
|
+
// The layout direction, resolved the way React Native resolves it for its
|
|
874
|
+
// own views: the field's `style.direction` if it says, else the app's.
|
|
875
|
+
// Fabric does not hand a Hybrid View its resolved direction, so native is
|
|
876
|
+
// told outright.
|
|
877
|
+
const rightToLeft =
|
|
878
|
+
flat?.direction === 'rtl' || (flat?.direction !== 'ltr' && I18nManager.isRTL)
|
|
879
|
+
// `TextInput` stretches to its parent; sizing to content is this
|
|
880
|
+
// component's own behaviour, and it is what stops it being a drop-in.
|
|
881
|
+
// `NitroInput` turns it off so flexbox gives it the parent's width, the
|
|
882
|
+
// way a `TextInput` gets one.
|
|
883
|
+
const inferredAutoWidth = flat?.width == null && flat?.flex == null
|
|
884
|
+
const autoWidth =
|
|
885
|
+
autoWidthProp === 'auto' ? inferredAutoWidth : (autoWidthProp ?? false)
|
|
886
|
+
const autoWidthRef = useRef(autoWidth)
|
|
887
|
+
autoWidthRef.current = autoWidth
|
|
888
|
+
// When the style pins both axes, the measured size is never used: `style`
|
|
889
|
+
// is applied after `autoSize`, so it wins. Taking the state update anyway
|
|
890
|
+
// costs a React commit per field on mount, which is pure waste for the
|
|
891
|
+
// common "field in a styled box" case.
|
|
892
|
+
const measured = autoWidth || flat?.height == null
|
|
893
|
+
const measuredRef = useRef(measured)
|
|
894
|
+
measuredRef.current = measured
|
|
895
|
+
const onSizeChange = useMemo(
|
|
896
|
+
() =>
|
|
897
|
+
callback((width: number, height: number) => {
|
|
898
|
+
if (!measuredRef.current) return
|
|
899
|
+
setSize((previous) => {
|
|
900
|
+
if (!autoWidthRef.current) {
|
|
901
|
+
return previous !== null && previous.height === height ? previous : { width: previous?.width ?? width, height }
|
|
902
|
+
}
|
|
903
|
+
return previous !== null && previous.width === width && previous.height === height
|
|
904
|
+
? previous
|
|
905
|
+
: { width, height }
|
|
906
|
+
})
|
|
907
|
+
}),
|
|
908
|
+
[]
|
|
909
|
+
)
|
|
910
|
+
const onChangeTextCallback = useMemo(
|
|
911
|
+
() =>
|
|
912
|
+
callback((text: string, count: number) => {
|
|
913
|
+
textRef.current = text
|
|
914
|
+
eventCountRef.current = count
|
|
915
|
+
if (controlledRef.current) setEventCountState(count)
|
|
916
|
+
const handler = latest.current.onChangeText
|
|
917
|
+
// A worklet handler already ran on the UI thread.
|
|
918
|
+
if (handler && !isWorklet(handler)) handler(text)
|
|
919
|
+
// `onChange` fires alongside it with the same text, as in RN. The
|
|
920
|
+
// target is the host tag when there is one (iOS); Android's Nitro
|
|
921
|
+
// view hands out no instance, so it is 0 there.
|
|
922
|
+
latest.current.onChange?.({
|
|
923
|
+
nativeEvent: { text, eventCount: count, target: reactTagOf(hostRef.current) },
|
|
924
|
+
})
|
|
925
|
+
}),
|
|
926
|
+
[]
|
|
927
|
+
)
|
|
928
|
+
const onChangeValueCallback = useMemo(
|
|
929
|
+
() =>
|
|
930
|
+
callback((next: number) => {
|
|
931
|
+
const handler = latest.current.onChangeValue
|
|
932
|
+
if (handler && !isWorklet(handler)) handler(next)
|
|
933
|
+
}),
|
|
934
|
+
[]
|
|
935
|
+
)
|
|
936
|
+
const onChangeMaskCallback = useMemo(
|
|
937
|
+
() =>
|
|
938
|
+
callback(
|
|
939
|
+
(formatted: string, extracted: string, tailPlaceholder: string, complete: boolean) => {
|
|
940
|
+
latest.current.onChangeMask?.(formatted, extracted, tailPlaceholder, complete)
|
|
941
|
+
}
|
|
942
|
+
),
|
|
943
|
+
[]
|
|
944
|
+
)
|
|
945
|
+
// Every event below carries `nativeEvent` for a handler written against a
|
|
946
|
+
// `TextInput`, and the same fields at the top level for one written against
|
|
947
|
+
// this. Built here rather than natively: the native side sends the values,
|
|
948
|
+
// and the shape is a JavaScript convention React Native owns.
|
|
949
|
+
const textEvent = (text: string): NitroInputTextEvent => {
|
|
950
|
+
const target = reactTagOf(hostRef.current)
|
|
951
|
+
return { text, target, nativeEvent: { text, target } }
|
|
952
|
+
}
|
|
953
|
+
const onFocusChangeCallback = useMemo(
|
|
954
|
+
() =>
|
|
955
|
+
callback((focused: boolean) => {
|
|
956
|
+
const key = registryKeyRef.current
|
|
957
|
+
if (key != null) {
|
|
958
|
+
if (focused) textInputRegistry?.focusInput(key)
|
|
959
|
+
else textInputRegistry?.blurInput(key)
|
|
960
|
+
}
|
|
961
|
+
const text = textRef.current
|
|
962
|
+
const target = reactTagOf(hostRef.current)
|
|
963
|
+
const count = eventCountRef.current
|
|
964
|
+
const event: NitroInputFocusEvent = {
|
|
965
|
+
text,
|
|
966
|
+
target,
|
|
967
|
+
eventCount: count,
|
|
968
|
+
nativeEvent: { text, target, eventCount: count },
|
|
969
|
+
}
|
|
970
|
+
const handler = focused ? latest.current.onFocus : latest.current.onBlur
|
|
971
|
+
if (handler && !isWorklet(handler)) handler(event)
|
|
972
|
+
}),
|
|
973
|
+
[]
|
|
974
|
+
)
|
|
975
|
+
const onSubmitEditingCallback = useMemo(
|
|
976
|
+
() =>
|
|
977
|
+
callback((text: string) => {
|
|
978
|
+
const handler = latest.current.onSubmitEditing
|
|
979
|
+
if (handler && !isWorklet(handler)) handler(textEvent(text))
|
|
980
|
+
}),
|
|
981
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
982
|
+
[]
|
|
983
|
+
)
|
|
984
|
+
const onEndEditingCallback = useMemo(
|
|
985
|
+
() =>
|
|
986
|
+
callback((text: string) => {
|
|
987
|
+
const handler = latest.current.onEndEditing
|
|
988
|
+
if (handler && !isWorklet(handler)) handler(textEvent(text))
|
|
989
|
+
}),
|
|
990
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
991
|
+
[]
|
|
992
|
+
)
|
|
993
|
+
const onSelectionChangeCallback = useMemo(
|
|
994
|
+
() =>
|
|
995
|
+
callback((start: number, end: number) => {
|
|
996
|
+
const handler = latest.current.onSelectionChange
|
|
997
|
+
if (!handler || isWorklet(handler)) return
|
|
998
|
+
const selection = { start, end }
|
|
999
|
+
const target = reactTagOf(hostRef.current)
|
|
1000
|
+
handler({ selection, start, end, target, nativeEvent: { selection, target } })
|
|
1001
|
+
}),
|
|
1002
|
+
[]
|
|
1003
|
+
)
|
|
1004
|
+
const onKeyPressCallback = useMemo(
|
|
1005
|
+
() =>
|
|
1006
|
+
callback((key: string) => {
|
|
1007
|
+
const handler = latest.current.onKeyPress
|
|
1008
|
+
if (!handler || isWorklet(handler)) return
|
|
1009
|
+
const target = reactTagOf(hostRef.current)
|
|
1010
|
+
const count = eventCountRef.current
|
|
1011
|
+
handler({ key, eventCount: count, target, nativeEvent: { key, eventCount: count, target } })
|
|
1012
|
+
}),
|
|
1013
|
+
[]
|
|
1014
|
+
)
|
|
1015
|
+
|
|
1016
|
+
// One handle for the component's lifetime: everything it reads goes
|
|
1017
|
+
// through a ref, so a value change never hands the parent a new object.
|
|
1018
|
+
useImperativeHandle(
|
|
1019
|
+
ref,
|
|
1020
|
+
() => {
|
|
1021
|
+
const send = (command: (native: NitroInputRef) => void) => {
|
|
1022
|
+
const native = nativeRef.current
|
|
1023
|
+
if (native != null) command(native)
|
|
1024
|
+
else pendingCommands.current.push(command)
|
|
1025
|
+
}
|
|
1026
|
+
return {
|
|
1027
|
+
focus: () => send((native) => native.focus()),
|
|
1028
|
+
blur: () => send((native) => native.blur()),
|
|
1029
|
+
clear: () => send((native) => native.clear()),
|
|
1030
|
+
setText: (text) => send((native) => native.replaceText(text)),
|
|
1031
|
+
setValue: (next) => send((native) => native.setValue(next)),
|
|
1032
|
+
// Before the native view attaches, `textRef` is a controlled field's
|
|
1033
|
+
// `value` and an uncontrolled one's initial text.
|
|
1034
|
+
getText: () => nativeRef.current?.currentText() ?? textRef.current,
|
|
1035
|
+
getValue: () => nativeRef.current?.getValue() ?? NaN,
|
|
1036
|
+
// The native view is the truth once there is one - it knows whether it
|
|
1037
|
+
// actually holds first responder. Before it attaches, fall back to the
|
|
1038
|
+
// registry this field already keeps up to date, which is the same place
|
|
1039
|
+
// `TextInput.isFocused()` reads from.
|
|
1040
|
+
isFocused: () =>
|
|
1041
|
+
nativeRef.current?.isFocused() ??
|
|
1042
|
+
(registryKeyRef.current != null &&
|
|
1043
|
+
textInputRegistry?.currentlyFocusedInput() === registryKeyRef.current),
|
|
1044
|
+
setSelection: (start: number, end?: number) => send((native) => native.setSelection(start, end ?? start)),
|
|
1045
|
+
get native() {
|
|
1046
|
+
return nativeRef.current
|
|
1047
|
+
},
|
|
1048
|
+
}
|
|
1049
|
+
},
|
|
1050
|
+
[]
|
|
1051
|
+
)
|
|
1052
|
+
|
|
1053
|
+
// Every native prop is sent with an explicit value: an optional prop that
|
|
1054
|
+
// is *removed* reaches native as `null`, which Nitro's parser rejects and
|
|
1055
|
+
// React Native turns into a fatal error. `Infinity` stands for "platform default"
|
|
1056
|
+
// where there is no value to express it (colors), `''` for fonts.
|
|
1057
|
+
// Not `NaN`: Nitro only calls a native setter when a prop's value changed,
|
|
1058
|
+
// and NaN never equals itself, so a NaN sentinel re-applied the whole
|
|
1059
|
+
// configuration on every render (every keystroke of a controlled field).
|
|
1060
|
+
const processedColor = useMemo(() => toProcessedColor(color) ?? Infinity, [color])
|
|
1061
|
+
const processedPlaceholderColor = useMemo(
|
|
1062
|
+
() => toProcessedColor(placeholderTextColor) ?? Infinity,
|
|
1063
|
+
[placeholderTextColor]
|
|
1064
|
+
)
|
|
1065
|
+
const processedCursorColor = useMemo(
|
|
1066
|
+
() => toProcessedColor(cursorColor) ?? Infinity,
|
|
1067
|
+
[cursorColor]
|
|
1068
|
+
)
|
|
1069
|
+
const processedSelectionColor = useMemo(
|
|
1070
|
+
() => toProcessedColor(selectionColor) ?? Infinity,
|
|
1071
|
+
[selectionColor]
|
|
1072
|
+
)
|
|
1073
|
+
const processedLabelColor = useMemo(() => toProcessedColor(labelColor) ?? Infinity, [labelColor])
|
|
1074
|
+
const processedLabelFocusedColor = useMemo(
|
|
1075
|
+
() => toProcessedColor(labelFocusedColor) ?? Infinity,
|
|
1076
|
+
[labelFocusedColor]
|
|
1077
|
+
)
|
|
1078
|
+
const processedStrokeColor = useMemo(() => toProcessedColor(strokeColor) ?? Infinity, [strokeColor])
|
|
1079
|
+
const processedFocusedStrokeColor = useMemo(
|
|
1080
|
+
() => toProcessedColor(focusedStrokeColor) ?? Infinity,
|
|
1081
|
+
[focusedStrokeColor]
|
|
1082
|
+
)
|
|
1083
|
+
const processedFillColor = useMemo(() => toProcessedColor(fillColor) ?? Infinity, [fillColor])
|
|
1084
|
+
const numericWeight = toNumericWeight(fontWeight) ?? 400
|
|
1085
|
+
const resolvedFontSize = fontSize ?? 32
|
|
1086
|
+
const resolvedAffixAlign = affixAlign ?? 'baseline'
|
|
1087
|
+
// Multiline is drawn by the system view and is always text: the glyph
|
|
1088
|
+
// engine lays one run out on one baseline, and an amount or a mask is a
|
|
1089
|
+
// single-line idea. Rather than half-honour the combination, drop the part
|
|
1090
|
+
// that cannot work and say so.
|
|
1091
|
+
const isMultiline = multiline ?? false
|
|
1092
|
+
if (__DEV__ && isMultiline) {
|
|
1093
|
+
if (mode != null && mode !== 'text') {
|
|
1094
|
+
warnIncompatible(`mode="${mode}"`)
|
|
1095
|
+
}
|
|
1096
|
+
if (morph) warnIncompatible('morph')
|
|
1097
|
+
if (prefix) warnIncompatible('prefix')
|
|
1098
|
+
if (suffix) warnIncompatible('suffix')
|
|
1099
|
+
}
|
|
1100
|
+
const resolvedMode = isMultiline ? 'text' : (mode ?? 'text')
|
|
1101
|
+
const resolvedMorph = isMultiline ? false : (morph ?? false)
|
|
1102
|
+
// The affixes are accessory views beside one line of text; a wrapping text
|
|
1103
|
+
// view has no slot for them, so they go with the mode rather than being
|
|
1104
|
+
// half-drawn on one platform and not the other.
|
|
1105
|
+
const resolvedPrefix = isMultiline ? '' : (prefix ?? '')
|
|
1106
|
+
const resolvedSuffix = isMultiline ? '' : (suffix ?? '')
|
|
1107
|
+
// A new `maskNotations` literal each render must not re-set the native
|
|
1108
|
+
// prop, so it is keyed on its contents. Always an array (empty = none):
|
|
1109
|
+
// removing the prop would reach native as `null`, which Nitro's array
|
|
1110
|
+
// parser rejects.
|
|
1111
|
+
const notationsKey =
|
|
1112
|
+
maskNotations != null && maskNotations.length > 0 ? JSON.stringify(maskNotations) : ''
|
|
1113
|
+
const stableNotations = useMemo(
|
|
1114
|
+
() => (notationsKey !== '' && maskNotations ? [...maskNotations] : EMPTY_NOTATIONS),
|
|
1115
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1116
|
+
[notationsKey]
|
|
1117
|
+
)
|
|
1118
|
+
const resolvedFractionDigits = fractionDigits ?? 2
|
|
1119
|
+
const resolvedKeyboardType =
|
|
1120
|
+
keyboardType ??
|
|
1121
|
+
(inputMode != null
|
|
1122
|
+
? INPUT_MODE_TO_KEYBOARD[inputMode]
|
|
1123
|
+
: resolvedMode === 'number'
|
|
1124
|
+
? resolvedFractionDigits > 0
|
|
1125
|
+
? 'decimal-pad'
|
|
1126
|
+
: 'number-pad'
|
|
1127
|
+
: 'default')
|
|
1128
|
+
const resolvedReturnKeyType =
|
|
1129
|
+
returnKeyType ?? (enterKeyHint != null ? ENTER_KEY_HINT_TO_RETURN_KEY[enterKeyHint] : 'default')
|
|
1130
|
+
// `inputMode="none"` means "focus it but show no keyboard", as in RN.
|
|
1131
|
+
const resolvedShowSoftInput =
|
|
1132
|
+
showSoftInputOnFocus ?? (inputMode != null ? inputMode !== 'none' : true)
|
|
1133
|
+
|
|
1134
|
+
// React Native resolves its HTML-flavoured aliases inside its own
|
|
1135
|
+
// components; a Nitro view is handed the raw props, so `aria-label` and
|
|
1136
|
+
// `id` would arrive unresolved and be dropped by the C++ parser. Both take
|
|
1137
|
+
// precedence over the older spelling, as they do on a `TextInput`.
|
|
1138
|
+
//
|
|
1139
|
+
// The label goes on the hidden field (below), so leaving it on the host as
|
|
1140
|
+
// well would put the same label on two accessibility elements. `testID`
|
|
1141
|
+
// stays on the host too: it makes no accessibility element of its own, and
|
|
1142
|
+
// a test that already queries the host must keep working.
|
|
1143
|
+
const {
|
|
1144
|
+
accessibilityLabel,
|
|
1145
|
+
'aria-label': ariaLabel,
|
|
1146
|
+
'aria-busy': ariaBusy,
|
|
1147
|
+
'aria-checked': ariaChecked,
|
|
1148
|
+
'aria-disabled': ariaDisabled,
|
|
1149
|
+
'aria-expanded': ariaExpanded,
|
|
1150
|
+
'aria-selected': ariaSelected,
|
|
1151
|
+
'aria-hidden': ariaHidden,
|
|
1152
|
+
'aria-labelledby': ariaLabelledBy,
|
|
1153
|
+
accessibilityState,
|
|
1154
|
+
accessibilityElementsHidden,
|
|
1155
|
+
accessibilityLabelledBy,
|
|
1156
|
+
importantForAccessibility,
|
|
1157
|
+
id,
|
|
1158
|
+
nativeID,
|
|
1159
|
+
...restViewProps
|
|
1160
|
+
} = viewProps as typeof viewProps & { 'aria-label'?: string }
|
|
1161
|
+
const resolvedAccessibilityLabel = ariaLabel ?? accessibilityLabel
|
|
1162
|
+
const resolvedNativeID = id ?? nativeID
|
|
1163
|
+
// The `aria-*` state, hidden and labelled-by props, folded the way
|
|
1164
|
+
// `TextInput` folds them: the aria spelling wins over the older one, and
|
|
1165
|
+
// `aria-hidden` becomes the platform's own notion of hidden.
|
|
1166
|
+
const hasState =
|
|
1167
|
+
accessibilityState != null ||
|
|
1168
|
+
ariaBusy != null ||
|
|
1169
|
+
ariaChecked != null ||
|
|
1170
|
+
ariaDisabled != null ||
|
|
1171
|
+
ariaExpanded != null ||
|
|
1172
|
+
ariaSelected != null
|
|
1173
|
+
const resolvedAccessibilityState = hasState
|
|
1174
|
+
? {
|
|
1175
|
+
busy: ariaBusy ?? accessibilityState?.busy,
|
|
1176
|
+
checked: ariaChecked ?? accessibilityState?.checked,
|
|
1177
|
+
disabled: ariaDisabled ?? accessibilityState?.disabled,
|
|
1178
|
+
expanded: ariaExpanded ?? accessibilityState?.expanded,
|
|
1179
|
+
selected: ariaSelected ?? accessibilityState?.selected,
|
|
1180
|
+
}
|
|
1181
|
+
: undefined
|
|
1182
|
+
const resolvedElementsHidden = ariaHidden ?? accessibilityElementsHidden
|
|
1183
|
+
const resolvedImportance =
|
|
1184
|
+
ariaHidden === true ? ('no-hide-descendants' as const) : importantForAccessibility
|
|
1185
|
+
const resolvedLabelledBy = ariaLabelledBy ?? accessibilityLabelledBy
|
|
1186
|
+
// Added only when there is one: a native prop is never sent `undefined`.
|
|
1187
|
+
const hostViewProps: typeof restViewProps & {
|
|
1188
|
+
nativeID?: string
|
|
1189
|
+
accessibilityState?: typeof resolvedAccessibilityState
|
|
1190
|
+
accessibilityElementsHidden?: boolean
|
|
1191
|
+
importantForAccessibility?: typeof resolvedImportance
|
|
1192
|
+
accessibilityLabelledBy?: typeof resolvedLabelledBy
|
|
1193
|
+
} = { ...restViewProps }
|
|
1194
|
+
if (resolvedNativeID != null) hostViewProps.nativeID = resolvedNativeID
|
|
1195
|
+
if (resolvedAccessibilityState != null) hostViewProps.accessibilityState = resolvedAccessibilityState
|
|
1196
|
+
if (resolvedElementsHidden != null) hostViewProps.accessibilityElementsHidden = resolvedElementsHidden
|
|
1197
|
+
if (resolvedImportance != null) hostViewProps.importantForAccessibility = resolvedImportance
|
|
1198
|
+
if (resolvedLabelledBy != null) hostViewProps.accessibilityLabelledBy = resolvedLabelledBy
|
|
1199
|
+
|
|
1200
|
+
const autoSize = useMemo(
|
|
1201
|
+
() =>
|
|
1202
|
+
size !== null
|
|
1203
|
+
? autoWidth
|
|
1204
|
+
? { width: size.width, height: size.height }
|
|
1205
|
+
: { height: size.height }
|
|
1206
|
+
: { height: resolvedFontSize * 1.25 },
|
|
1207
|
+
[size, resolvedFontSize, autoWidth]
|
|
1208
|
+
)
|
|
1209
|
+
|
|
1210
|
+
return (
|
|
1211
|
+
<NativeNitroInputView
|
|
1212
|
+
{...hostViewProps}
|
|
1213
|
+
ref={hostRef as React.Ref<never>}
|
|
1214
|
+
style={[autoSize, style]}
|
|
1215
|
+
hybridRef={hybridRef}
|
|
1216
|
+
text={value ?? initialText}
|
|
1217
|
+
mostRecentEventCount={eventCountRef.current}
|
|
1218
|
+
mode={resolvedMode}
|
|
1219
|
+
plain={!resolvedMorph}
|
|
1220
|
+
fractionDigits={resolvedFractionDigits}
|
|
1221
|
+
maxIntegerDigits={maxIntegerDigits ?? 15}
|
|
1222
|
+
groupingSeparator={groupingSeparator ?? ','}
|
|
1223
|
+
decimalSeparator={decimalSeparator ?? '.'}
|
|
1224
|
+
prefix={resolvedPrefix}
|
|
1225
|
+
suffix={resolvedSuffix}
|
|
1226
|
+
prefixFontSize={prefixFontSize ?? resolvedFontSize}
|
|
1227
|
+
suffixFontSize={suffixFontSize ?? resolvedFontSize}
|
|
1228
|
+
signPlacement={signPlacement ?? 'beforeAffix'}
|
|
1229
|
+
prefixAlign={prefixAlign ?? resolvedAffixAlign}
|
|
1230
|
+
suffixAlign={suffixAlign ?? resolvedAffixAlign}
|
|
1231
|
+
placeholder={placeholder ?? ''}
|
|
1232
|
+
placeholderColor={processedPlaceholderColor}
|
|
1233
|
+
duration={duration ?? 400}
|
|
1234
|
+
easing={easing ?? 'expo'}
|
|
1235
|
+
bounce={bounce ?? 0.15}
|
|
1236
|
+
effect={effect ?? 'auto'}
|
|
1237
|
+
fontSize={resolvedFontSize}
|
|
1238
|
+
lineHeight={Math.max(0, lineHeight ?? 0)}
|
|
1239
|
+
fontWeight={numericWeight}
|
|
1240
|
+
fontFamily={fontFamily ?? ''}
|
|
1241
|
+
color={processedColor}
|
|
1242
|
+
textAlign={textAlign ?? 'auto'}
|
|
1243
|
+
rightToLeft={rightToLeft}
|
|
1244
|
+
caretColor={processedCursorColor}
|
|
1245
|
+
selectionColor={processedSelectionColor}
|
|
1246
|
+
caretHidden={caretHidden ?? false}
|
|
1247
|
+
adjustsFontSizeToFit={adjustsFontSizeToFit ?? false}
|
|
1248
|
+
minimumFontScale={minimumFontScale ?? 0.5}
|
|
1249
|
+
allowFontScaling={allowFontScaling ?? false}
|
|
1250
|
+
maxFontSizeMultiplier={maxFontSizeMultiplier ?? 0}
|
|
1251
|
+
variant={variant ?? 'none'}
|
|
1252
|
+
label={label ?? ''}
|
|
1253
|
+
labelBehavior={labelBehavior ?? 'float'}
|
|
1254
|
+
labelColor={processedLabelColor}
|
|
1255
|
+
labelFocusedColor={processedLabelFocusedColor}
|
|
1256
|
+
labelFontSize={labelFontSize ?? 0}
|
|
1257
|
+
strokeColor={processedStrokeColor}
|
|
1258
|
+
focusedStrokeColor={processedFocusedStrokeColor}
|
|
1259
|
+
strokeWidth={strokeWidth ?? 1}
|
|
1260
|
+
cornerRadius={cornerRadius ?? 8}
|
|
1261
|
+
fillColor={processedFillColor}
|
|
1262
|
+
mask={resolvedMode === 'mask' ? (mask ?? '') : ''}
|
|
1263
|
+
maskNotations={stableNotations}
|
|
1264
|
+
maskAutocomplete={maskAutocomplete ?? true}
|
|
1265
|
+
maskAutoSkip={maskAutoSkip ?? false}
|
|
1266
|
+
onChangeMask={onChangeMaskCallback}
|
|
1267
|
+
keyboardType={resolvedKeyboardType}
|
|
1268
|
+
returnKeyType={resolvedReturnKeyType}
|
|
1269
|
+
autoCapitalize={autoCapitalize ?? 'sentences'}
|
|
1270
|
+
autoCorrect={autoCorrect ?? true}
|
|
1271
|
+
editable={(editable ?? true) && !(readOnly ?? false)}
|
|
1272
|
+
multiline={isMultiline}
|
|
1273
|
+
numberOfLines={Math.max(0, Math.trunc(numberOfLines ?? rows ?? 0))}
|
|
1274
|
+
textAlignVertical={textAlignVertical ?? 'auto'}
|
|
1275
|
+
scrollEnabled={scrollEnabled ?? true}
|
|
1276
|
+
autoFocus={autoFocus ?? false}
|
|
1277
|
+
fieldTestID={viewProps.testID ?? ''}
|
|
1278
|
+
fieldAccessibilityLabel={resolvedAccessibilityLabel ?? ''}
|
|
1279
|
+
submitBehavior={
|
|
1280
|
+
submitBehavior ??
|
|
1281
|
+
// As `TextInput` resolves the legacy `blurOnSubmit`: a multiline
|
|
1282
|
+
// field's return key inserts a line break unless told to blur.
|
|
1283
|
+
(multiline
|
|
1284
|
+
? blurOnSubmit === true
|
|
1285
|
+
? 'blurAndSubmit'
|
|
1286
|
+
: 'newline'
|
|
1287
|
+
: blurOnSubmit === false
|
|
1288
|
+
? 'submit'
|
|
1289
|
+
: 'blurAndSubmit')
|
|
1290
|
+
}
|
|
1291
|
+
secureTextEntry={secureTextEntry ?? false}
|
|
1292
|
+
keyboardAppearance={keyboardAppearance ?? 'default'}
|
|
1293
|
+
textContentType={textContentType ?? autoComplete ?? ''}
|
|
1294
|
+
enablesReturnKeyAutomatically={enablesReturnKeyAutomatically ?? false}
|
|
1295
|
+
showSoftInputOnFocus={resolvedShowSoftInput}
|
|
1296
|
+
selectTextOnFocus={selectTextOnFocus ?? false}
|
|
1297
|
+
clearTextOnFocus={clearTextOnFocus ?? false}
|
|
1298
|
+
contextMenuHidden={contextMenuHidden ?? false}
|
|
1299
|
+
spellCheck={spellCheck ?? autoCorrect ?? true}
|
|
1300
|
+
selectionStart={selection?.start ?? -1}
|
|
1301
|
+
selectionEnd={selection?.end ?? selection?.start ?? -1}
|
|
1302
|
+
maxLength={maxLength ?? 0}
|
|
1303
|
+
transformWorklet={transformId}
|
|
1304
|
+
onChangeTextWorklet={onChangeTextId}
|
|
1305
|
+
onChangeValueWorklet={onChangeValueId}
|
|
1306
|
+
onFocusChangeWorklet={onFocusId}
|
|
1307
|
+
onSelectionChangeWorklet={onSelectionChangeId}
|
|
1308
|
+
onSubmitEditingWorklet={onSubmitEditingId}
|
|
1309
|
+
onEndEditingWorklet={onEndEditingId}
|
|
1310
|
+
onKeyPressWorklet={onKeyPressId}
|
|
1311
|
+
onChangeText={onChangeTextCallback}
|
|
1312
|
+
onChangeValue={onChangeValueCallback}
|
|
1313
|
+
onFocusChange={onFocusChangeCallback}
|
|
1314
|
+
onSubmitEditing={onSubmitEditingCallback}
|
|
1315
|
+
onEndEditing={onEndEditingCallback}
|
|
1316
|
+
onSelectionChange={onSelectionChangeCallback}
|
|
1317
|
+
onKeyPress={onKeyPressCallback}
|
|
1318
|
+
onSizeChange={onSizeChange}
|
|
1319
|
+
/>
|
|
1320
|
+
)
|
|
1321
|
+
}
|
|
1322
|
+
)
|
|
1323
|
+
// The name DevTools and error stacks show, as `TextInput` names itself.
|
|
1324
|
+
NitroInput.displayName = 'NitroInput'
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* Registers `fn` on the UI runtime for as long as it stays the same function
|
|
1328
|
+
* and returns its id (`0` while there is none or worklets are unavailable).
|
|
1329
|
+
*
|
|
1330
|
+
* The id is reserved during render so the first commit already carries it, but
|
|
1331
|
+
* the registration itself happens in the effect: a render React discards (under
|
|
1332
|
+
* StrictMode, or a concurrent render that loses) would otherwise leave an entry
|
|
1333
|
+
* on the UI runtime that no cleanup ever removes.
|
|
1334
|
+
*/
|
|
1335
|
+
function useWorkletId<T extends (...args: never[]) => unknown>(
|
|
1336
|
+
fn: T | undefined,
|
|
1337
|
+
register: (fn: T, id: number) => void
|
|
1338
|
+
): number {
|
|
1339
|
+
const id = useMemo(() => (fn ? allocateWorkletId() : 0), [fn])
|
|
1340
|
+
useEffect(() => {
|
|
1341
|
+
if (id === 0 || !fn) return
|
|
1342
|
+
register(fn, id)
|
|
1343
|
+
return () => unregisterWorklet(id)
|
|
1344
|
+
}, [id, fn, register])
|
|
1345
|
+
return id
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/**
|
|
1349
|
+
* The same, for the focus pair: native reports one focus change, so the two
|
|
1350
|
+
* handlers share an id and the wrapper picks between them.
|
|
1351
|
+
*/
|
|
1352
|
+
function useWorkletPairId<T extends (...args: never[]) => unknown>(
|
|
1353
|
+
onFocus: T | undefined,
|
|
1354
|
+
onBlur: T | undefined,
|
|
1355
|
+
register: (a: T | undefined, b: T | undefined, id: number) => void
|
|
1356
|
+
): number {
|
|
1357
|
+
const id = useMemo(() => (onFocus || onBlur ? allocateWorkletId() : 0), [onFocus, onBlur])
|
|
1358
|
+
useEffect(() => {
|
|
1359
|
+
if (id === 0) return
|
|
1360
|
+
register(onFocus, onBlur, id)
|
|
1361
|
+
return () => unregisterWorklet(id)
|
|
1362
|
+
}, [id, onFocus, onBlur, register])
|
|
1363
|
+
return id
|
|
1364
|
+
}
|