react-native-nitro-input 0.3.1 → 0.3.3
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 +114 -0
- package/README.md +75 -3
- package/android/src/main/cpp/JMaskEngine.cpp +45 -0
- package/android/src/main/cpp/JMaskEngine.hpp +9 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +49 -4
- package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +266 -8
- package/android/src/main/java/com/margelo/nitro/nitroinput/UnmountHandoffs.kt +57 -0
- package/cpp/MaskEngine.cpp +294 -33
- package/cpp/MaskEngine.hpp +68 -0
- package/ios/HybridNitroInputView.swift +37 -4
- package/ios/NitroInputCommands.mm +10 -0
- package/ios/NitroInputView.swift +474 -20
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/lib/commonjs/src/NitroInput.js +116 -15
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/lib/module/src/NitroInput.js +117 -16
- package/lib/typescript/src/NitroInput.d.ts +55 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +124 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +17 -0
- package/nitrogen/generated/android/c++/JNitroInputCharacterMapping.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputMaskAffinity.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroInputTextCase.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +40 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +52 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputCharacterMapping.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMaskAffinity.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextCase.kt +24 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +25 -11
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +9 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +68 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +48 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +9 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +111 -0
- package/nitrogen/generated/ios/swift/NitroInputCharacterMapping.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputMaskAffinity.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroInputTextCase.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +17 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +26 -0
- package/nitrogen/generated/shared/c++/NitroInputCharacterMapping.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputMaskAffinity.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroInputTextCase.hpp +80 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +16 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +27 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +8 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +190 -11
- package/src/index.ts +3 -0
- package/src/specs/NitroInput.nitro.ts +68 -0
package/src/NitroInput.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import React, {
|
|
|
2
2
|
forwardRef,
|
|
3
3
|
useEffect,
|
|
4
4
|
useImperativeHandle,
|
|
5
|
+
useLayoutEffect,
|
|
5
6
|
useMemo,
|
|
6
7
|
useRef,
|
|
7
8
|
useState,
|
|
@@ -37,6 +38,9 @@ import type {
|
|
|
37
38
|
NitroInputSignPlacement,
|
|
38
39
|
NitroInputVariant,
|
|
39
40
|
NitroInputNotation,
|
|
41
|
+
NitroInputMaskAffinity,
|
|
42
|
+
NitroInputTextCase,
|
|
43
|
+
NitroInputCharacterMapping,
|
|
40
44
|
} from './specs/NitroInput.nitro'
|
|
41
45
|
import {
|
|
42
46
|
allocateWorkletId,
|
|
@@ -152,9 +156,25 @@ function reactTagOf(host: unknown): number {
|
|
|
152
156
|
}
|
|
153
157
|
|
|
154
158
|
const EMPTY_NOTATIONS: NitroInputNotation[] = []
|
|
159
|
+
const EMPTY_STRINGS: string[] = []
|
|
160
|
+
const EMPTY_MAPPINGS: NitroInputCharacterMapping[] = []
|
|
155
161
|
|
|
156
162
|
/** Mounted fields, by the host instance React Native's registry stores. */
|
|
157
163
|
const mountedFields = new WeakMap<object, { focus(): void; blur(): void }>()
|
|
164
|
+
/** The same fields, iterable: `Keyboard.dismiss()` with nothing focused reaches one through it. */
|
|
165
|
+
const liveFields = new Set<{ focus(): void; blur(): void }>()
|
|
166
|
+
/**
|
|
167
|
+
* The last field that handed the keyboard off as it unmounted, kept until its
|
|
168
|
+
* hold is over: a screen may have no field left (a tap-only question) when it
|
|
169
|
+
* calls `Keyboard.dismiss()`, and the hold is released through a native blur.
|
|
170
|
+
*/
|
|
171
|
+
let lastHandoff: { blur(): void } | null = null
|
|
172
|
+
/**
|
|
173
|
+
* Which field a native view currently belongs to. A view can change hands:
|
|
174
|
+
* on iOS a hidden <Activity>'s views go back to the pool and another field
|
|
175
|
+
* may be given one; on Android they are kept for the same field.
|
|
176
|
+
*/
|
|
177
|
+
const viewOwner = new WeakMap<object, object>()
|
|
158
178
|
let patchedRegistry = false
|
|
159
179
|
|
|
160
180
|
/**
|
|
@@ -190,7 +210,12 @@ function patchRegistryOnce() {
|
|
|
190
210
|
registry.blurTextInput = (input: unknown) => {
|
|
191
211
|
const field = input != null ? mountedFields.get(input as object) : undefined
|
|
192
212
|
if (field) return field.blur()
|
|
193
|
-
|
|
213
|
+
const result = originalBlur.call(registry, input)
|
|
214
|
+
// `Keyboard.dismiss()` with no input focused: a field that just left the
|
|
215
|
+
// screen may still be holding the keyboard for the next one
|
|
216
|
+
// (`keyboardHandoffMs`). A native blur on any field lets that go.
|
|
217
|
+
if (input == null) (liveFields.values().next().value ?? lastHandoff)?.blur()
|
|
218
|
+
return result
|
|
194
219
|
}
|
|
195
220
|
}
|
|
196
221
|
}
|
|
@@ -318,6 +343,44 @@ export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus'
|
|
|
318
343
|
maskAutocomplete?: boolean
|
|
319
344
|
/** `'mask'`: backspace walks back over autocompleted constants. Default: `false`. */
|
|
320
345
|
maskAutoSkip?: boolean
|
|
346
|
+
/**
|
|
347
|
+
* `'mask'`: more patterns the text may take, e.g. a 4-6-5 Amex card beside
|
|
348
|
+
* a 4-4-4-4 `mask`: `['{34}[00] [000000] [00000]', '{37}[00] [000000] [00000]']`.
|
|
349
|
+
* Every edit is masked with each and the best, by `maskAffinityStrategy`, is
|
|
350
|
+
* kept; `mask` wins ties.
|
|
351
|
+
*/
|
|
352
|
+
maskAffinityFormats?: string[]
|
|
353
|
+
/**
|
|
354
|
+
* `'mask'`: how the best of `maskAffinityFormats` is chosen. `'wholeString'`
|
|
355
|
+
* the format that accepts the most characters and inserts the fewest,
|
|
356
|
+
* `'prefix'` the one whose output starts most like what was typed,
|
|
357
|
+
* `'capacity'` / `'extractedValueCapacity'` the fullest format that still
|
|
358
|
+
* holds all of the text / all of its value characters. Default: `'wholeString'`.
|
|
359
|
+
*/
|
|
360
|
+
maskAffinityStrategy?: NitroInputMaskAffinity
|
|
361
|
+
/** `'mask'`: uppercases or lowercases letters as they are typed or pasted. Default: `'none'`. */
|
|
362
|
+
maskTextCase?: NitroInputTextCase
|
|
363
|
+
/**
|
|
364
|
+
* `'mask'`: characters replaced before masking, one character each:
|
|
365
|
+
* `{ 'С': 'C' }` turns a Cyrillic look-alike into a Latin letter,
|
|
366
|
+
* `{ ',': '.' }` lets a comma through to a dot-decimal mask. An empty
|
|
367
|
+
* replacement drops the character.
|
|
368
|
+
*/
|
|
369
|
+
maskCharacterMap?: Record<string, string>
|
|
370
|
+
/**
|
|
371
|
+
* `'mask'`: the character each empty slot shows in the kept placeholder and
|
|
372
|
+
* in `onChangeMask`'s tail. Default: `'_'` with `keepPlaceholder`, otherwise
|
|
373
|
+
* the slot's own notation character.
|
|
374
|
+
*/
|
|
375
|
+
maskSlotPlaceholder?: string
|
|
376
|
+
/**
|
|
377
|
+
* `'mask'`: keeps the rest of the placeholder visible after what has been
|
|
378
|
+
* typed: with `placeholder="1234 5678 9012"`, typing "12345" shows
|
|
379
|
+
* "1234 5" and then "678 9012" greyed. Without a `placeholder` the mask
|
|
380
|
+
* itself is shown, its empty slots as `maskSlotPlaceholder`:
|
|
381
|
+
* `+1 (212) ___-____`. Default: `false`.
|
|
382
|
+
*/
|
|
383
|
+
keepPlaceholder?: boolean
|
|
321
384
|
/**
|
|
322
385
|
* `'mask'` mode: called whenever the text changes, from a keystroke,
|
|
323
386
|
* `setText` / `clear` or the `value` prop alike, with the masked text, the
|
|
@@ -503,6 +566,22 @@ export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus'
|
|
|
503
566
|
selectTextOnFocus?: boolean
|
|
504
567
|
/** Empty the field when it gains focus. Default: `false`. */
|
|
505
568
|
clearTextOnFocus?: boolean
|
|
569
|
+
/**
|
|
570
|
+
* When this field is popped or unmounted while it has the keyboard, keep the
|
|
571
|
+
* keyboard up this many milliseconds so the next field to focus (the screen
|
|
572
|
+
* you return to, the next step's field) takes it over in place instead of
|
|
573
|
+
* the keyboard dropping and rising again. If no field does, the keyboard
|
|
574
|
+
* hides when the time runs out; `Keyboard.dismiss()` hides it at once.
|
|
575
|
+
* 300-500 covers a stack pop. Default: `0` (off).
|
|
576
|
+
*/
|
|
577
|
+
keyboardHandoffMs?: number
|
|
578
|
+
/**
|
|
579
|
+
* iOS: on the number, phone and decimal pads, which have no return key, a
|
|
580
|
+
* bar over the keyboard with the `returnKeyType` key ("Next", "Done"...), as
|
|
581
|
+
* `TextInput` shows. `false` leaves the pad bare - for a screen that has its
|
|
582
|
+
* own button riding the keyboard. Default: `true`.
|
|
583
|
+
*/
|
|
584
|
+
returnKeyBar?: boolean
|
|
506
585
|
/** Hides the Cut/Copy/Paste menu. Default: `false`. */
|
|
507
586
|
contextMenuHidden?: boolean
|
|
508
587
|
/** Spell checking in `'text'` mode. Defaults to `autoCorrect`. */
|
|
@@ -698,6 +777,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
698
777
|
maskNotations,
|
|
699
778
|
maskAutocomplete,
|
|
700
779
|
maskAutoSkip,
|
|
780
|
+
maskAffinityFormats,
|
|
781
|
+
maskAffinityStrategy,
|
|
782
|
+
maskTextCase,
|
|
783
|
+
maskCharacterMap,
|
|
784
|
+
maskSlotPlaceholder,
|
|
785
|
+
keepPlaceholder,
|
|
701
786
|
returnKeyType,
|
|
702
787
|
autoCapitalize,
|
|
703
788
|
autoCorrect,
|
|
@@ -720,6 +805,8 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
720
805
|
showSoftInputOnFocus,
|
|
721
806
|
selectTextOnFocus,
|
|
722
807
|
clearTextOnFocus,
|
|
808
|
+
keyboardHandoffMs,
|
|
809
|
+
returnKeyBar,
|
|
723
810
|
contextMenuHidden,
|
|
724
811
|
spellCheck,
|
|
725
812
|
readOnly,
|
|
@@ -743,6 +830,28 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
743
830
|
ref
|
|
744
831
|
) {
|
|
745
832
|
const nativeRef = useRef<NitroInputRef | null>(null)
|
|
833
|
+
// `keyboardHandoffMs`: hand the keyboard off while this field still has it.
|
|
834
|
+
// A layout-effect cleanup runs in React's commit, so the native call is
|
|
835
|
+
// queued on the main thread before Fabric's removal of the view - by then
|
|
836
|
+
// UIKit would already have taken first responder away.
|
|
837
|
+
const handoffRef = useRef(keyboardHandoffMs ?? 0)
|
|
838
|
+
handoffRef.current = keyboardHandoffMs ?? 0
|
|
839
|
+
useLayoutEffect(
|
|
840
|
+
() => () => {
|
|
841
|
+
if (handoffRef.current > 0) {
|
|
842
|
+
const native = nativeRef.current
|
|
843
|
+
native?.prepareForUnmount()
|
|
844
|
+
if (native) {
|
|
845
|
+
const handoff = { blur: () => native.blur() }
|
|
846
|
+
lastHandoff = handoff
|
|
847
|
+
setTimeout(() => {
|
|
848
|
+
if (lastHandoff === handoff) lastHandoff = null
|
|
849
|
+
}, handoffRef.current + 100)
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
},
|
|
853
|
+
[]
|
|
854
|
+
)
|
|
746
855
|
// What React Native's registry stores for this field. The React host
|
|
747
856
|
// instance when there is one (iOS), otherwise a stable per-instance token:
|
|
748
857
|
// on Android the Nitro host component hands out no instance, and the
|
|
@@ -824,9 +933,9 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
824
933
|
const [, setEventCountState] = useState(0)
|
|
825
934
|
const controlledRef = useRef(value != null)
|
|
826
935
|
controlledRef.current = value != null
|
|
827
|
-
//
|
|
828
|
-
//
|
|
829
|
-
const [
|
|
936
|
+
// An uncontrolled field's text for its native view: its initial text, and
|
|
937
|
+
// what it held when <Activity> last hid it (see the layout effect below).
|
|
938
|
+
const [uncontrolledText, setUncontrolledText] = useState(() => defaultValue ?? '')
|
|
830
939
|
|
|
831
940
|
// Nitro callbacks must be wrapped with `callback()` and the wrapper object
|
|
832
941
|
// has to be referentially stable, otherwise every render re-sets the prop.
|
|
@@ -836,10 +945,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
836
945
|
// does, as React Native queues a `TextInput`'s view commands. Without this
|
|
837
946
|
// the common `useEffect(() => ref.current?.focus(), [])` was a silent no-op.
|
|
838
947
|
const pendingCommands = useRef<Array<(native: NitroInputRef) => void>>([])
|
|
948
|
+
const [owner] = useState(() => ({}))
|
|
839
949
|
const hybridRef = useMemo(
|
|
840
950
|
() =>
|
|
841
951
|
callback((instance: NitroInputRef) => {
|
|
842
952
|
nativeRef.current = instance
|
|
953
|
+
viewOwner.set(instance as object, owner)
|
|
843
954
|
const queued = pendingCommands.current
|
|
844
955
|
pendingCommands.current = []
|
|
845
956
|
for (const command of queued) command(instance)
|
|
@@ -847,20 +958,55 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
847
958
|
}),
|
|
848
959
|
[]
|
|
849
960
|
)
|
|
961
|
+
// React's <Activity> hides a subtree by deleting its native views (they go
|
|
962
|
+
// back to the view pool, and another field may be given one) and creates
|
|
963
|
+
// new ones when it is shown again, running layout-effect cleanups on hide
|
|
964
|
+
// and the effects again on show. So on hide: keep the text for the view the
|
|
965
|
+
// field gets back (an uncontrolled field's text lives only natively), and
|
|
966
|
+
// let go of the old view, or a handle kept from before would read and
|
|
967
|
+
// focus whichever field is given it next. While hidden, focus() and blur()
|
|
968
|
+
// do nothing and text changes wait for the field to be shown.
|
|
969
|
+
const hiddenRef = useRef(false)
|
|
970
|
+
const hiddenViewRef = useRef<NitroInputRef | null>(null)
|
|
971
|
+
useLayoutEffect(() => {
|
|
972
|
+
hiddenRef.current = false
|
|
973
|
+
// Shown again. Android keeps a hidden field's view, and calls no
|
|
974
|
+
// `hybridRef` for it again: take it back, unless another field has been
|
|
975
|
+
// given it (iOS), in which case this field's new view calls `hybridRef`.
|
|
976
|
+
const previous = hiddenViewRef.current
|
|
977
|
+
hiddenViewRef.current = null
|
|
978
|
+
if (nativeRef.current == null && previous != null && viewOwner.get(previous as object) === owner) {
|
|
979
|
+
nativeRef.current = previous
|
|
980
|
+
const queued = pendingCommands.current
|
|
981
|
+
pendingCommands.current = []
|
|
982
|
+
for (const command of queued) command(previous)
|
|
983
|
+
}
|
|
984
|
+
return () => {
|
|
985
|
+
const native = nativeRef.current
|
|
986
|
+
if (native != null) textRef.current = native.currentText()
|
|
987
|
+
hiddenViewRef.current = native
|
|
988
|
+
nativeRef.current = null
|
|
989
|
+
hiddenRef.current = true
|
|
990
|
+
if (!controlledRef.current) setUncontrolledText(textRef.current)
|
|
991
|
+
}
|
|
992
|
+
}, [])
|
|
850
993
|
useEffect(() => {
|
|
851
994
|
if (textInputRegistry == null) return
|
|
852
995
|
const key = (hostRef.current as object | null) ?? { nitroInput: true }
|
|
853
996
|
registryKeyRef.current = key
|
|
854
997
|
patchRegistryOnce()
|
|
855
|
-
|
|
998
|
+
const field = {
|
|
856
999
|
focus: () => nativeRef.current?.focus(),
|
|
857
1000
|
blur: () => nativeRef.current?.blur(),
|
|
858
|
-
}
|
|
1001
|
+
}
|
|
1002
|
+
mountedFields.set(key, field)
|
|
1003
|
+
liveFields.add(field)
|
|
859
1004
|
textInputRegistry.registerInput(key)
|
|
860
1005
|
return () => {
|
|
861
1006
|
if (textInputRegistry.currentlyFocusedInput() === key) textInputRegistry.blurInput(key)
|
|
862
1007
|
textInputRegistry.unregisterInput(key)
|
|
863
1008
|
mountedFields.delete(key)
|
|
1009
|
+
liveFields.delete(field)
|
|
864
1010
|
registryKeyRef.current = null
|
|
865
1011
|
}
|
|
866
1012
|
}, [])
|
|
@@ -1029,9 +1175,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
1029
1175
|
if (native != null) command(native)
|
|
1030
1176
|
else pendingCommands.current.push(command)
|
|
1031
1177
|
}
|
|
1178
|
+
const unlessHidden = (command: (native: NitroInputRef) => void) => {
|
|
1179
|
+
if (!hiddenRef.current) send(command)
|
|
1180
|
+
}
|
|
1032
1181
|
return {
|
|
1033
|
-
focus: () =>
|
|
1034
|
-
blur: () =>
|
|
1182
|
+
focus: () => unlessHidden((native) => native.focus()),
|
|
1183
|
+
blur: () => unlessHidden((native) => native.blur()),
|
|
1035
1184
|
clear: () => send((native) => native.clear()),
|
|
1036
1185
|
setText: (text) => send((native) => native.replaceText(text)),
|
|
1037
1186
|
setValue: (next) => send((native) => native.setValue(next)),
|
|
@@ -1044,10 +1193,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
1044
1193
|
// registry this field already keeps up to date, which is the same place
|
|
1045
1194
|
// `TextInput.isFocused()` reads from.
|
|
1046
1195
|
isFocused: () =>
|
|
1196
|
+
hiddenRef.current ? false :
|
|
1047
1197
|
nativeRef.current?.isFocused() ??
|
|
1048
1198
|
(registryKeyRef.current != null &&
|
|
1049
1199
|
textInputRegistry?.currentlyFocusedInput() === registryKeyRef.current),
|
|
1050
|
-
setSelection: (start: number, end?: number) =>
|
|
1200
|
+
setSelection: (start: number, end?: number) =>
|
|
1201
|
+
unlessHidden((native) => native.setSelection(start, end ?? start)),
|
|
1051
1202
|
get native() {
|
|
1052
1203
|
return nativeRef.current
|
|
1053
1204
|
},
|
|
@@ -1122,6 +1273,24 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
1122
1273
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1123
1274
|
[notationsKey]
|
|
1124
1275
|
)
|
|
1276
|
+
// The same for the affinity formats and the character map.
|
|
1277
|
+
const affinityKey =
|
|
1278
|
+
maskAffinityFormats != null && maskAffinityFormats.length > 0 ? JSON.stringify(maskAffinityFormats) : ''
|
|
1279
|
+
const stableAffinityFormats = useMemo(
|
|
1280
|
+
() => (affinityKey !== '' && maskAffinityFormats ? [...maskAffinityFormats] : EMPTY_STRINGS),
|
|
1281
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1282
|
+
[affinityKey]
|
|
1283
|
+
)
|
|
1284
|
+
const characterMapKey =
|
|
1285
|
+
maskCharacterMap != null && Object.keys(maskCharacterMap).length > 0 ? JSON.stringify(maskCharacterMap) : ''
|
|
1286
|
+
const stableCharacterMap = useMemo<NitroInputCharacterMapping[]>(
|
|
1287
|
+
() =>
|
|
1288
|
+
characterMapKey !== '' && maskCharacterMap
|
|
1289
|
+
? Object.entries(maskCharacterMap).map(([from, to]) => ({ from, to }))
|
|
1290
|
+
: EMPTY_MAPPINGS,
|
|
1291
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1292
|
+
[characterMapKey]
|
|
1293
|
+
)
|
|
1125
1294
|
const resolvedFractionDigits = fractionDigits ?? derived?.fractionDigits ?? 2
|
|
1126
1295
|
const resolvedKeyboardType =
|
|
1127
1296
|
keyboardType ??
|
|
@@ -1220,8 +1389,10 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
1220
1389
|
ref={hostRef as React.Ref<never>}
|
|
1221
1390
|
style={[autoSize, style]}
|
|
1222
1391
|
hybridRef={hybridRef}
|
|
1223
|
-
text={value ??
|
|
1224
|
-
|
|
1392
|
+
text={value ?? uncontrolledText}
|
|
1393
|
+
// An uncontrolled field's `text` is its initial text: a count of 0 is
|
|
1394
|
+
// never caught up after the first edit, so native never puts it back.
|
|
1395
|
+
mostRecentEventCount={value != null ? eventCountRef.current : 0}
|
|
1225
1396
|
mode={resolvedMode}
|
|
1226
1397
|
plain={!reflows}
|
|
1227
1398
|
fractionDigits={resolvedFractionDigits}
|
|
@@ -1275,6 +1446,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
1275
1446
|
maskNotations={stableNotations}
|
|
1276
1447
|
maskAutocomplete={maskAutocomplete ?? true}
|
|
1277
1448
|
maskAutoSkip={maskAutoSkip ?? false}
|
|
1449
|
+
maskAffinityFormats={stableAffinityFormats}
|
|
1450
|
+
maskAffinityStrategy={maskAffinityStrategy ?? 'wholeString'}
|
|
1451
|
+
maskTextCase={maskTextCase ?? 'none'}
|
|
1452
|
+
maskCharacterMap={stableCharacterMap}
|
|
1453
|
+
maskSlotPlaceholder={maskSlotPlaceholder ?? (keepPlaceholder ? '_' : '')}
|
|
1454
|
+
keepPlaceholder={keepPlaceholder ?? false}
|
|
1278
1455
|
onChangeMask={onChangeMaskCallback}
|
|
1279
1456
|
keyboardType={resolvedKeyboardType}
|
|
1280
1457
|
returnKeyType={resolvedReturnKeyType}
|
|
@@ -1307,6 +1484,8 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
1307
1484
|
showSoftInputOnFocus={resolvedShowSoftInput}
|
|
1308
1485
|
selectTextOnFocus={selectTextOnFocus ?? false}
|
|
1309
1486
|
clearTextOnFocus={clearTextOnFocus ?? false}
|
|
1487
|
+
keyboardHandoffMs={keyboardHandoffMs ?? 0}
|
|
1488
|
+
returnKeyBar={returnKeyBar ?? true}
|
|
1310
1489
|
contextMenuHidden={contextMenuHidden ?? false}
|
|
1311
1490
|
spellCheck={spellCheck ?? autoCorrect ?? true}
|
|
1312
1491
|
selectionStart={selection?.start ?? -1}
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,24 @@ import type {
|
|
|
11
11
|
*/
|
|
12
12
|
export type NitroInputMode = 'text' | 'number' | 'mask'
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* How the best of `maskAffinityFormats` is chosen for the text in the field:
|
|
16
|
+
* `'wholeString'` the format that accepts the most characters and inserts the
|
|
17
|
+
* fewest, `'prefix'` the one whose output starts most like what was typed,
|
|
18
|
+
* `'capacity'` / `'extractedValueCapacity'` the fullest format that still
|
|
19
|
+
* holds all of the text / all of its value characters.
|
|
20
|
+
*/
|
|
21
|
+
export type NitroInputMaskAffinity = 'wholeString' | 'prefix' | 'capacity' | 'extractedValueCapacity'
|
|
22
|
+
|
|
23
|
+
/** How letters are cased as they are typed into a mask. */
|
|
24
|
+
export type NitroInputTextCase = 'none' | 'upper' | 'lower'
|
|
25
|
+
|
|
26
|
+
/** One character replaced by another before masking; an empty `to` drops it. */
|
|
27
|
+
export interface NitroInputCharacterMapping {
|
|
28
|
+
from: string
|
|
29
|
+
to: string
|
|
30
|
+
}
|
|
31
|
+
|
|
14
32
|
/**
|
|
15
33
|
* A caller-defined slot character for `mask`, beyond the built-in
|
|
16
34
|
* `0 9 A a _ - …`. `characterSet` lists every character the slot accepts.
|
|
@@ -175,6 +193,34 @@ export interface NitroInputProps extends HybridViewProps {
|
|
|
175
193
|
* Default: `false`.
|
|
176
194
|
*/
|
|
177
195
|
maskAutoSkip: boolean
|
|
196
|
+
/**
|
|
197
|
+
* `'mask'`: more patterns the text may take, e.g. a 4-6-5 Amex card beside
|
|
198
|
+
* a 4-4-4-4 `mask`. Every edit is masked with each and the best, by
|
|
199
|
+
* `maskAffinityStrategy`, is kept; `mask` wins ties. Default: none.
|
|
200
|
+
*/
|
|
201
|
+
maskAffinityFormats: string[]
|
|
202
|
+
/** `'mask'`: how the best of `maskAffinityFormats` is chosen. Default: `'wholeString'`. */
|
|
203
|
+
maskAffinityStrategy: NitroInputMaskAffinity
|
|
204
|
+
/** `'mask'`: uppercases or lowercases letters as they are typed or pasted. Default: `'none'`. */
|
|
205
|
+
maskTextCase: NitroInputTextCase
|
|
206
|
+
/**
|
|
207
|
+
* `'mask'`: characters replaced before masking, e.g. a Cyrillic "С" with a
|
|
208
|
+
* Latin "C", or "," with "." for a decimal mask. Default: none.
|
|
209
|
+
*/
|
|
210
|
+
maskCharacterMap: NitroInputCharacterMapping[]
|
|
211
|
+
/**
|
|
212
|
+
* `'mask'`: the character each empty slot shows in the kept placeholder and
|
|
213
|
+
* in `onChangeMask`'s tail, e.g. `'_'`. Empty: the slot's own notation
|
|
214
|
+
* character. Default: `''`.
|
|
215
|
+
*/
|
|
216
|
+
maskSlotPlaceholder: string
|
|
217
|
+
/**
|
|
218
|
+
* `'mask'`: keeps the rest of the placeholder visible after what has been
|
|
219
|
+
* typed, so "1234 5" shows "678 9012" greyed after it. Without a
|
|
220
|
+
* `placeholder`, the mask itself is shown, empty slots as
|
|
221
|
+
* `maskSlotPlaceholder`: `+1 (212) ___-____`. Default: `false`.
|
|
222
|
+
*/
|
|
223
|
+
keepPlaceholder: boolean
|
|
178
224
|
/** `'number'`: inserted between every three integer digits; empty disables grouping. Default: `','`. */
|
|
179
225
|
groupingSeparator: string
|
|
180
226
|
/** `'number'`: between the integer and fraction digits. Default: `'.'`. */
|
|
@@ -352,6 +398,21 @@ export interface NitroInputProps extends HybridViewProps {
|
|
|
352
398
|
selectTextOnFocus: boolean
|
|
353
399
|
/** Empty the field when it gains focus. Default: `false`. */
|
|
354
400
|
clearTextOnFocus: boolean
|
|
401
|
+
/**
|
|
402
|
+
* iOS: when this field loses its window while it has the keyboard (its
|
|
403
|
+
* screen is popped or unmounted), keep the keyboard up for this many
|
|
404
|
+
* milliseconds so the next field to focus takes it over in place, instead
|
|
405
|
+
* of the keyboard dropping and rising again. `0` lets it go at once.
|
|
406
|
+
* Default: `0`.
|
|
407
|
+
*/
|
|
408
|
+
keyboardHandoffMs: number
|
|
409
|
+
/**
|
|
410
|
+
* iOS: on the number, phone and decimal pads (no return key of their own),
|
|
411
|
+
* a bar over the keyboard with the `returnKeyType` key, as `TextInput`
|
|
412
|
+
* shows. `false` leaves the pad bare, for a screen with its own button on
|
|
413
|
+
* the keyboard. Default: `true`.
|
|
414
|
+
*/
|
|
415
|
+
returnKeyBar: boolean
|
|
355
416
|
/** Hides the Cut/Copy/Paste menu. Default: `false`. */
|
|
356
417
|
contextMenuHidden: boolean
|
|
357
418
|
/** Spell checking (`'text'` mode). Defaults to `autoCorrect`. */
|
|
@@ -441,6 +502,13 @@ export interface NitroInputMethods extends HybridViewMethods {
|
|
|
441
502
|
* `end` defaults to `start` for a plain caret move; both are clamped.
|
|
442
503
|
*/
|
|
443
504
|
setSelection(start: number, end: number): void
|
|
505
|
+
/**
|
|
506
|
+
* The field is about to be unmounted: if it has the keyboard and
|
|
507
|
+
* `keyboardHandoffMs` is set, hand the keyboard to the stand-in now, while
|
|
508
|
+
* the field is still first responder (by the time its view is removed,
|
|
509
|
+
* UIKit has already taken that away). Called by `NitroInput` itself.
|
|
510
|
+
*/
|
|
511
|
+
prepareForUnmount(): void
|
|
444
512
|
}
|
|
445
513
|
|
|
446
514
|
export type NitroInputView = HybridView<NitroInputProps, NitroInputMethods>
|