react-native-nitro-input 0.3.2 → 0.3.4

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.
Files changed (30) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/README.md +45 -0
  3. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +25 -4
  4. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +211 -3
  5. package/android/src/main/java/com/margelo/nitro/nitroinput/UnmountHandoffs.kt +57 -0
  6. package/ios/HybridNitroInputView.swift +19 -4
  7. package/ios/NitroInputCommands.mm +10 -0
  8. package/ios/NitroInputView.swift +585 -14
  9. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  10. package/lib/commonjs/src/NitroInput.js +103 -15
  11. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  12. package/lib/module/src/NitroInput.js +104 -16
  13. package/lib/typescript/src/NitroInput.d.ts +16 -0
  14. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +22 -0
  15. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +22 -0
  16. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +5 -0
  17. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +10 -0
  18. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +16 -0
  19. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +18 -0
  20. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +12 -0
  21. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +3 -0
  22. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +33 -0
  23. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +5 -0
  24. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +5 -0
  25. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +4 -0
  26. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +6 -0
  27. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  28. package/package.json +1 -1
  29. package/src/NitroInput.tsx +117 -11
  30. package/src/specs/NitroInput.nitro.ts +22 -0
@@ -637,6 +637,18 @@ namespace margelo::nitro::nitroinput {
637
637
  inline void setClearTextOnFocus(bool clearTextOnFocus) noexcept override {
638
638
  _swiftPart.setClearTextOnFocus(std::forward<decltype(clearTextOnFocus)>(clearTextOnFocus));
639
639
  }
640
+ inline double getKeyboardHandoffMs() noexcept override {
641
+ return _swiftPart.getKeyboardHandoffMs();
642
+ }
643
+ inline void setKeyboardHandoffMs(double keyboardHandoffMs) noexcept override {
644
+ _swiftPart.setKeyboardHandoffMs(std::forward<decltype(keyboardHandoffMs)>(keyboardHandoffMs));
645
+ }
646
+ inline bool getReturnKeyBar() noexcept override {
647
+ return _swiftPart.getReturnKeyBar();
648
+ }
649
+ inline void setReturnKeyBar(bool returnKeyBar) noexcept override {
650
+ _swiftPart.setReturnKeyBar(std::forward<decltype(returnKeyBar)>(returnKeyBar));
651
+ }
640
652
  inline bool getContextMenuHidden() noexcept override {
641
653
  return _swiftPart.getContextMenuHidden();
642
654
  }
@@ -841,6 +853,12 @@ namespace margelo::nitro::nitroinput {
841
853
  std::rethrow_exception(__result.error());
842
854
  }
843
855
  }
856
+ inline void prepareForUnmount() override {
857
+ auto __result = _swiftPart.prepareForUnmount();
858
+ if (__result.hasError()) [[unlikely]] {
859
+ std::rethrow_exception(__result.error());
860
+ }
861
+ }
844
862
 
845
863
  private:
846
864
  NitroInput::HybridNitroInputViewSpec_cxx _swiftPart;
@@ -584,6 +584,18 @@ using namespace margelo::nitro::nitroinput::views;
584
584
  : !newViewProps.clearTextOnFocus.hasSameValue(oldViewProps->clearTextOnFocus)) {
585
585
  swiftPart.setClearTextOnFocus(newViewProps.clearTextOnFocus.get());
586
586
  }
587
+ // keyboardHandoffMs: number
588
+ if (oldViewProps == nullptr
589
+ ? newViewProps.keyboardHandoffMs.isProvided()
590
+ : !newViewProps.keyboardHandoffMs.hasSameValue(oldViewProps->keyboardHandoffMs)) {
591
+ swiftPart.setKeyboardHandoffMs(newViewProps.keyboardHandoffMs.get());
592
+ }
593
+ // returnKeyBar: boolean
594
+ if (oldViewProps == nullptr
595
+ ? newViewProps.returnKeyBar.isProvided()
596
+ : !newViewProps.returnKeyBar.hasSameValue(oldViewProps->returnKeyBar)) {
597
+ swiftPart.setReturnKeyBar(newViewProps.returnKeyBar.get());
598
+ }
587
599
  // contextMenuHidden: boolean
