react-native-nitro-input 0.3.4 → 0.3.6
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 +32 -0
- package/README.md +28 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +6 -0
- package/ios/HybridNitroInputView.swift +9 -1
- package/ios/NitroInputView.swift +65 -3
- package/lib/commonjs/src/NitroInput.js +19 -8
- package/lib/commonjs/src/worklets.js +7 -3
- package/lib/module/src/NitroInput.js +19 -8
- package/lib/module/src/worklets.js +7 -3
- package/lib/typescript/src/NitroInput.d.ts +17 -0
- package/lib/typescript/src/worklets.d.ts +3 -1
- package/package.json +1 -1
- package/src/NitroInput.tsx +40 -11
- package/src/worklets.ts +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.6
|
|
4
|
+
|
|
5
|
+
- **A controlled `selection` rendered before the latest keystrokes is
|
|
6
|
+
dropped**, as `value` already was and as React Native's `TextInput` does.
|
|
7
|
+
Typed faster than JS renders (a slow Android phone, a busy JS thread), a
|
|
8
|
+
caret computed for an earlier text landed in a field that already had more
|
|
9
|
+
in it, and the next characters went in behind the first ones: a code field
|
|
10
|
+
driven by `selection` took "123456" as "156234". Uncontrolled fields are
|
|
11
|
+
unchanged.
|
|
12
|
+
|
|
13
|
+
## 0.3.5
|
|
14
|
+
|
|
15
|
+
- **`focusedValue`**: pass a shared value (Reanimated's `useSharedValue(false)`)
|
|
16
|
+
and the field keeps it equal to its focus, set on the UI thread the moment
|
|
17
|
+
focus changes, while `onFocus` / `onBlur` still run on JS. For focus styling
|
|
18
|
+
that must not wait for JS and that wraps more than the field: a border
|
|
19
|
+
around a row holding an icon or a country picker in front and a clear
|
|
20
|
+
button behind. Needs `react-native-worklets`.
|
|
21
|
+
- **Worklet focus handlers see the first focus of an `autoFocus` field.** They
|
|
22
|
+
were registered in an effect, which can run after the view has attached and
|
|
23
|
+
taken focus, so a worklet `onFocus` (and `useNitroInputState`'s `focused`)
|
|
24
|
+
missed it. They are registered while rendering now.
|
|
25
|
+
- **iOS: no keyboard left over a screen popped back to.** Going back from a
|
|
26
|
+
screen whose field had the keyboard, with `keyboardHandoffMs`, to a screen
|
|
27
|
+
where no field takes it (a list whose search field was never focused), the
|
|
28
|
+
keyboard stayed up over that screen for the whole `keyboardHandoffMs` after
|
|
29
|
+
the pop. React unmounts the popped screen's field before the pop starts, so
|
|
30
|
+
the handoff could not tell a pop from a field swapped in place. The hold now
|
|
31
|
+
ends as the screen starts to leave when no field is waiting for the
|
|
32
|
+
keyboard; a field there that had it when it was covered, or one focused on
|
|
33
|
+
return, still takes it over.
|
|
34
|
+
|
|
3
35
|
## 0.3.4
|
|
4
36
|
|
|
5
37
|
- **iOS: a focused field that took no typing** (with
|
package/README.md
CHANGED
|
@@ -357,6 +357,33 @@ function Field() {
|
|
|
357
357
|
values; `field.handlers` are the worklets that keep them current. Typing in
|
|
358
358
|
that field runs nothing on the JS thread and re-renders nothing.
|
|
359
359
|
|
|
360
|
+
#### Focus styling around a field and what sits beside it
|
|
361
|
+
|
|
362
|
+
`field.handlers` replace your JS `onFocus` / `onBlur`. When you only need the
|
|
363
|
+
focus state, and keep your handlers, pass a shared value as `focusedValue`: the
|
|
364
|
+
field sets it on the UI thread the moment focus changes, `autoFocus` included,
|
|
365
|
+
and `onFocus` / `onBlur` still run. The row that draws the border can then hold
|
|
366
|
+
anything, the way a form field often has an icon or a country picker in front
|
|
367
|
+
and a clear button behind:
|
|
368
|
+
|
|
369
|
+
```tsx
|
|
370
|
+
function SearchField({ onFocus }: { onFocus: () => void }) {
|
|
371
|
+
const focused = useSharedValue(false)
|
|
372
|
+
const ring = useAnimatedStyle(() => ({
|
|
373
|
+
borderColor: focused.value ? '#2563eb' : 'transparent',
|
|
374
|
+
}))
|
|
375
|
+
return (
|
|
376
|
+
<Animated.View style={[styles.row, ring]}>
|
|
377
|
+
<SearchIcon />
|
|
378
|
+
<NitroInput style={{ flex: 1 }} focusedValue={focused} onFocus={onFocus} />
|
|
379
|
+
<ClearButton />
|
|
380
|
+
</Animated.View>
|
|
381
|
+
)
|
|
382
|
+
}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
The border changes in the frame the caret appears, with no JS round trip.
|
|
386
|
+
|
|
360
387
|
A worklet can only reach what it closes over, unless worklets run in
|
|
361
388
|
[Bundle Mode](https://docs.swmansion.com/react-native-worklets/docs/bundleMode/),
|
|
362
389
|
which gives them the whole bundle. That is what lets a `transform` use a real
|
|
@@ -549,6 +576,7 @@ screen behaves like theirs with these settings:
|
|
|
549
576
|
| `onChangeMask` | `(formatted, extracted, tail, complete) => void` | – | `mask` mode: the formatted text, the characters the user contributed, what is still missing, and whether every mandatory slot is filled. |
|
|
550
577
|
| `signPlacement` | `'beforeAffix' \| 'afterAffix'` | `'beforeAffix'` | Where a negative amount's sign sits relative to `prefix`: `-$1,234.56` or `$-1,234.56`. Reflow only — a plain field's affixes are accessory views outside the text. |
|
|
551
578
|
| `onFocus` / `onBlur` | `(event) => void` | – | Carries `text`, `eventCount` and `target`. |
|
|
579
|
+
| `focusedValue` | `SharedValue<boolean>` | – | Kept equal to the field's focus on the UI thread, while `onFocus` / `onBlur` still run. Needs `react-native-worklets`. |
|
|
552
580
|
| `onSubmitEditing` | `(event) => void` | – | Return key pressed; what happens next is `submitBehavior`. |
|
|
553
581
|
| `onEndEditing` | `(event) => void` | – | Editing finished. |
|
|
554
582
|
| `onSelectionChange` | `(event) => void` | – | The caret or selection moved, in code points. |
|
|
@@ -532,12 +532,18 @@ class HybridNitroInputView(private val context: ThemedReactContext) : HybridNitr
|
|
|
532
532
|
}
|
|
533
533
|
|
|
534
534
|
/// A controlled `selection`; -1 on either end means "leave the caret alone".
|
|
535
|
+
/// Like `text`, dropped when it was rendered before JS saw the latest native
|
|
536
|
+
/// edits (React Native's `TextInput` does the same): typed faster than JS
|
|
537
|
+
/// renders, a caret computed for "1" landed in a field already showing
|
|
538
|
+
/// "1234", and the next digits went in behind the first one. An uncontrolled
|
|
539
|
+
/// field sends a count of 0 and is applied as before.
|
|
535
540
|
private fun flushSelectionIfNeeded() {
|
|
536
541
|
if (!selectionDirty) return
|
|
537
542
|
selectionDirty = false
|
|
538
543
|
val start = clampInt(selectionStart, -1, Int.MAX_VALUE, -1)
|
|
539
544
|
val end = clampInt(selectionEnd, -1, Int.MAX_VALUE, -1)
|
|
540
545
|
if (start < 0 || end < 0) return
|
|
546
|
+
if (mostRecentEventCount > 0 && mostRecentEventCount < eventCount) return
|
|
541
547
|
inputView.setSelection(start, end)
|
|
542
548
|
}
|
|
543
549
|
|
|
@@ -434,13 +434,21 @@ final class HybridNitroInputView: HybridNitroInputViewSpec, RecyclableView {
|
|
|
434
434
|
}
|
|
435
435
|
|
|
436
436
|
/// A controlled `selection`: `-1` on either end means "leave the caret alone",
|
|
437
|
-
/// which is what an uncontrolled field sends on every render.
|
|
437
|
+
/// which is what an uncontrolled field sends on every render. Like `text`,
|
|
438
|
+
/// dropped when it was rendered before JS saw the latest native edits (React
|
|
439
|
+
/// Native's `TextInput` does the same): typed faster than JS renders, a caret
|
|
440
|
+
/// computed for "1" landed in a field already showing "1234". An
|
|
441
|
+
/// uncontrolled field sends a count of 0 and is applied as before.
|
|
438
442
|
private func applySelectionIfNeeded() {
|
|
439
443
|
guard selectionDirty else { return }
|
|
440
444
|
selectionDirty = false
|
|
441
445
|
let start = Self.clampInt(selectionStart, -1, Int(Int32.max), fallback: -1)
|
|
442
446
|
let end = Self.clampInt(selectionEnd, -1, Int(Int32.max), fallback: -1)
|
|
443
447
|
guard start >= 0, end >= 0 else { return }
|
|
448
|
+
snapshotLock.lock()
|
|
449
|
+
let nativeCount = snapshot.eventCount
|
|
450
|
+
snapshotLock.unlock()
|
|
451
|
+
if mostRecentEventCount.isFinite, mostRecentEventCount > 0, Int(mostRecentEventCount) < nativeCount { return }
|
|
444
452
|
inputView.setSelection(start: start, end: end)
|
|
445
453
|
}
|
|
446
454
|
|
package/ios/NitroInputView.swift
CHANGED
|
@@ -633,6 +633,8 @@ final class NitroInputView: UIView {
|
|
|
633
633
|
/// Set while a NitroInput (or the keyboard handoff's stand-in) takes first
|
|
634
634
|
/// responder, which must never be refused.
|
|
635
635
|
fileprivate static var claimInProgress = false
|
|
636
|
+
/// Every live field, to ask whether one is about to take the keyboard.
|
|
637
|
+
private static let liveFields = NSHashTable<NitroInputView>.weakObjects()
|
|
636
638
|
private var everInWindow = false
|
|
637
639
|
/// When the field last stopped editing while on screen (media time; 0 = never).
|
|
638
640
|
private var endedEditingAt: CFTimeInterval = 0
|
|
@@ -705,6 +707,7 @@ final class NitroInputView: UIView {
|
|
|
705
707
|
super.init(frame: frame)
|
|
706
708
|
// Starts it tracking the keyboard before any field needs to ask.
|
|
707
709
|
_ = KeyboardHandoff.shared
|
|
710
|
+
Self.liveFields.add(self)
|
|
708
711
|
isOpaque = false
|
|
709
712
|
backgroundColor = .clear
|
|
710
713
|
clipsToBounds = false
|
|
@@ -914,7 +917,61 @@ final class NitroInputView: UIView {
|
|
|
914
917
|
func handOffKeyboardIfFocused() {
|
|
915
918
|
guard traits.keyboardHandoffMs > 0, traits.showSoftInputOnFocus,
|
|
916
919
|
let window, editor.isFirstResponder else { return }
|
|
917
|
-
KeyboardHandoff.shared.hold(like: editor, in: window, for: traits.keyboardHandoffMs)
|
|
920
|
+
let hold = KeyboardHandoff.shared.hold(like: editor, in: window, for: traits.keyboardHandoffMs)
|
|
921
|
+
if let hold, let host = hostViewController {
|
|
922
|
+
Self.letGo(hold, ifLeaving: host, until: CACurrentMediaTime() + traits.keyboardHandoffMs / 1000)
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/// Ends a hold as soon as the leaving field's screen turns out to be going
|
|
927
|
+
/// itself (back, a sheet closing) with no field waiting to take the
|
|
928
|
+
/// keyboard: held on, it only stays up over a screen that has no use for it
|
|
929
|
+
/// until the time runs out. React unmounts a popped screen's fields before
|
|
930
|
+
/// react-native-screens starts the pop, so at the handoff a pop still looks
|
|
931
|
+
/// like a view swapped in place; the screen starts leaving a moment later.
|
|
932
|
+
/// A field on the screen underneath that takes the keyboard back ends the
|
|
933
|
+
/// hold itself.
|
|
934
|
+
private static func letGo(_ hold: Int, ifLeaving host: UIViewController, until deadline: CFTimeInterval) {
|
|
935
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) { [weak host] in
|
|
936
|
+
let handoff = KeyboardHandoff.shared
|
|
937
|
+
guard let host, handoff.isHolding, handoff.currentHold == hold else { return }
|
|
938
|
+
if isLeaving(host) {
|
|
939
|
+
// Once more a moment later: the screen underneath comes back into the
|
|
940
|
+
// window as the pop starts, and a field there that had the keyboard
|
|
941
|
+
// asks for it back only then.
|
|
942
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
|
|
943
|
+
guard handoff.isHolding, handoff.currentHold == hold, !fieldWaitsForKeyboard() else { return }
|
|
944
|
+
handoff.letGo()
|
|
945
|
+
}
|
|
946
|
+
return
|
|
947
|
+
}
|
|
948
|
+
if CACurrentMediaTime() < deadline { letGo(hold, ifLeaving: host, until: deadline) }
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/// `controller`'s screen is being popped or dismissed. react-native-screens
|
|
953
|
+
/// takes a popped screen out of its navigation controller as the pop
|
|
954
|
+
/// starts, while its views are still on screen: a controller in no
|
|
955
|
+
/// container, presented by nothing and not the window's root is on its way
|
|
956
|
+
/// out.
|
|
957
|
+
private static func isLeaving(_ controller: UIViewController) -> Bool {
|
|
958
|
+
var current: UIViewController? = controller
|
|
959
|
+
while let each = current {
|
|
960
|
+
if each.isMovingFromParent || each.isBeingDismissed { return true }
|
|
961
|
+
if each.parent == nil, each.presentingViewController == nil,
|
|
962
|
+
each.viewIfLoaded?.window?.rootViewController !== each { return true }
|
|
963
|
+
current = each.parent
|
|
964
|
+
}
|
|
965
|
+
return false
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/// A field is about to take first responder: a return waiting for the
|
|
969
|
+
/// transition to end, or a focus() held until its view is back in a window.
|
|
970
|
+
private static func fieldWaitsForKeyboard() -> Bool {
|
|
971
|
+
let now = CACurrentMediaTime()
|
|
972
|
+
return liveFields.allObjects.contains { field in
|
|
973
|
+
(field.focusAfterTransition && field.window != nil) || field.pendingFocusUntil > now
|
|
974
|
+
}
|
|
918
975
|
}
|
|
919
976
|
|
|
920
977
|
/// `becomeFirstResponder`, retried next turn if UIKit declines because the
|
|
@@ -3287,6 +3344,8 @@ final class KeyboardHandoff {
|
|
|
3287
3344
|
private var holder: UITextField?
|
|
3288
3345
|
private var release: DispatchWorkItem?
|
|
3289
3346
|
private var endObserver: NSObjectProtocol?
|
|
3347
|
+
/// Which hold is running, so a check on an older one leaves this one alone.
|
|
3348
|
+
private(set) var currentHold = 0
|
|
3290
3349
|
/// Whether the system keyboard is up or on its way up.
|
|
3291
3350
|
private(set) var keyboardVisible = false
|
|
3292
3351
|
|
|
@@ -3304,7 +3363,8 @@ final class KeyboardHandoff {
|
|
|
3304
3363
|
|
|
3305
3364
|
/// Takes the keyboard from `editor` (the leaving field's system field) for up
|
|
3306
3365
|
/// to `milliseconds`.
|
|
3307
|
-
|
|
3366
|
+
@discardableResult
|
|
3367
|
+
func hold(like editor: UIView, in window: UIWindow, for milliseconds: Double) -> Int? {
|
|
3308
3368
|
letGo()
|
|
3309
3369
|
let box = UIView(frame: CGRect(x: -1000, y: -1000, width: 1, height: 1))
|
|
3310
3370
|
box.tag = -1
|
|
@@ -3339,8 +3399,9 @@ final class KeyboardHandoff {
|
|
|
3339
3399
|
NitroInputView.claimInProgress = false
|
|
3340
3400
|
guard held else {
|
|
3341
3401
|
box.removeFromSuperview()
|
|
3342
|
-
return
|
|
3402
|
+
return nil
|
|
3343
3403
|
}
|
|
3404
|
+
currentHold += 1
|
|
3344
3405
|
container = box
|
|
3345
3406
|
holder = field
|
|
3346
3407
|
// Another field took the keyboard: the hold is over.
|
|
@@ -3353,6 +3414,7 @@ final class KeyboardHandoff {
|
|
|
3353
3414
|
let item = DispatchWorkItem { [weak self] in self?.letGo() }
|
|
3354
3415
|
release = item
|
|
3355
3416
|
DispatchQueue.main.asyncAfter(deadline: .now() + milliseconds / 1000, execute: item)
|
|
3417
|
+
return currentHold
|
|
3356
3418
|
}
|
|
3357
3419
|
|
|
3358
3420
|
/// Nothing claimed the keyboard in time, or `Keyboard.dismiss()`: let it hide.
|
|
@@ -198,7 +198,7 @@ function warnIncompatible(what) {
|
|
|
198
198
|
* Like a `TextInput`, it takes its width from its parent; `autoWidth` sizes
|
|
199
199
|
* it to its content instead, and is on by default when the text reflows.
|
|
200
200
|
*/
|
|
201
|
-
exports.NitroInput = (0, react_1.forwardRef)(function NitroInput({ value, defaultValue, mode, fractionDigits, maxIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, prefixFontSize, suffixFontSize, affixAlign, signPlacement, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, format, placeholder, placeholderTextColor, duration, easing, bounce, effect, fontSize, lineHeight, fontWeight, fontFamily, color, textAlign, cursorColor, selectionColor, caretHidden, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, variant, label, labelBehavior, labelColor, labelFocusedColor, labelFontSize, strokeColor, focusedStrokeColor, strokeWidth, cornerRadius, fillColor, keyboardType, inputMode, enterKeyHint, mask, maskNotations, maskAutocomplete, maskAutoSkip, maskAffinityFormats, maskAffinityStrategy, maskTextCase, maskCharacterMap, maskSlotPlaceholder, keepPlaceholder, returnKeyType, autoCapitalize, autoCorrect, editable, multiline, numberOfLines, rows, textAlignVertical, scrollEnabled, autoFocus, transition, autoWidth: autoWidthProp, submitBehavior, blurOnSubmit, secureTextEntry, keyboardAppearance, textContentType, autoComplete, enablesReturnKeyAutomatically, showSoftInputOnFocus, selectTextOnFocus, clearTextOnFocus, keyboardHandoffMs, returnKeyBar, contextMenuHidden, spellCheck, readOnly, selection, onEndEditing, onSelectionChange, onKeyPress, maxLength, transform, onChangeText, onChange, onChangeValue, onChangeMask, onFocus, onBlur, onSubmitEditing, onNativeRef, style, ...viewProps }, ref) {
|
|
201
|
+
exports.NitroInput = (0, react_1.forwardRef)(function NitroInput({ value, defaultValue, mode, fractionDigits, maxIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, prefixFontSize, suffixFontSize, affixAlign, signPlacement, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, format, placeholder, placeholderTextColor, duration, easing, bounce, effect, fontSize, lineHeight, fontWeight, fontFamily, color, textAlign, cursorColor, selectionColor, caretHidden, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, variant, label, labelBehavior, labelColor, labelFocusedColor, labelFontSize, strokeColor, focusedStrokeColor, strokeWidth, cornerRadius, fillColor, keyboardType, inputMode, enterKeyHint, mask, maskNotations, maskAutocomplete, maskAutoSkip, maskAffinityFormats, maskAffinityStrategy, maskTextCase, maskCharacterMap, maskSlotPlaceholder, keepPlaceholder, returnKeyType, autoCapitalize, autoCorrect, editable, multiline, numberOfLines, rows, textAlignVertical, scrollEnabled, autoFocus, transition, autoWidth: autoWidthProp, submitBehavior, blurOnSubmit, secureTextEntry, keyboardAppearance, textContentType, autoComplete, enablesReturnKeyAutomatically, showSoftInputOnFocus, selectTextOnFocus, clearTextOnFocus, keyboardHandoffMs, returnKeyBar, contextMenuHidden, spellCheck, readOnly, selection, onEndEditing, onSelectionChange, onKeyPress, maxLength, transform, onChangeText, onChange, onChangeValue, onChangeMask, onFocus, onBlur, focusedValue, onSubmitEditing, onNativeRef, style, ...viewProps }, ref) {
|
|
202
202
|
const nativeRef = (0, react_1.useRef)(null);
|
|
203
203
|
// `keyboardHandoffMs`: hand the keyboard off while this field still has it.
|
|
204
204
|
// A layout-effect cleanup runs in React's commit, so the native call is
|
|
@@ -270,7 +270,7 @@ exports.NitroInput = (0, react_1.forwardRef)(function NitroInput({ value, defaul
|
|
|
270
270
|
const onChangeValueId = useWorkletId(onChangeValueWorklet, worklets_1.registerCallback);
|
|
271
271
|
// Marking any of these `'worklet'` moves it to the UI thread; the JS
|
|
272
272
|
// handler is then skipped, exactly as `onChangeText` already worked.
|
|
273
|
-
const onFocusId =
|
|
273
|
+
const onFocusId = useFocusWorkletId((0, worklets_1.isWorklet)(onFocus) ? onFocus : undefined, (0, worklets_1.isWorklet)(onBlur) ? onBlur : undefined, focusedValue);
|
|
274
274
|
const onSelectionChangeId = useWorkletId((0, worklets_1.isWorklet)(onSelectionChange) ? onSelectionChange : undefined, worklets_1.registerSelectionChange);
|
|
275
275
|
const onSubmitEditingId = useWorkletId((0, worklets_1.isWorklet)(onSubmitEditing) ? onSubmitEditing : undefined, worklets_1.registerTextEvent);
|
|
276
276
|
const onEndEditingId = useWorkletId((0, worklets_1.isWorklet)(onEndEditing) ? onEndEditing : undefined, worklets_1.registerTextEvent);
|
|
@@ -680,16 +680,27 @@ function useWorkletId(fn, register) {
|
|
|
680
680
|
return id;
|
|
681
681
|
}
|
|
682
682
|
/**
|
|
683
|
-
* The same, for
|
|
684
|
-
*
|
|
683
|
+
* The same, for focus: native reports one focus change, so the two handlers
|
|
684
|
+
* and `focusedValue` share an id and the wrapper picks between them.
|
|
685
|
+
*
|
|
686
|
+
* Registered while rendering, not in an effect: an `autoFocus` field takes
|
|
687
|
+
* focus as its view attaches, which can come before this component's effects
|
|
688
|
+
* run, and a focus change for an id that is not registered yet is dropped -
|
|
689
|
+
* `focusedValue` would miss the field's first focus. The registration is
|
|
690
|
+
* synchronous on the UI runtime, so it is there before the view is.
|
|
685
691
|
*/
|
|
686
|
-
function
|
|
687
|
-
const id = (0, react_1.useMemo)(() =>
|
|
692
|
+
function useFocusWorkletId(onFocus, onBlur, focusedValue) {
|
|
693
|
+
const id = (0, react_1.useMemo)(() => {
|
|
694
|
+
if (!onFocus && !onBlur && !focusedValue)
|
|
695
|
+
return 0;
|
|
696
|
+
const allocated = (0, worklets_1.allocateWorkletId)();
|
|
697
|
+
(0, worklets_1.registerFocusChange)(onFocus, onBlur, allocated, focusedValue);
|
|
698
|
+
return allocated;
|
|
699
|
+
}, [onFocus, onBlur, focusedValue]);
|
|
688
700
|
(0, react_1.useEffect)(() => {
|
|
689
701
|
if (id === 0)
|
|
690
702
|
return;
|
|
691
|
-
register(onFocus, onBlur, id);
|
|
692
703
|
return () => (0, worklets_1.unregisterWorklet)(id);
|
|
693
|
-
}, [id
|
|
704
|
+
}, [id]);
|
|
694
705
|
return id;
|
|
695
706
|
}
|
|
@@ -153,15 +153,19 @@ function registerCallback(callback, id) {
|
|
|
153
153
|
* The event is the one the JS handlers get, minus `target`: a react tag is a
|
|
154
154
|
* JS-thread notion and there is nothing on the UI runtime to ask for it.
|
|
155
155
|
*/
|
|
156
|
-
function registerFocusChange(onFocus, onBlur, id) {
|
|
156
|
+
function registerFocusChange(onFocus, onBlur, id, focused) {
|
|
157
157
|
if (id === 0 || !ensureWorkletsInstalled())
|
|
158
158
|
return;
|
|
159
159
|
const worklets = loadWorklets();
|
|
160
160
|
worklets.executeOnUIRuntimeSync(() => {
|
|
161
161
|
'worklet';
|
|
162
|
-
const wrapper = (
|
|
163
|
-
const event = { text, target: 0, eventCount: 0, nativeEvent: { text, target: 0, eventCount: 0 } };
|
|
162
|
+
const wrapper = (isFocused, text) => {
|
|
164
163
|
if (focused)
|
|
164
|
+
focused.value = isFocused;
|
|
165
|
+
if (!onFocus && !onBlur)
|
|
166
|
+
return;
|
|
167
|
+
const event = { text, target: 0, eventCount: 0, nativeEvent: { text, target: 0, eventCount: 0 } };
|
|
168
|
+
if (isFocused)
|
|
165
169
|
onFocus?.(event);
|
|
166
170
|
else
|
|
167
171
|
onBlur?.(event);
|
|
@@ -159,7 +159,7 @@ function warnIncompatible(what) {
|
|
|
159
159
|
* Like a `TextInput`, it takes its width from its parent; `autoWidth` sizes
|
|
160
160
|
* it to its content instead, and is on by default when the text reflows.
|
|
161
161
|
*/
|
|
162
|
-
export const NitroInput = forwardRef(function NitroInput({ value, defaultValue, mode, fractionDigits, maxIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, prefixFontSize, suffixFontSize, affixAlign, signPlacement, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, format, placeholder, placeholderTextColor, duration, easing, bounce, effect, fontSize, lineHeight, fontWeight, fontFamily, color, textAlign, cursorColor, selectionColor, caretHidden, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, variant, label, labelBehavior, labelColor, labelFocusedColor, labelFontSize, strokeColor, focusedStrokeColor, strokeWidth, cornerRadius, fillColor, keyboardType, inputMode, enterKeyHint, mask, maskNotations, maskAutocomplete, maskAutoSkip, maskAffinityFormats, maskAffinityStrategy, maskTextCase, maskCharacterMap, maskSlotPlaceholder, keepPlaceholder, returnKeyType, autoCapitalize, autoCorrect, editable, multiline, numberOfLines, rows, textAlignVertical, scrollEnabled, autoFocus, transition, autoWidth: autoWidthProp, submitBehavior, blurOnSubmit, secureTextEntry, keyboardAppearance, textContentType, autoComplete, enablesReturnKeyAutomatically, showSoftInputOnFocus, selectTextOnFocus, clearTextOnFocus, keyboardHandoffMs, returnKeyBar, contextMenuHidden, spellCheck, readOnly, selection, onEndEditing, onSelectionChange, onKeyPress, maxLength, transform, onChangeText, onChange, onChangeValue, onChangeMask, onFocus, onBlur, onSubmitEditing, onNativeRef, style, ...viewProps }, ref) {
|
|
162
|
+
export const NitroInput = forwardRef(function NitroInput({ value, defaultValue, mode, fractionDigits, maxIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, prefixFontSize, suffixFontSize, affixAlign, signPlacement, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, format, placeholder, placeholderTextColor, duration, easing, bounce, effect, fontSize, lineHeight, fontWeight, fontFamily, color, textAlign, cursorColor, selectionColor, caretHidden, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, variant, label, labelBehavior, labelColor, labelFocusedColor, labelFontSize, strokeColor, focusedStrokeColor, strokeWidth, cornerRadius, fillColor, keyboardType, inputMode, enterKeyHint, mask, maskNotations, maskAutocomplete, maskAutoSkip, maskAffinityFormats, maskAffinityStrategy, maskTextCase, maskCharacterMap, maskSlotPlaceholder, keepPlaceholder, returnKeyType, autoCapitalize, autoCorrect, editable, multiline, numberOfLines, rows, textAlignVertical, scrollEnabled, autoFocus, transition, autoWidth: autoWidthProp, submitBehavior, blurOnSubmit, secureTextEntry, keyboardAppearance, textContentType, autoComplete, enablesReturnKeyAutomatically, showSoftInputOnFocus, selectTextOnFocus, clearTextOnFocus, keyboardHandoffMs, returnKeyBar, contextMenuHidden, spellCheck, readOnly, selection, onEndEditing, onSelectionChange, onKeyPress, maxLength, transform, onChangeText, onChange, onChangeValue, onChangeMask, onFocus, onBlur, focusedValue, onSubmitEditing, onNativeRef, style, ...viewProps }, ref) {
|
|
163
163
|
const nativeRef = useRef(null);
|
|
164
164
|
// `keyboardHandoffMs`: hand the keyboard off while this field still has it.
|
|
165
165
|
// A layout-effect cleanup runs in React's commit, so the native call is
|
|
@@ -231,7 +231,7 @@ export const NitroInput = forwardRef(function NitroInput({ value, defaultValue,
|
|
|
231
231
|
const onChangeValueId = useWorkletId(onChangeValueWorklet, registerCallback);
|
|
232
232
|
// Marking any of these `'worklet'` moves it to the UI thread; the JS
|
|
233
233
|
// handler is then skipped, exactly as `onChangeText` already worked.
|
|
234
|
-
const onFocusId =
|
|
234
|
+
const onFocusId = useFocusWorkletId(isWorklet(onFocus) ? onFocus : undefined, isWorklet(onBlur) ? onBlur : undefined, focusedValue);
|
|
235
235
|
const onSelectionChangeId = useWorkletId(isWorklet(onSelectionChange) ? onSelectionChange : undefined, registerSelectionChange);
|
|
236
236
|
const onSubmitEditingId = useWorkletId(isWorklet(onSubmitEditing) ? onSubmitEditing : undefined, registerTextEvent);
|
|
237
237
|
const onEndEditingId = useWorkletId(isWorklet(onEndEditing) ? onEndEditing : undefined, registerTextEvent);
|
|
@@ -641,16 +641,27 @@ function useWorkletId(fn, register) {
|
|
|
641
641
|
return id;
|
|
642
642
|
}
|
|
643
643
|
/**
|
|
644
|
-
* The same, for
|
|
645
|
-
*
|
|
644
|
+
* The same, for focus: native reports one focus change, so the two handlers
|
|
645
|
+
* and `focusedValue` share an id and the wrapper picks between them.
|
|
646
|
+
*
|
|
647
|
+
* Registered while rendering, not in an effect: an `autoFocus` field takes
|
|
648
|
+
* focus as its view attaches, which can come before this component's effects
|
|
649
|
+
* run, and a focus change for an id that is not registered yet is dropped -
|
|
650
|
+
* `focusedValue` would miss the field's first focus. The registration is
|
|
651
|
+
* synchronous on the UI runtime, so it is there before the view is.
|
|
646
652
|
*/
|
|
647
|
-
function
|
|
648
|
-
const id = useMemo(() =>
|
|
653
|
+
function useFocusWorkletId(onFocus, onBlur, focusedValue) {
|
|
654
|
+
const id = useMemo(() => {
|
|
655
|
+
if (!onFocus && !onBlur && !focusedValue)
|
|
656
|
+
return 0;
|
|
657
|
+
const allocated = allocateWorkletId();
|
|
658
|
+
registerFocusChange(onFocus, onBlur, allocated, focusedValue);
|
|
659
|
+
return allocated;
|
|
660
|
+
}, [onFocus, onBlur, focusedValue]);
|
|
649
661
|
useEffect(() => {
|
|
650
662
|
if (id === 0)
|
|
651
663
|
return;
|
|
652
|
-
register(onFocus, onBlur, id);
|
|
653
664
|
return () => unregisterWorklet(id);
|
|
654
|
-
}, [id
|
|
665
|
+
}, [id]);
|
|
655
666
|
return id;
|
|
656
667
|
}
|
|
@@ -141,15 +141,19 @@ export function registerCallback(callback, id) {
|
|
|
141
141
|
* The event is the one the JS handlers get, minus `target`: a react tag is a
|
|
142
142
|
* JS-thread notion and there is nothing on the UI runtime to ask for it.
|
|
143
143
|
*/
|
|
144
|
-
export function registerFocusChange(onFocus, onBlur, id) {
|
|
144
|
+
export function registerFocusChange(onFocus, onBlur, id, focused) {
|
|
145
145
|
if (id === 0 || !ensureWorkletsInstalled())
|
|
146
146
|
return;
|
|
147
147
|
const worklets = loadWorklets();
|
|
148
148
|
worklets.executeOnUIRuntimeSync(() => {
|
|
149
149
|
'worklet';
|
|
150
|
-
const wrapper = (
|
|
151
|
-
const event = { text, target: 0, eventCount: 0, nativeEvent: { text, target: 0, eventCount: 0 } };
|
|
150
|
+
const wrapper = (isFocused, text) => {
|
|
152
151
|
if (focused)
|
|
152
|
+
focused.value = isFocused;
|
|
153
|
+
if (!onFocus && !onBlur)
|
|
154
|
+
return;
|
|
155
|
+
const event = { text, target: 0, eventCount: 0, nativeEvent: { text, target: 0, eventCount: 0 } };
|
|
156
|
+
if (isFocused)
|
|
153
157
|
onFocus?.(event);
|
|
154
158
|
else
|
|
155
159
|
onBlur?.(event);
|
|
@@ -3,6 +3,7 @@ import { type ColorValue, type TextStyle, type ViewProps } from 'react-native';
|
|
|
3
3
|
import { type HybridRef } from 'react-native-nitro-modules';
|
|
4
4
|
import type { NitroInputAffixAlign, NitroInputAutoCapitalize, NitroInputEasing, NitroInputEffect, NitroInputKeyboardAppearance, NitroInputKeyboardType, NitroInputLabelBehavior, NitroInputMethods, NitroInputMode, NitroInputProps as NativeNitroInputProps, NitroInputReturnKeyType, NitroInputSubmitBehavior, NitroInputTextAlign, NitroInputTextAlignVertical, NitroInputSignPlacement, NitroInputVariant, NitroInputNotation, NitroInputMaskAffinity, NitroInputTextCase } from './specs/NitroInput.nitro';
|
|
5
5
|
import { type NitroInputSelection, type NitroInputTransform } from './worklets';
|
|
6
|
+
import type { SharedValueLike } from './useNitroInputState';
|
|
6
7
|
import type { NumberFormat } from './NumberFormat';
|
|
7
8
|
/** React Native's `inputMode`, the HTML-aligned alias for `keyboardType`. */
|
|
8
9
|
export type InputMode = 'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url';
|
|
@@ -439,6 +440,22 @@ export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus'
|
|
|
439
440
|
* `({ text }) => …` reads better than `(e) => e.nativeEvent.text`.
|
|
440
441
|
*/
|
|
441
442
|
onFocus?: (event: NitroInputFocusEvent) => void;
|
|
443
|
+
/**
|
|
444
|
+
* A shared value (Reanimated's `useSharedValue(false)`) the field keeps equal
|
|
445
|
+
* to whether it has focus, set on the UI thread the moment focus changes.
|
|
446
|
+
* `onFocus` / `onBlur` still run as usual. For focus styling that must not
|
|
447
|
+
* wait for JS: a border around the field and whatever sits beside it, an
|
|
448
|
+
* icon's tint, a label's colour. Needs `react-native-worklets`.
|
|
449
|
+
*
|
|
450
|
+
* ```tsx
|
|
451
|
+
* const focused = useSharedValue(false)
|
|
452
|
+
* const ring = useAnimatedStyle(() => ({ borderColor: focused.value ? '#2563eb' : 'transparent' }))
|
|
453
|
+
* <Animated.View style={[styles.row, ring]}>
|
|
454
|
+
* <Icon /><NitroInput focusedValue={focused} onFocus={track} /><ClearButton />
|
|
455
|
+
* </Animated.View>
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
focusedValue?: SharedValueLike<boolean>;
|
|
442
459
|
/** Blurred. Same event as {@link NitroInputProps.onFocus}. */
|
|
443
460
|
onBlur?: (event: NitroInputFocusEvent) => void;
|
|
444
461
|
/** The return key was pressed. */
|
|
@@ -103,7 +103,9 @@ export declare function registerCallback(callback: (arg: never) => void, id: num
|
|
|
103
103
|
* The event is the one the JS handlers get, minus `target`: a react tag is a
|
|
104
104
|
* JS-thread notion and there is nothing on the UI runtime to ask for it.
|
|
105
105
|
*/
|
|
106
|
-
export declare function registerFocusChange(onFocus: ((event: WorkletFocusEvent) => void) | undefined, onBlur: ((event: WorkletFocusEvent) => void) | undefined, id: number
|
|
106
|
+
export declare function registerFocusChange(onFocus: ((event: WorkletFocusEvent) => void) | undefined, onBlur: ((event: WorkletFocusEvent) => void) | undefined, id: number, focused?: {
|
|
107
|
+
value: boolean;
|
|
108
|
+
}): void;
|
|
107
109
|
/** Registers an `onSelectionChange` worklet, given the two scalars native sends. */
|
|
108
110
|
export declare function registerSelectionChange(handler: (event: WorkletSelectionEvent) => void, id: number): void;
|
|
109
111
|
/** Registers an `onSubmitEditing` / `onEndEditing` worklet, given the text. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-input",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Native text for React Native, built with Nitro: a text and amount input with native formatting, masks and a Torph-style reflow, and an animated number (odometer, SwiftUI-style numeric text, ticker) whose digits roll or swap natively, with currency layouts and animated currency switches, shrink-to-fit, a loading shimmer and a jackpot reveal.",
|
|
5
5
|
"main": "lib/commonjs/src/index.js",
|
|
6
6
|
"module": "lib/module/src/index.js",
|
package/src/NitroInput.tsx
CHANGED
|
@@ -54,7 +54,9 @@ import {
|
|
|
54
54
|
unregisterWorklet,
|
|
55
55
|
type NitroInputSelection,
|
|
56
56
|
type NitroInputTransform,
|
|
57
|
+
type WorkletFocusEvent,
|
|
57
58
|
} from './worklets'
|
|
59
|
+
import type { SharedValueLike } from './useNitroInputState'
|
|
58
60
|
import { toNumericWeight, toProcessedColor } from './styleHelpers'
|
|
59
61
|
import { formatProps } from './formatProps'
|
|
60
62
|
import type { NumberFormat } from './NumberFormat'
|
|
@@ -644,6 +646,22 @@ export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus'
|
|
|
644
646
|
* `({ text }) => …` reads better than `(e) => e.nativeEvent.text`.
|
|
645
647
|
*/
|
|
646
648
|
onFocus?: (event: NitroInputFocusEvent) => void
|
|
649
|
+
/**
|
|
650
|
+
* A shared value (Reanimated's `useSharedValue(false)`) the field keeps equal
|
|
651
|
+
* to whether it has focus, set on the UI thread the moment focus changes.
|
|
652
|
+
* `onFocus` / `onBlur` still run as usual. For focus styling that must not
|
|
653
|
+
* wait for JS: a border around the field and whatever sits beside it, an
|
|
654
|
+
* icon's tint, a label's colour. Needs `react-native-worklets`.
|
|
655
|
+
*
|
|
656
|
+
* ```tsx
|
|
657
|
+
* const focused = useSharedValue(false)
|
|
658
|
+
* const ring = useAnimatedStyle(() => ({ borderColor: focused.value ? '#2563eb' : 'transparent' }))
|
|
659
|
+
* <Animated.View style={[styles.row, ring]}>
|
|
660
|
+
* <Icon /><NitroInput focusedValue={focused} onFocus={track} /><ClearButton />
|
|
661
|
+
* </Animated.View>
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
focusedValue?: SharedValueLike<boolean>
|
|
647
665
|
/** Blurred. Same event as {@link NitroInputProps.onFocus}. */
|
|
648
666
|
onBlur?: (event: NitroInputFocusEvent) => void
|
|
649
667
|
/** The return key was pressed. */
|
|
@@ -822,6 +840,7 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
822
840
|
onChangeMask,
|
|
823
841
|
onFocus,
|
|
824
842
|
onBlur,
|
|
843
|
+
focusedValue,
|
|
825
844
|
onSubmitEditing,
|
|
826
845
|
onNativeRef,
|
|
827
846
|
style,
|
|
@@ -902,10 +921,10 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
|
|
|
902
921
|
const onChangeValueId = useWorkletId(onChangeValueWorklet, registerCallback)
|
|
903
922
|
// Marking any of these `'worklet'` moves it to the UI thread; the JS
|
|
904
923
|
// handler is then skipped, exactly as `onChangeText` already worked.
|
|
905
|
-
const onFocusId =
|
|
924
|
+
const onFocusId = useFocusWorkletId(
|
|
906
925
|
isWorklet(onFocus) ? (onFocus as never) : undefined,
|
|
907
926
|
isWorklet(onBlur) ? (onBlur as never) : undefined,
|
|
908
|
-
|
|
927
|
+
focusedValue
|
|
909
928
|
)
|
|
910
929
|
const onSelectionChangeId = useWorkletId(
|
|
911
930
|
isWorklet(onSelectionChange) ? (onSelectionChange as never) : undefined,
|
|
@@ -1537,19 +1556,29 @@ function useWorkletId<T extends (...args: never[]) => unknown>(
|
|
|
1537
1556
|
}
|
|
1538
1557
|
|
|
1539
1558
|
/**
|
|
1540
|
-
* The same, for
|
|
1541
|
-
*
|
|
1559
|
+
* The same, for focus: native reports one focus change, so the two handlers
|
|
1560
|
+
* and `focusedValue` share an id and the wrapper picks between them.
|
|
1561
|
+
*
|
|
1562
|
+
* Registered while rendering, not in an effect: an `autoFocus` field takes
|
|
1563
|
+
* focus as its view attaches, which can come before this component's effects
|
|
1564
|
+
* run, and a focus change for an id that is not registered yet is dropped -
|
|
1565
|
+
* `focusedValue` would miss the field's first focus. The registration is
|
|
1566
|
+
* synchronous on the UI runtime, so it is there before the view is.
|
|
1542
1567
|
*/
|
|
1543
|
-
function
|
|
1544
|
-
onFocus:
|
|
1545
|
-
onBlur:
|
|
1546
|
-
|
|
1568
|
+
function useFocusWorkletId(
|
|
1569
|
+
onFocus: ((event: WorkletFocusEvent) => void) | undefined,
|
|
1570
|
+
onBlur: ((event: WorkletFocusEvent) => void) | undefined,
|
|
1571
|
+
focusedValue: SharedValueLike<boolean> | undefined
|
|
1547
1572
|
): number {
|
|
1548
|
-
const id = useMemo(() =>
|
|
1573
|
+
const id = useMemo(() => {
|
|
1574
|
+
if (!onFocus && !onBlur && !focusedValue) return 0
|
|
1575
|
+
const allocated = allocateWorkletId()
|
|
1576
|
+
registerFocusChange(onFocus, onBlur, allocated, focusedValue)
|
|
1577
|
+
return allocated
|
|
1578
|
+
}, [onFocus, onBlur, focusedValue])
|
|
1549
1579
|
useEffect(() => {
|
|
1550
1580
|
if (id === 0) return
|
|
1551
|
-
register(onFocus, onBlur, id)
|
|
1552
1581
|
return () => unregisterWorklet(id)
|
|
1553
|
-
}, [id
|
|
1582
|
+
}, [id])
|
|
1554
1583
|
return id
|
|
1555
1584
|
}
|
package/src/worklets.ts
CHANGED
|
@@ -227,15 +227,18 @@ export function registerCallback(callback: (arg: never) => void, id: number): vo
|
|
|
227
227
|
export function registerFocusChange(
|
|
228
228
|
onFocus: ((event: WorkletFocusEvent) => void) | undefined,
|
|
229
229
|
onBlur: ((event: WorkletFocusEvent) => void) | undefined,
|
|
230
|
-
id: number
|
|
230
|
+
id: number,
|
|
231
|
+
focused?: { value: boolean }
|
|
231
232
|
): void {
|
|
232
233
|
if (id === 0 || !ensureWorkletsInstalled()) return
|
|
233
234
|
const worklets = loadWorklets()!
|
|
234
235
|
worklets.executeOnUIRuntimeSync(() => {
|
|
235
236
|
'worklet'
|
|
236
|
-
const wrapper = (
|
|
237
|
+
const wrapper = (isFocused: boolean, text: string) => {
|
|
238
|
+
if (focused) focused.value = isFocused
|
|
239
|
+
if (!onFocus && !onBlur) return
|
|
237
240
|
const event = { text, target: 0, eventCount: 0, nativeEvent: { text, target: 0, eventCount: 0 } }
|
|
238
|
-
if (
|
|
241
|
+
if (isFocused) onFocus?.(event)
|
|
239
242
|
else onBlur?.(event)
|
|
240
243
|
}
|
|
241
244
|
globalThis.__nitroInputWorklets?.set(id, wrapper as (...args: never[]) => unknown)
|