588
600
  if (oldViewProps == nullptr
589
601
  ? newViewProps.contextMenuHidden.isProvided()
@@ -91,6 +91,8 @@ public protocol HybridNitroInputViewSpec_protocol: HybridObject, HybridView {
91
91
  var showSoftInputOnFocus: Bool { get set }
92
92
  var selectTextOnFocus: Bool { get set }
93
93
  var clearTextOnFocus: Bool { get set }
94
+ var keyboardHandoffMs: Double { get set }
95
+ var returnKeyBar: Bool { get set }
94
96
  var contextMenuHidden: Bool { get set }
95
97
  var spellCheck: Bool { get set }
96
98
  var selectionStart: Double { get set }
@@ -124,6 +126,7 @@ public protocol HybridNitroInputViewSpec_protocol: HybridObject, HybridView {
124
126
  func getValue() throws -> Double
125
127
  func isFocused() throws -> Bool
126
128
  func setSelection(start: Double, end: Double) throws -> Void
129
+ func prepareForUnmount() throws -> Void
127
130
  }
128
131
 
129
132
  public extension HybridNitroInputViewSpec_protocol {
@@ -1030,6 +1030,28 @@ open class HybridNitroInputViewSpec_cxx {
1030
1030
  }
1031
1031
  }
1032
1032
 
1033
+ public final var keyboardHandoffMs: Double {
1034
+ @inline(__always)
1035
+ get {
1036
+ return self.__implementation.keyboardHandoffMs
1037
+ }
1038
+ @inline(__always)
1039
+ set {
1040
+ self.__implementation.keyboardHandoffMs = newValue
1041
+ }
1042
+ }
1043
+
1044
+ public final var returnKeyBar: Bool {
1045
+ @inline(__always)
1046
+ get {
1047
+ return self.__implementation.returnKeyBar
1048
+ }
1049
+ @inline(__always)
1050
+ set {
1051
+ self.__implementation.returnKeyBar = newValue
1052
+ }
1053
+ }
1054
+
1033
1055
  public final var contextMenuHidden: Bool {
1034
1056
  @inline(__always)
1035
1057
  get {
@@ -1564,6 +1586,17 @@ open class HybridNitroInputViewSpec_cxx {
1564
1586
  }
1565
1587
  }
1566
1588
 
1589
+ @inline(__always)
1590
+ public final func prepareForUnmount() -> bridge.Result_void_ {
1591
+ do {
1592
+ try self.__implementation.prepareForUnmount()
1593
+ return bridge.create_Result_void_()
1594
+ } catch (let __error) {
1595
+ let __exceptionPtr = __error.toCpp()
1596
+ return bridge.create_Result_void_(__exceptionPtr)
1597
+ }
1598
+ }
1599
+
1567
1600
  public final func getView() -> UnsafeMutableRawPointer {
1568
1601
  return Unmanaged.passRetained(__implementation.view).toOpaque()
1569
1602
  }
@@ -176,6 +176,10 @@ namespace margelo::nitro::nitroinput {
176
176
  prototype.registerHybridSetter("selectTextOnFocus", &HybridNitroInputViewSpec::setSelectTextOnFocus);
177
177
  prototype.registerHybridGetter("clearTextOnFocus", &HybridNitroInputViewSpec::getClearTextOnFocus);
178
178
  prototype.registerHybridSetter("clearTextOnFocus", &HybridNitroInputViewSpec::setClearTextOnFocus);
179
+ prototype.registerHybridGetter("keyboardHandoffMs", &HybridNitroInputViewSpec::getKeyboardHandoffMs);
180
+ prototype.registerHybridSetter("keyboardHandoffMs", &HybridNitroInputViewSpec::setKeyboardHandoffMs);
181
+ prototype.registerHybridGetter("returnKeyBar", &HybridNitroInputViewSpec::getReturnKeyBar);
182
+ prototype.registerHybridSetter("returnKeyBar", &HybridNitroInputViewSpec::setReturnKeyBar);
179
183
  prototype.registerHybridGetter("contextMenuHidden", &HybridNitroInputViewSpec::getContextMenuHidden);
180
184
  prototype.registerHybridSetter("contextMenuHidden", &HybridNitroInputViewSpec::setContextMenuHidden);
181
185
  prototype.registerHybridGetter("spellCheck", &HybridNitroInputViewSpec::getSpellCheck);
@@ -229,6 +233,7 @@ namespace margelo::nitro::nitroinput {
229
233
  prototype.registerHybridMethod("getValue", &HybridNitroInputViewSpec::getValue);
230
234
  prototype.registerHybridMethod("isFocused", &HybridNitroInputViewSpec::isFocused);
231
235
  prototype.registerHybridMethod("setSelection", &HybridNitroInputViewSpec::setSelection);
236
+ prototype.registerHybridMethod("prepareForUnmount", &HybridNitroInputViewSpec::prepareForUnmount);
232
237
  });
233
238
  }
234
239
 
@@ -262,6 +262,10 @@ namespace margelo::nitro::nitroinput {
262
262
  virtual void setSelectTextOnFocus(bool selectTextOnFocus) = 0;
263
263
  virtual bool getClearTextOnFocus() = 0;
264
264
  virtual void setClearTextOnFocus(bool clearTextOnFocus) = 0;
265
+ virtual double getKeyboardHandoffMs() = 0;
266
+ virtual void setKeyboardHandoffMs(double keyboardHandoffMs) = 0;
267
+ virtual bool getReturnKeyBar() = 0;
268
+ virtual void setReturnKeyBar(bool returnKeyBar) = 0;
265
269
  virtual bool getContextMenuHidden() = 0;
266
270
  virtual void setContextMenuHidden(bool contextMenuHidden) = 0;
267
271
  virtual bool getSpellCheck() = 0;
@@ -318,6 +322,7 @@ namespace margelo::nitro::nitroinput {
318
322
  virtual double getValue() = 0;
319
323
  virtual bool isFocused() = 0;
320
324
  virtual void setSelection(double start, double end) = 0;
325
+ virtual void prepareForUnmount() = 0;
321
326
 
322
327
  protected:
323
328
  // Hybrid Setup
@@ -101,6 +101,8 @@ namespace margelo::nitro::nitroinput::views {
101
101
  showSoftInputOnFocus(nitro::ReactProp<bool>::fromRawValue("NitroInputView", "showSoftInputOnFocus", rawProps, sourceProps.showSoftInputOnFocus)),
102
102
  selectTextOnFocus(nitro::ReactProp<bool>::fromRawValue("NitroInputView", "selectTextOnFocus", rawProps, sourceProps.selectTextOnFocus)),
103
103
  clearTextOnFocus(nitro::ReactProp<bool>::fromRawValue("NitroInputView", "clearTextOnFocus", rawProps, sourceProps.clearTextOnFocus)),
104
+ keyboardHandoffMs(nitro::ReactProp<double>::fromRawValue("NitroInputView", "keyboardHandoffMs", rawProps, sourceProps.keyboardHandoffMs)),
105
+ returnKeyBar(nitro::ReactProp<bool>::fromRawValue("NitroInputView", "returnKeyBar", rawProps, sourceProps.returnKeyBar)),
104
106
  contextMenuHidden(nitro::ReactProp<bool>::fromRawValue("NitroInputView", "contextMenuHidden", rawProps, sourceProps.contextMenuHidden)),
105
107
  spellCheck(nitro::ReactProp<bool>::fromRawValue("NitroInputView", "spellCheck", rawProps, sourceProps.spellCheck)),
106
108
  selectionStart(nitro::ReactProp<double>::fromRawValue("NitroInputView", "selectionStart", rawProps, sourceProps.selectionStart)),
@@ -208,6 +210,8 @@ namespace margelo::nitro::nitroinput::views {
208
210
  case hashString("showSoftInputOnFocus"): return true;
209
211
  case hashString("selectTextOnFocus"): return true;
210
212
  case hashString("clearTextOnFocus"): return true;
213
+ case hashString("keyboardHandoffMs"): return true;
214
+ case hashString("returnKeyBar"): return true;
211
215
  case hashString("contextMenuHidden"): return true;
212
216
  case hashString("spellCheck"): return true;
213
217
  case hashString("selectionStart"): return true;
@@ -143,6 +143,8 @@ namespace margelo::nitro::nitroinput::views {
143
143
  nitro::ReactProp<bool> showSoftInputOnFocus;
144
144
  nitro::ReactProp<bool> selectTextOnFocus;
145
145
  nitro::ReactProp<bool> clearTextOnFocus;
146
+ nitro::ReactProp<double> keyboardHandoffMs;
147
+ nitro::ReactProp<bool> returnKeyBar;
146
148
  nitro::ReactProp<bool> contextMenuHidden;
147
149
  nitro::ReactProp<bool> spellCheck;
148
150
  nitro::ReactProp<double> selectionStart;
@@ -250,6 +252,8 @@ namespace margelo::nitro::nitroinput::views {
250
252
  showSoftInputOnFocus.hasSameValue(other.showSoftInputOnFocus) &&
251
253
  selectTextOnFocus.hasSameValue(other.selectTextOnFocus) &&
252
254
  clearTextOnFocus.hasSameValue(other.clearTextOnFocus) &&
255
+ keyboardHandoffMs.hasSameValue(other.keyboardHandoffMs) &&
256
+ returnKeyBar.hasSameValue(other.returnKeyBar) &&
253
257
  contextMenuHidden.hasSameValue(other.contextMenuHidden) &&
254
258
  spellCheck.hasSameValue(other.spellCheck) &&
255
259
  selectionStart.hasSameValue(other.selectionStart) &&
@@ -358,6 +362,8 @@ namespace margelo::nitro::nitroinput::views {
358
362
  showSoftInputOnFocus.isProvided() ||
359
363
  selectTextOnFocus.isProvided() ||
360
364
  clearTextOnFocus.isProvided() ||
365
+ keyboardHandoffMs.isProvided() ||
366
+ returnKeyBar.isProvided() ||
361
367
  contextMenuHidden.isProvided() ||
362
368
  spellCheck.isProvided() ||
363
369
  selectionStart.isProvided() ||
@@ -85,6 +85,8 @@
85
85
  "showSoftInputOnFocus": true,
86
86
  "selectTextOnFocus": true,
87
87
  "clearTextOnFocus": true,
88
+ "keyboardHandoffMs": true,
89
+ "returnKeyBar": true,
88
90
  "contextMenuHidden": true,
89
91
  "spellCheck": true,
90
92
  "selectionStart": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-input",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
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",
@@ -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,
@@ -160,6 +161,20 @@ const EMPTY_MAPPINGS: NitroInputCharacterMapping[] = []
160
161
 
161
162
  /** Mounted fields, by the host instance React Native's registry stores. */
162
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>()
163
178
  let patchedRegistry = false
164
179
 
165
180
  /**
@@ -195,7 +210,12 @@ function patchRegistryOnce() {
195
210
  registry.blurTextInput = (input: unknown) => {
196
211
  const field = input != null ? mountedFields.get(input as object) : undefined
197
212
  if (field) return field.blur()
198
- return originalBlur.call(registry, input)
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
199
219
  }
200
220
  }
201
221
  }
@@ -546,6 +566,22 @@ export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus'
546
566
  selectTextOnFocus?: boolean
547
567
  /** Empty the field when it gains focus. Default: `false`. */
548
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
549
585
  /** Hides the Cut/Copy/Paste menu. Default: `false`. */
550
586
  contextMenuHidden?: boolean
551
587
  /** Spell checking in `'text'` mode. Defaults to `autoCorrect`. */
@@ -769,6 +805,8 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
769
805
  showSoftInputOnFocus,
770
806
  selectTextOnFocus,
771
807
  clearTextOnFocus,
808
+ keyboardHandoffMs,
809
+ returnKeyBar,
772
810
  contextMenuHidden,
773
811
  spellCheck,
774
812
  readOnly,
@@ -792,6 +830,28 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
792
830
  ref
793
831
  ) {
794
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
+ )
795
855
  // What React Native's registry stores for this field. The React host
796
856
  // instance when there is one (iOS), otherwise a stable per-instance token:
797
857
  // on Android the Nitro host component hands out no instance, and the
@@ -873,9 +933,9 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
873
933
  const [, setEventCountState] = useState(0)
874
934
  const controlledRef = useRef(value != null)
875
935
  controlledRef.current = value != null
876
- // The initial text of an uncontrolled field never changes afterwards, so
877
- // native applies it exactly once.
878
- const [initialText] = useState(() => defaultValue ?? '')
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 ?? '')
879
939
 
880
940
  // Nitro callbacks must be wrapped with `callback()` and the wrapper object
881
941
  // has to be referentially stable, otherwise every render re-sets the prop.
@@ -885,10 +945,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
885
945
  // does, as React Native queues a `TextInput`'s view commands. Without this
886
946
  // the common `useEffect(() => ref.current?.focus(), [])` was a silent no-op.
887
947
  const pendingCommands = useRef<Array<(native: NitroInputRef) => void>>([])
948
+ const [owner] = useState(() => ({}))
888
949
  const hybridRef = useMemo(
889
950
  () =>
890
951
  callback((instance: NitroInputRef) => {
891
952
  nativeRef.current = instance
953
+ viewOwner.set(instance as object, owner)
892
954
  const queued = pendingCommands.current
893
955
  pendingCommands.current = []
894
956
  for (const command of queued) command(instance)
@@ -896,20 +958,55 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
896
958
  }),
897
959
  []
898
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
+ }, [])
899
993
  useEffect(() => {
900
994
  if (textInputRegistry == null) return
901
995
  const key = (hostRef.current as object | null) ?? { nitroInput: true }
902
996
  registryKeyRef.current = key
903
997
  patchRegistryOnce()
904
- mountedFields.set(key, {
998
+ const field = {
905
999
  focus: () => nativeRef.current?.focus(),
906
1000
  blur: () => nativeRef.current?.blur(),
907
- })
1001
+ }
1002
+ mountedFields.set(key, field)
1003
+ liveFields.add(field)
908
1004
  textInputRegistry.registerInput(key)
909
1005
  return () => {
910
1006
  if (textInputRegistry.currentlyFocusedInput() === key) textInputRegistry.blurInput(key)
911
1007
  textInputRegistry.unregisterInput(key)
912
1008
  mountedFields.delete(key)
1009
+ liveFields.delete(field)
913
1010
  registryKeyRef.current = null
914
1011
  }
915
1012
  }, [])
@@ -1078,9 +1175,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
1078
1175
  if (native != null) command(native)
1079
1176
  else pendingCommands.current.push(command)
1080
1177
  }
1178
+ const unlessHidden = (command: (native: NitroInputRef) => void) => {
1179
+ if (!hiddenRef.current) send(command)
1180
+ }
1081
1181
  return {
1082
- focus: () => send((native) => native.focus()),
1083
- blur: () => send((native) => native.blur()),
1182
+ focus: () => unlessHidden((native) => native.focus()),
1183
+ blur: () => unlessHidden((native) => native.blur()),
1084
1184
  clear: () => send((native) => native.clear()),
1085
1185
  setText: (text) => send((native) => native.replaceText(text)),
1086
1186
  setValue: (next) => send((native) => native.setValue(next)),
@@ -1093,10 +1193,12 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
1093
1193
  // registry this field already keeps up to date, which is the same place
1094
1194
  // `TextInput.isFocused()` reads from.
1095
1195
  isFocused: () =>
1196
+ hiddenRef.current ? false :
1096
1197
  nativeRef.current?.isFocused() ??
1097
1198
  (registryKeyRef.current != null &&
1098
1199
  textInputRegistry?.currentlyFocusedInput() === registryKeyRef.current),
1099
- setSelection: (start: number, end?: number) => send((native) => native.setSelection(start, end ?? start)),
1200
+ setSelection: (start: number, end?: number) =>
1201
+ unlessHidden((native) => native.setSelection(start, end ?? start)),
1100
1202
  get native() {
1101
1203
  return nativeRef.current
1102
1204
  },
@@ -1287,8 +1389,10 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
1287
1389
  ref={hostRef as React.Ref<never>}
1288
1390
  style={[autoSize, style]}
1289
1391
  hybridRef={hybridRef}
1290
- text={value ?? initialText}
1291
- mostRecentEventCount={eventCountRef.current}
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}
1292
1396
  mode={resolvedMode}
1293
1397
  plain={!reflows}
1294
1398
  fractionDigits={resolvedFractionDigits}
@@ -1380,6 +1484,8 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
1380
1484
  showSoftInputOnFocus={resolvedShowSoftInput}
1381
1485
  selectTextOnFocus={selectTextOnFocus ?? false}
1382
1486
  clearTextOnFocus={clearTextOnFocus ?? false}
1487
+ keyboardHandoffMs={keyboardHandoffMs ?? 0}
1488
+ returnKeyBar={returnKeyBar ?? true}
1383
1489
  contextMenuHidden={contextMenuHidden ?? false}
1384
1490
  spellCheck={spellCheck ?? autoCorrect ?? true}
1385
1491
  selectionStart={selection?.start ?? -1}
@@ -398,6 +398,21 @@ export interface NitroInputProps extends HybridViewProps {
398
398
  selectTextOnFocus: boolean
399
399
  /** Empty the field when it gains focus. Default: `false`. */
400
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
401
416
  /** Hides the Cut/Copy/Paste menu. Default: `false`. */
402
417
  contextMenuHidden: boolean
403
418
  /** Spell checking (`'text'` mode). Defaults to `autoCorrect`. */
@@ -487,6 +502,13 @@ export interface NitroInputMethods extends HybridViewMethods {
487
502
  * `end` defaults to `start` for a plain caret move; both are clamped.
488
503
  */
489
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
490
512
  }
491
513
 
492
514
  export type NitroInputView = HybridView<NitroInputProps, NitroInputMethods>