react-native-nitro-input 0.3.7 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -0
- package/README.md +9 -4
- package/android/CMakeLists.txt +1 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputMeasure.kt +52 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +74 -80
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +18 -18
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +14 -14
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +199 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +398 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +112 -333
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
- package/cpp/RollingEngine.cpp +68 -5
- package/cpp/RollingEngine.hpp +13 -0
- package/cpp/numberformat/FormatFigure.cpp +134 -0
- package/cpp/numberformat/FormatFigure.hpp +65 -0
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +32 -10
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +9 -1
- package/ios/HybridNitroInputView.swift +69 -75
- package/ios/HybridNitroNumberView.swift +18 -18
- package/ios/HybridNitroTextView.swift +14 -14
- package/ios/InputSizing.swift +534 -0
- package/ios/NitroInputView.swift +102 -198
- package/ios/NitroNumberView.swift +98 -23
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/lib/commonjs/src/NitroInput.js +164 -23
- package/lib/commonjs/src/NitroNumber.js +19 -11
- package/lib/commonjs/src/NitroText.js +6 -6
- package/lib/commonjs/src/NumberFormat.js +20 -0
- package/lib/commonjs/src/formatProps.js +57 -81
- package/lib/commonjs/src/specs/NitroInputMeasure.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/lib/module/src/NitroInput.js +165 -24
- package/lib/module/src/NitroNumber.js +20 -12
- package/lib/module/src/NitroText.js +6 -6
- package/lib/module/src/NumberFormat.js +18 -0
- package/lib/module/src/formatProps.js +54 -81
- package/lib/module/src/specs/NitroInputMeasure.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +9 -16
- package/lib/typescript/src/NitroNumber.d.ts +25 -7
- package/lib/typescript/src/NumberFormat.d.ts +15 -1
- package/lib/typescript/src/formatProps.d.ts +23 -18
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +17 -8
- package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +67 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +6 -6
- package/lib/typescript/src/specs/NitroText.nitro.d.ts +6 -6
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +33 -0
- package/nitro.json +10 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +2 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +16 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.cpp +93 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.hpp +63 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +76 -40
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +24 -16
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +30 -30
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -12
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +30 -30
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +12 -12
- package/nitrogen/generated/android/c++/JNitroInputSize.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +284 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +44 -24
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +18 -18
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +18 -18
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputMeasureSpec.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +32 -8
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +6 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +6 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSize.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +246 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +29 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +11 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.hpp +112 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +56 -32
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +24 -24
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +24 -24
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +56 -32
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +24 -24
- package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +24 -24
- package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec_cxx.swift +138 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +12 -8
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +68 -24
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -6
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +18 -18
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +6 -6
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +18 -18
- package/nitrogen/generated/ios/swift/NitroInputSize.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputSizing.swift +242 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +24 -16
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +24 -16
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +8 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +12 -12
- package/nitrogen/generated/shared/c++/NitroInputSize.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +263 -0
- package/nitrogen/generated/shared/c++/NumberFigure.hpp +95 -0
- package/nitrogen/generated/shared/c++/NumberFormatLayout.hpp +112 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +24 -16
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +36 -24
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -12
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -18
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +12 -12
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +18 -18
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/package.json +5 -3
- package/src/NitroInput.tsx +201 -64
- package/src/NitroNumber.tsx +54 -25
- package/src/NitroText.tsx +6 -6
- package/src/NumberFormat.ts +22 -0
- package/src/formatProps.ts +59 -99
- package/src/specs/NitroInput.nitro.ts +17 -8
- package/src/specs/NitroInputMeasure.nitro.ts +76 -0
- package/src/specs/NitroNumber.nitro.ts +6 -6
- package/src/specs/NitroText.nitro.ts +6 -6
- package/src/specs/NumberFormat.nitro.ts +35 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
2
2
|
import { I18nManager, StyleSheet, } from 'react-native';
|
|
3
|
-
import { callback, getHostComponent, } from 'react-native-nitro-modules';
|
|
3
|
+
import { callback, getHostComponent, NitroModules, } from 'react-native-nitro-modules';
|
|
4
4
|
import NitroInputViewConfig from '../nitrogen/generated/shared/json/NitroInputViewConfig.json';
|
|
5
5
|
import { allocateWorkletId, isWorklet, registerFocusChange, registerKeyPress, registerSelectionChange, registerTextEvent, registerCallback, registerTransform, unregisterWorklet, } from './worklets';
|
|
6
6
|
import { toNumericWeight, toProcessedColor } from './styleHelpers';
|
|
@@ -145,21 +145,75 @@ function warnIncompatible(what) {
|
|
|
145
145
|
'field is drawn by the system view, and an amount, a mask and their ' +
|
|
146
146
|
'affixes are single-line. Drop one of the two.');
|
|
147
147
|
}
|
|
148
|
+
const NO_INSETS = { left: 0, top: 0, right: 0, bottom: 0 };
|
|
148
149
|
/**
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* ones slide or fade in and removed ones leave alongside their neighbours.
|
|
155
|
-
* In `'number'` mode every edit is formatted on the native side before a
|
|
156
|
-
* frame is drawn, with the caret kept in place, so there is never an
|
|
157
|
-
* unformatted frame and never a round trip through JS.
|
|
158
|
-
*
|
|
159
|
-
* Like a `TextInput`, it takes its width from its parent; `autoWidth` sizes
|
|
160
|
-
* it to its content instead, and is on by default when the text reflows.
|
|
150
|
+
* The style's padding plus border per edge, resolved the way Yoga does: a
|
|
151
|
+
* start / end edge before left / right, then horizontal / vertical, then all.
|
|
152
|
+
* A size measured for the text gets them added, as `TextInput`'s is, so a
|
|
153
|
+
* padded field grows rather than squeezing its text. Percentages are not
|
|
154
|
+
* resolved here (they need the parent's size) and count as 0.
|
|
161
155
|
*/
|
|
162
|
-
|
|
156
|
+
function styleInsets(flat, rightToLeft) {
|
|
157
|
+
if (flat == null)
|
|
158
|
+
return NO_INSETS;
|
|
159
|
+
const n = (key) => {
|
|
160
|
+
const value = flat[key];
|
|
161
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
162
|
+
};
|
|
163
|
+
const start = rightToLeft ? 'End' : 'Start';
|
|
164
|
+
const end = rightToLeft ? 'Start' : 'End';
|
|
165
|
+
const insets = {
|
|
166
|
+
left: (n(`padding${start}`) ?? n('paddingLeft') ?? n('paddingHorizontal') ?? n('padding') ?? 0) +
|
|
167
|
+
(n(`border${start}Width`) ?? n('borderLeftWidth') ?? n('borderWidth') ?? 0),
|
|
168
|
+
right: (n(`padding${end}`) ?? n('paddingRight') ?? n('paddingHorizontal') ?? n('padding') ?? 0) +
|
|
169
|
+
(n(`border${end}Width`) ?? n('borderRightWidth') ?? n('borderWidth') ?? 0),
|
|
170
|
+
top: (n('paddingTop') ?? n('paddingVertical') ?? n('padding') ?? 0) + (n('borderTopWidth') ?? n('borderWidth') ?? 0),
|
|
171
|
+
bottom: (n('paddingBottom') ?? n('paddingVertical') ?? n('padding') ?? 0) +
|
|
172
|
+
(n('borderBottomWidth') ?? n('borderWidth') ?? 0),
|
|
173
|
+
};
|
|
174
|
+
return insets.left === 0 && insets.right === 0 && insets.top === 0 && insets.bottom === 0 ? NO_INSETS : insets;
|
|
175
|
+
}
|
|
176
|
+
function styleTextAlign(value, rightToLeft) {
|
|
177
|
+
switch (value) {
|
|
178
|
+
case undefined:
|
|
179
|
+
return undefined;
|
|
180
|
+
// `justify` has no meaning on one line: the start edge, as on a TextInput.
|
|
181
|
+
case 'start':
|
|
182
|
+
case 'justify':
|
|
183
|
+
return 'auto';
|
|
184
|
+
case 'end':
|
|
185
|
+
return rightToLeft ? 'left' : 'right';
|
|
186
|
+
default:
|
|
187
|
+
return value;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function styleTextAlignVertical(value) {
|
|
191
|
+
return value ?? undefined;
|
|
192
|
+
}
|
|
193
|
+
// One per app, made on first use. `null` when the native side has none (an
|
|
194
|
+
// app built against an older version, or a test without native modules): the
|
|
195
|
+
// field then takes its size from the view's first report, as it used to.
|
|
196
|
+
let measurer;
|
|
197
|
+
function measureSizing(sizing) {
|
|
198
|
+
if (measurer === undefined) {
|
|
199
|
+
try {
|
|
200
|
+
const made = NitroModules.createHybridObject('NitroInputMeasure');
|
|
201
|
+
measurer = typeof made?.measure === 'function' ? made : null;
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
measurer = null;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (measurer === null)
|
|
208
|
+
return null;
|
|
209
|
+
try {
|
|
210
|
+
return measurer.measure(sizing);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export const NitroInput = forwardRef(function NitroInput({ value, defaultValue, mode, fractionDigits, maxIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, prefixFontSize, suffixFontSize, affixAlign, signPlacement, prefixAlign, suffixAlign, letterSpacing: letterSpacingProp, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, format, placeholder, placeholderTextColor, duration, easing, bounce, effect, fontSize: fontSizeProp, lineHeight: lineHeightProp, fontWeight: fontWeightProp, fontFamily: fontFamilyProp, color: colorProp, textAlign: textAlignProp, 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: textAlignVerticalProp, 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
217
|
const nativeRef = useRef(null);
|
|
164
218
|
// `keyboardHandoffMs`: hand the keyboard off while this field still has it.
|
|
165
219
|
// A layout-effect cleanup runs in React's commit, so the native call is
|
|
@@ -222,6 +276,11 @@ export const NitroInput = forwardRef(function NitroInput({ value, defaultValue,
|
|
|
222
276
|
if (value != null)
|
|
223
277
|
textRef.current = value;
|
|
224
278
|
const [size, setSize] = useState(null);
|
|
279
|
+
// The size measured before the view existed (see `measureSizing`), and
|
|
280
|
+
// whether the view has reported its own since.
|
|
281
|
+
const seedRef = useRef(null);
|
|
282
|
+
const seedKeyRef = useRef('');
|
|
283
|
+
const reportedRef = useRef(false);
|
|
225
284
|
// Worklets are registered on the UI runtime and referenced by id; a
|
|
226
285
|
// handler marked 'worklet' runs there instead of on the JS thread.
|
|
227
286
|
const transformId = useWorkletId(transform, registerTransform);
|
|
@@ -327,11 +386,24 @@ export const NitroInput = forwardRef(function NitroInput({ value, defaultValue,
|
|
|
327
386
|
// A field with a width of its own (the usual case) never needs a React
|
|
328
387
|
// commit per keystroke: only the height (font-dependent) is taken from native.
|
|
329
388
|
const flat = useMemo(() => StyleSheet.flatten(style), [style]);
|
|
389
|
+
// `TextInput` takes its typography from `style`; so does this, under the
|
|
390
|
+
// props of the same name, which win. (Fabric hands a view its style's keys
|
|
391
|
+
// as props too; the view's own are named so that none can be one of them -
|
|
392
|
+
// see PropNameCollisions.test.)
|
|
393
|
+
const fontSize = fontSizeProp ?? flat?.fontSize;
|
|
394
|
+
const lineHeight = lineHeightProp ?? flat?.lineHeight;
|
|
395
|
+
const fontWeight = fontWeightProp ?? flat?.fontWeight;
|
|
396
|
+
const fontFamily = fontFamilyProp ?? flat?.fontFamily;
|
|
397
|
+
const color = colorProp ?? flat?.color;
|
|
398
|
+
const letterSpacing = letterSpacingProp ?? flat?.letterSpacing;
|
|
399
|
+
const textAlignVertical = textAlignVerticalProp ?? styleTextAlignVertical(flat?.textAlignVertical);
|
|
330
400
|
// The layout direction, resolved the way React Native resolves it for its
|
|
331
401
|
// own views: the field's `style.direction` if it says, else the app's.
|
|
332
402
|
// Fabric does not hand a Hybrid View its resolved direction, so native is
|
|
333
403
|
// told outright.
|
|
334
404
|
const rightToLeft = flat?.direction === 'rtl' || (flat?.direction !== 'ltr' && I18nManager.isRTL);
|
|
405
|
+
const textAlign = textAlignProp ?? styleTextAlign(flat?.textAlign, rightToLeft);
|
|
406
|
+
const insets = useMemo(() => styleInsets(flat, rightToLeft), [flat, rightToLeft]);
|
|
335
407
|
// `TextInput` stretches to its parent; sizing to content is this
|
|
336
408
|
// component's own behaviour, and it is what stops it being a drop-in.
|
|
337
409
|
// `NitroInput` turns it off so flexbox gives it the parent's width, the
|
|
@@ -349,13 +421,17 @@ export const NitroInput = forwardRef(function NitroInput({ value, defaultValue,
|
|
|
349
421
|
const measuredRef = useRef(measured);
|
|
350
422
|
measuredRef.current = measured;
|
|
351
423
|
const onSizeChange = useMemo(() => callback((width, height) => {
|
|
424
|
+
reportedRef.current = true;
|
|
352
425
|
if (!measuredRef.current)
|
|
353
426
|
return;
|
|
354
427
|
setSize((previous) => {
|
|
428
|
+
// Until the first report the field shows the seed; a report that
|
|
429
|
+
// matches it leaves `size` as it is, and costs no commit.
|
|
430
|
+
const shown = previous ?? seedRef.current;
|
|
355
431
|
if (!autoWidthRef.current) {
|
|
356
|
-
return
|
|
432
|
+
return shown !== null && shown.height === height ? previous : { width: shown?.width ?? width, height };
|
|
357
433
|
}
|
|
358
|
-
return
|
|
434
|
+
return shown !== null && shown.width === width && shown.height === height
|
|
359
435
|
? previous
|
|
360
436
|
: { width, height };
|
|
361
437
|
});
|
|
@@ -600,15 +676,80 @@ export const NitroInput = forwardRef(function NitroInput({ value, defaultValue,
|
|
|
600
676
|
hostViewProps.importantForAccessibility = resolvedImportance;
|
|
601
677
|
if (resolvedLabelledBy != null)
|
|
602
678
|
hostViewProps.accessibilityLabelledBy = resolvedLabelledBy;
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
:
|
|
608
|
-
|
|
679
|
+
// What the field's size follows. Sent to the view as props and, before
|
|
680
|
+
// the view exists, to `measureSizing`, so the two see the same values.
|
|
681
|
+
const sizing = {
|
|
682
|
+
text: value ?? uncontrolledText,
|
|
683
|
+
mode: resolvedMode,
|
|
684
|
+
plain: !reflows,
|
|
685
|
+
multiline: isMultiline,
|
|
686
|
+
secureTextEntry: secureTextEntry ?? false,
|
|
687
|
+
maxLength: maxLength ?? 0,
|
|
688
|
+
fractionDigits: resolvedFractionDigits,
|
|
689
|
+
maxIntegerDigits: maxIntegerDigits ?? 15,
|
|
690
|
+
groupingSeparator: groupingSeparator ?? derived?.groupingSeparator ?? ',',
|
|
691
|
+
decimalSeparator: decimalSeparator ?? derived?.decimalSeparator ?? '.',
|
|
692
|
+
prefix: resolvedPrefix,
|
|
693
|
+
suffix: resolvedSuffix,
|
|
694
|
+
signPlacement: signPlacement ?? derived?.signPlacement ?? 'beforeAffix',
|
|
695
|
+
placeholder: placeholder ?? '',
|
|
696
|
+
mask: resolvedMode === 'mask' ? (mask ?? '') : '',
|
|
697
|
+
maskNotations: stableNotations,
|
|
698
|
+
maskAutocomplete: maskAutocomplete ?? true,
|
|
699
|
+
maskAutoSkip: maskAutoSkip ?? false,
|
|
700
|
+
maskAffinityFormats: stableAffinityFormats,
|
|
701
|
+
maskAffinityStrategy: maskAffinityStrategy ?? 'wholeString',
|
|
702
|
+
maskTextCase: maskTextCase ?? 'none',
|
|
703
|
+
maskCharacterMap: stableCharacterMap,
|
|
704
|
+
maskSlotPlaceholder: maskSlotPlaceholder ?? (keepPlaceholder ? '_' : ''),
|
|
705
|
+
keepPlaceholder: keepPlaceholder ?? false,
|
|
706
|
+
textSize: resolvedFontSize,
|
|
707
|
+
textLineHeight: Math.max(0, lineHeight ?? 0),
|
|
708
|
+
prefixFontSize: prefixFontSize ?? resolvedFontSize,
|
|
709
|
+
suffixFontSize: suffixFontSize ?? resolvedFontSize,
|
|
710
|
+
textWeight: numericWeight,
|
|
711
|
+
textFamily: fontFamily ?? '',
|
|
712
|
+
textLetterSpacing: letterSpacing ?? 0,
|
|
713
|
+
prefixSpacing: prefixSpacing ?? Infinity,
|
|
714
|
+
suffixSpacing: suffixSpacing ?? Infinity,
|
|
715
|
+
allowFontScaling: allowFontScaling ?? false,
|
|
716
|
+
maxFontSizeMultiplier: maxFontSizeMultiplier ?? 0,
|
|
717
|
+
variant: variant ?? 'none',
|
|
718
|
+
label: label ?? '',
|
|
719
|
+
labelBehavior: labelBehavior ?? 'float',
|
|
720
|
+
labelFontSize: labelFontSize ?? 0,
|
|
721
|
+
cornerRadius: cornerRadius ?? 8,
|
|
722
|
+
};
|
|
723
|
+
// Until the view reports its size, the field lays out at the size it will
|
|
724
|
+
// report, measured now with the view's own code - as `TextInput` measures
|
|
725
|
+
// in its shadow node - rather than at its parent's width for the commit
|
|
726
|
+
// the report takes. Not for a wrapping field (its height needs the width
|
|
727
|
+
// it will get), a `transform` (a worklet, run on the UI thread) or a
|
|
728
|
+
// style that sets both dimensions. Measured again only when what it
|
|
729
|
+
// follows changes.
|
|
730
|
+
if (!reportedRef.current) {
|
|
731
|
+
const seedKey = measured && !isMultiline && transformId === 0 ? JSON.stringify(sizing) : '';
|
|
732
|
+
if (seedKey !== seedKeyRef.current) {
|
|
733
|
+
seedKeyRef.current = seedKey;
|
|
734
|
+
seedRef.current = seedKey === '' ? null : measureSizing(sizing);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const shownSize = size ?? seedRef.current;
|
|
738
|
+
// The size measured for the text, plus the style's padding and border
|
|
739
|
+
// (Yoga's box includes them; `TextInput`'s measured size gets them too).
|
|
740
|
+
const autoSize = useMemo(() => {
|
|
741
|
+
const extraWidth = insets.left + insets.right;
|
|
742
|
+
const extraHeight = insets.top + insets.bottom;
|
|
743
|
+
return shownSize !== null
|
|
744
|
+
? autoWidth
|
|
745
|
+
? { width: shownSize.width + extraWidth, height: shownSize.height + extraHeight }
|
|
746
|
+
: { height: shownSize.height + extraHeight }
|
|
747
|
+
: { height: resolvedFontSize * 1.25 + extraHeight };
|
|
748
|
+
}, [shownSize, resolvedFontSize, autoWidth, insets]);
|
|
749
|
+
return (React.createElement(NativeNitroInputView, { ...hostViewProps, ...sizing, ref: hostRef, style: [autoSize, style], hybridRef: hybridRef,
|
|
609
750
|
// An uncontrolled field's `text` is its initial text: a count of 0 is
|
|
610
751
|
// never caught up after the first edit, so native never puts it back.
|
|
611
|
-
mostRecentEventCount: value != null ? eventCountRef.current : 0,
|
|
752
|
+
mostRecentEventCount: value != null ? eventCountRef.current : 0, prefixAlign: prefixAlign ?? resolvedAffixAlign, suffixAlign: suffixAlign ?? resolvedAffixAlign, prefixOffset: prefixOffset ?? 0, suffixOffset: suffixOffset ?? 0, placeholderColor: processedPlaceholderColor, duration: duration ?? 400, easing: easing ?? 'expo', bounce: bounce ?? 0.15, effect: effect ?? 'auto', textColor: processedColor, textAlignment: textAlign ?? 'auto', rightToLeft: rightToLeft, caretColor: processedCursorColor, selectionColor: processedSelectionColor, caretHidden: caretHidden ?? false, adjustsFontSizeToFit: adjustsFontSizeToFit ?? false, minimumFontScale: minimumFontScale ?? 0.5, labelColor: processedLabelColor, labelFocusedColor: processedLabelFocusedColor, strokeColor: processedStrokeColor, focusedStrokeColor: processedFocusedStrokeColor, strokeWidth: strokeWidth ?? 1, fillColor: processedFillColor, onChangeMask: onChangeMaskCallback, keyboardType: resolvedKeyboardType, returnKeyType: resolvedReturnKeyType, autoCapitalize: autoCapitalize ?? 'sentences', autoCorrect: autoCorrect ?? true, editable: (editable ?? true) && !(readOnly ?? false), numberOfLines: Math.max(0, Math.trunc(numberOfLines ?? rows ?? 0)), textVerticalAlignment: textAlignVertical ?? 'auto', textInsetLeft: insets.left, textInsetTop: insets.top, textInsetRight: insets.right, textInsetBottom: insets.bottom, scrollEnabled: scrollEnabled ?? true, autoFocus: autoFocus ?? false, fieldTestID: viewProps.testID ?? '', fieldAccessibilityLabel: resolvedAccessibilityLabel ?? '', submitBehavior: submitBehavior ??
|
|
612
753
|
// As `TextInput` resolves the legacy `blurOnSubmit`: a multiline
|
|
613
754
|
// field's return key inserts a line break unless told to blur.
|
|
614
755
|
(multiline
|
|
@@ -617,7 +758,7 @@ export const NitroInput = forwardRef(function NitroInput({ value, defaultValue,
|
|
|
617
758
|
: 'newline'
|
|
618
759
|
: blurOnSubmit === false
|
|
619
760
|
? 'submit'
|
|
620
|
-
: 'blurAndSubmit'),
|
|
761
|
+
: 'blurAndSubmit'), keyboardAppearance: keyboardAppearance ?? 'default', textContentType: textContentType ?? autoComplete ?? '', enablesReturnKeyAutomatically: enablesReturnKeyAutomatically ?? false, showSoftInputOnFocus: resolvedShowSoftInput, selectTextOnFocus: selectTextOnFocus ?? false, clearTextOnFocus: clearTextOnFocus ?? false, keyboardHandoffMs: keyboardHandoffMs ?? 0, returnKeyBar: returnKeyBar ?? true, contextMenuHidden: contextMenuHidden ?? false, spellCheck: spellCheck ?? autoCorrect ?? true, selectionStart: selection?.start ?? -1, selectionEnd: selection?.end ?? selection?.start ?? -1, transformWorklet: transformId, onChangeTextWorklet: onChangeTextId, onChangeValueWorklet: onChangeValueId, onFocusChangeWorklet: onFocusId, onSelectionChangeWorklet: onSelectionChangeId, onSubmitEditingWorklet: onSubmitEditingId, onEndEditingWorklet: onEndEditingId, onKeyPressWorklet: onKeyPressId, onChangeText: onChangeTextCallback, onChangeValue: onChangeValueCallback, onFocusChange: onFocusChangeCallback, onSubmitEditing: onSubmitEditingCallback, onEndEditing: onEndEditingCallback, onSelectionChange: onSelectionChangeCallback, onKeyPress: onKeyPressCallback, onSizeChange: onSizeChange }));
|
|
621
762
|
});
|
|
622
763
|
// The name DevTools and error stacks show, as `TextInput` names itself.
|
|
623
764
|
NitroInput.displayName = 'NitroInput';
|
|
@@ -3,7 +3,7 @@ import { I18nManager, StyleSheet, } from 'react-native';
|
|
|
3
3
|
import { callback, getHostComponent, } from 'react-native-nitro-modules';
|
|
4
4
|
import NitroNumberViewConfig from '../nitrogen/generated/shared/json/NitroNumberViewConfig.json';
|
|
5
5
|
import { toNumericWeight, toProcessedColor } from './styleHelpers';
|
|
6
|
-
import {
|
|
6
|
+
import { figureOf, formatProps, toNumber, withMinorDigits } from './formatProps';
|
|
7
7
|
/**
|
|
8
8
|
* The raw Nitro host component. Prefer {@link NitroNumber}, which adds
|
|
9
9
|
* auto-sizing, color/weight conversion and a convenient handle.
|
|
@@ -27,7 +27,7 @@ const TRANSITION_DEFAULTS = {
|
|
|
27
27
|
* The view sizes itself to its content unless you pass an explicit `width` /
|
|
28
28
|
* `height` in `style`.
|
|
29
29
|
*/
|
|
30
|
-
export const NitroNumber = forwardRef(function NitroNumber({ value, fractionDigits, minimumIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, transition, flashUpColor, flashDownColor, flashDuration, popOnChange, duration, easing, bounce, stagger, direction, reveal, revealStyle, revealDuration, revealBounce, revealGrow, revealStagger, revealMilestones, revealMilestoneHold, onRevealEnd, onRevealMilestone, loading, shimmerColor, shimmerDuration, shimmerAngle, shimmerWidth, shimmerBaseColor, shimmerDirection, shimmerDelay, fontSize, prefixFontSize, suffixFontSize, affixAlign, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, tabularNums, format, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, fontWeight, fontFamily, color, textAlign, signDisplay, plusSign, minusSign, prefixColor, suffixColor, fractionColor, fractionFontSize, fractionAlign, digitGlyphs, groupingSizes, digits, continuous, animated, respectReduceMotion, onAnimationStart, onAnimationEnd, onNativeRef, style, ...viewProps }, ref) {
|
|
30
|
+
export const NitroNumber = forwardRef(function NitroNumber({ value, minorDigits, fractionDigits, minimumIntegerDigits, groupingSeparator, decimalSeparator, prefix, suffix, transition, flashUpColor, flashDownColor, flashDuration, popOnChange, duration, easing, bounce, stagger, direction, reveal, revealStyle, revealDuration, revealBounce, revealGrow, revealStagger, revealMilestones, revealMilestoneHold, onRevealEnd, onRevealMilestone, loading, shimmerColor, shimmerDuration, shimmerAngle, shimmerWidth, shimmerBaseColor, shimmerDirection, shimmerDelay, fontSize, prefixFontSize, suffixFontSize, affixAlign, prefixAlign, suffixAlign, letterSpacing, prefixSpacing, suffixSpacing, prefixOffset, suffixOffset, tabularNums, format, adjustsFontSizeToFit, minimumFontScale, allowFontScaling, maxFontSizeMultiplier, fontWeight, fontFamily, color, textAlign, signDisplay, plusSign, minusSign, prefixColor, suffixColor, fractionColor, fractionFontSize, fractionAlign, digitGlyphs, groupingSizes, digits, continuous, animated, respectReduceMotion, onAnimationStart, onAnimationEnd, onNativeRef, style, ...viewProps }, ref) {
|
|
31
31
|
const nativeRef = useRef(null);
|
|
32
32
|
const latestOnNativeRef = useRef(onNativeRef);
|
|
33
33
|
latestOnNativeRef.current = onNativeRef;
|
|
@@ -35,8 +35,10 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, fractionDigi
|
|
|
35
35
|
latestOnRevealEnd.current = onRevealEnd;
|
|
36
36
|
const latestOnRevealMilestone = useRef(onRevealMilestone);
|
|
37
37
|
latestOnRevealMilestone.current = onRevealMilestone;
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// The value as the format takes it: minor units shifted in, exactly.
|
|
39
|
+
const input = withMinorDigits(value, minorDigits);
|
|
40
|
+
const latestValue = useRef(input);
|
|
41
|
+
latestValue.current = input;
|
|
40
42
|
const latestOnAnimationStart = useRef(onAnimationStart);
|
|
41
43
|
latestOnAnimationStart.current = onAnimationStart;
|
|
42
44
|
const latestOnAnimationEnd = useRef(onAnimationEnd);
|
|
@@ -68,7 +70,7 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, fractionDigi
|
|
|
68
70
|
}), []);
|
|
69
71
|
// The figure a compact format rolls is not the value (1.2 for 1,234): report the value.
|
|
70
72
|
const onAnimationEndCallback = useMemo(() => callback((shown) => {
|
|
71
|
-
latestOnAnimationEnd.current?.(compactRef.current ? latestValue.current : shown);
|
|
73
|
+
latestOnAnimationEnd.current?.(compactRef.current ? toNumber(latestValue.current) : shown);
|
|
72
74
|
}), []);
|
|
73
75
|
// A new array literal each render must not re-set the native prop. Always an
|
|
74
76
|
// array (empty = no tiers): removing the prop would reach native as `null`,
|
|
@@ -83,7 +85,7 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, fractionDigi
|
|
|
83
85
|
jumpTo: (next) => nativeRef.current?.jumpTo(next),
|
|
84
86
|
animateTo: (next) => nativeRef.current?.animateTo(next),
|
|
85
87
|
revealTo: (next) => nativeRef.current?.revealTo(next),
|
|
86
|
-
getValue: () =>
|
|
88
|
+
getValue: () => compactRef.current ? toNumber(latestValue.current) : nativeRef.current?.value ?? toNumber(latestValue.current),
|
|
87
89
|
get native() {
|
|
88
90
|
return nativeRef.current;
|
|
89
91
|
},
|
|
@@ -104,14 +106,20 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, fractionDigi
|
|
|
104
106
|
const resolvedFontSize = fontSize ?? 32;
|
|
105
107
|
const resolvedAffixAlign = affixAlign ?? 'baseline';
|
|
106
108
|
const derived = format ? formatProps(format) : undefined;
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
+
// What the format shows for this value: the figure rounded its way, in the
|
|
110
|
+
// fraction digits it prints. Compact notation also swaps the suffix: "1.2K"
|
|
111
|
+
// rolls the figure 1.2.
|
|
112
|
+
const figure = useMemo(() => (format ? figureOf(format, input) : undefined), [format, input]);
|
|
113
|
+
const compact = derived?.compact ? figure : undefined;
|
|
109
114
|
const compactRef = useRef(false);
|
|
110
115
|
compactRef.current = compact !== undefined;
|
|
111
116
|
// Which way the value moved, for a compact figure that fell while the value grew (999 → 1K).
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
previousValue.current
|
|
117
|
+
const numericValue = toNumber(input);
|
|
118
|
+
const previousValue = useRef(numericValue);
|
|
119
|
+
const compactDirection = compact && direction === undefined ? (numericValue >= previousValue.current ? 'up' : 'down') : undefined;
|
|
120
|
+
previousValue.current = numericValue;
|
|
121
|
+
// Without a format the engine rounds the double, as it always has.
|
|
122
|
+
const shownValue = figure ? figure.value : numericValue;
|
|
115
123
|
const processedPrefixColor = useMemo(() => toProcessedColor(prefixColor) ?? Infinity, [prefixColor]);
|
|
116
124
|
const processedSuffixColor = useMemo(() => toProcessedColor(suffixColor) ?? Infinity, [suffixColor]);
|
|
117
125
|
const processedFractionColor = useMemo(() => toProcessedColor(fractionColor) ?? Infinity, [fractionColor]);
|
|
@@ -147,5 +155,5 @@ export const NitroNumber = forwardRef(function NitroNumber({ value, fractionDigi
|
|
|
147
155
|
// does not hand a Hybrid View its resolved direction, so native is told.
|
|
148
156
|
const layoutDirection = useMemo(() => StyleSheet.flatten(style)?.direction, [style]);
|
|
149
157
|
const rightToLeft = layoutDirection === 'rtl' || (layoutDirection !== 'ltr' && I18nManager.isRTL);
|
|
150
|
-
return (React.createElement(NativeNitroNumberView, { ...viewProps, style: [autoSize, style], hybridRef: hybridRef, value:
|
|
158
|
+
return (React.createElement(NativeNitroNumberView, { ...viewProps, style: [autoSize, style], hybridRef: hybridRef, value: shownValue, fractionDigits: fractionDigits ?? figure?.fractionDigits ?? 0, minimumIntegerDigits: minimumIntegerDigits ?? derived?.minimumIntegerDigits ?? 1, groupingSeparator: groupingSeparator ?? derived?.groupingSeparator ?? '', decimalSeparator: decimalSeparator ?? derived?.decimalSeparator ?? '.', prefix: prefix ?? compact?.prefix ?? derived?.prefix ?? '', suffix: suffix ?? compact?.suffix ?? derived?.suffix ?? '', transition: transition ?? 'roll', flashUpColor: processedFlashUp, flashDownColor: processedFlashDown, flashDuration: flashDuration ?? 600, popOnChange: popOnChange ?? 0, duration: still ? 0 : duration ?? TRANSITION_DEFAULTS[transition ?? 'roll'].duration, easing: easing ?? TRANSITION_DEFAULTS[transition ?? 'roll'].easing, bounce: bounce ?? 0.15, stagger: stagger ?? TRANSITION_DEFAULTS[transition ?? 'roll'].stagger, rollDirection: direction ?? compactDirection ?? 'auto', revealState: reveal === undefined ? 0 : reveal ? 2 : 1, revealStyle: revealStyle ?? 'count', revealDuration: still ? 0 : revealDuration ?? 2200, revealBounce: revealBounce ?? 0.12, revealGrow: revealGrow ?? 0.2, revealStagger: revealStagger ?? 200, revealMilestones: stableMilestones, revealMilestoneHold: revealMilestoneHold ?? 0, onRevealEnd: onRevealEndCallback, onRevealMilestone: onRevealMilestoneCallback, onAnimationStart: onAnimationStartCallback, onAnimationEnd: onAnimationEndCallback, loading: loading ?? false, shimmerColor: processedShimmerColor, shimmerDuration: shimmerDuration ?? 950, shimmerAngle: shimmerAngle ?? 31, shimmerWidth: shimmerWidth ?? 1, shimmerBaseColor: processedShimmerBase, shimmerDirection: shimmerDirection ?? 'auto', shimmerDelay: shimmerDelay ?? 0, textSize: resolvedFontSize, prefixFontSize: prefixFontSize ?? resolvedFontSize, suffixFontSize: suffixFontSize ?? resolvedFontSize, affixAlign: resolvedAffixAlign, prefixAlign: prefixAlign ?? resolvedAffixAlign, suffixAlign: suffixAlign ?? resolvedAffixAlign, textLetterSpacing: letterSpacing ?? 0, prefixSpacing: prefixSpacing ?? Infinity, suffixSpacing: suffixSpacing ?? Infinity, prefixOffset: prefixOffset ?? 0, suffixOffset: suffixOffset ?? 0, tabularNums: tabularNums ?? true, signDisplay: signDisplay ?? derived?.signDisplay ?? 'auto', plusSign: plusSign ?? derived?.plusSign ?? '+', minusSign: minusSign ?? derived?.minusSign ?? '-', digitGlyphs: stableGlyphs, groupingSizes: stableGroups, digitMax: digitMax, continuous: continuous ?? false, prefixColor: processedPrefixColor, suffixColor: processedSuffixColor, fractionColor: processedFractionColor, fractionFontSize: fractionFontSize ?? Infinity, fractionAlign: fractionAlign ?? 'baseline', respectReduceMotion: respectReduceMotion ?? true, adjustsFontSizeToFit: adjustsFontSizeToFit ?? false, minimumFontScale: minimumFontScale ?? 0.5, allowFontScaling: allowFontScaling ?? false, maxFontSizeMultiplier: maxFontSizeMultiplier ?? 0, textWeight: numericWeight, textFamily: fontFamily ?? '', textColor: processedColor, textAlignment: textAlign ?? 'auto', rightToLeft: rightToLeft, onSizeChange: onSizeChange }));
|
|
151
159
|
});
|
|
@@ -46,18 +46,18 @@ export function NitroText({ children, fontSize, fontWeight, fontFamily, color, l
|
|
|
46
46
|
native.text = text;
|
|
47
47
|
native.style = style == null ? { width: Math.ceil(width), height } : [{ width: Math.ceil(width), height }, style];
|
|
48
48
|
if (size !== 17)
|
|
49
|
-
native.
|
|
49
|
+
native.textSize = size;
|
|
50
50
|
if (weight !== 400)
|
|
51
|
-
native.
|
|
51
|
+
native.textWeight = weight;
|
|
52
52
|
if (family !== '')
|
|
53
|
-
native.
|
|
53
|
+
native.textFamily = family;
|
|
54
54
|
if (spacing !== 0)
|
|
55
|
-
native.
|
|
55
|
+
native.textLetterSpacing = spacing;
|
|
56
56
|
const processedColor = toProcessedColor(color);
|
|
57
57
|
if (processedColor !== undefined)
|
|
58
|
-
native.
|
|
58
|
+
native.textColor = processedColor;
|
|
59
59
|
if (textAlign !== undefined)
|
|
60
|
-
native.
|
|
60
|
+
native.textAlignment = textAlign;
|
|
61
61
|
if (duration !== undefined)
|
|
62
62
|
native.duration = duration;
|
|
63
63
|
if (easing !== undefined)
|
|
@@ -350,6 +350,24 @@ const nativeOf = (value, method, unwrap = false) => slotsOf(value, method, unwra
|
|
|
350
350
|
export function sharedFormatterOf(value) {
|
|
351
351
|
return typeof value === 'object' && value !== null ? slots.get(value)?.native : undefined;
|
|
352
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* What `format` shows for `value`, as `NitroNumber` rolls it: the figure
|
|
355
|
+
* rounded and trimmed as the format prints it, and the text around it. Read
|
|
356
|
+
* in C++ off the formatter's own parts, from the number, bigint or decimal
|
|
357
|
+
* string itself.
|
|
358
|
+
* @internal
|
|
359
|
+
*/
|
|
360
|
+
export function figureOf(format, value) {
|
|
361
|
+
return nativeOf(format, 'figure').figure(toMathematicalValue(value));
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* The prefix, suffix, separators, sign placement, grouping and digits of
|
|
365
|
+
* `format`, read in C++ off its parts.
|
|
366
|
+
* @internal
|
|
367
|
+
*/
|
|
368
|
+
export function layoutOf(format) {
|
|
369
|
+
return nativeOf(format, 'layout').layout();
|
|
370
|
+
}
|
|
353
371
|
// `length` is 0 as the specification says: the arguments are read from `arguments`.
|
|
354
372
|
function NumberFormatConstructor() {
|
|
355
373
|
const locales = arguments[0];
|
|
@@ -1,32 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { layoutOf, sharedFormatterOf } from './NumberFormat';
|
|
2
|
+
export { figureOf } from './NumberFormat';
|
|
3
3
|
// By the native formatter, which instances built with the same locales and options share.
|
|
4
4
|
const cache = new WeakMap();
|
|
5
|
-
function join(parts, from, to) {
|
|
6
|
-
return parts
|
|
7
|
-
.slice(from, to)
|
|
8
|
-
.map((p) => p.value)
|
|
9
|
-
.join('');
|
|
10
|
-
}
|
|
11
|
-
/** The text before and after the number in `parts`. */
|
|
12
|
-
function affixes(parts) {
|
|
13
|
-
const first = parts.findIndex((p) => NUMBER_PARTS.has(p.type));
|
|
14
|
-
let last = first;
|
|
15
|
-
parts.forEach((p, i) => {
|
|
16
|
-
if (NUMBER_PARTS.has(p.type))
|
|
17
|
-
last = i;
|
|
18
|
-
});
|
|
19
|
-
// The sign is the component's own glyph, not part of an affix.
|
|
20
|
-
const unsigned = (from, to) => join(parts.map((p) => (p.type === 'minusSign' || p.type === 'plusSign' ? { ...p, value: '' } : p)), from, to);
|
|
21
|
-
return {
|
|
22
|
-
prefix: first < 0 ? '' : unsigned(0, first),
|
|
23
|
-
suffix: first < 0 ? '' : unsigned(last + 1, parts.length),
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
5
|
/**
|
|
27
|
-
* The prefix, suffix, separators and digit counts of `format`, read
|
|
28
|
-
* parts once and cached per formatter. The components keep their own
|
|
29
|
-
* (a sign, a prefix, digits in groups, a suffix), so a format outside it
|
|
6
|
+
* The prefix, suffix, separators and digit counts of `format`, read in C++
|
|
7
|
+
* off its parts once and cached per formatter. The components keep their own
|
|
8
|
+
* model (a sign, a prefix, digits in groups, a suffix), so a format outside it
|
|
30
9
|
* (accounting parentheses, a unit between the digits) is followed as far as
|
|
31
10
|
* that model goes.
|
|
32
11
|
*/
|
|
@@ -36,69 +15,63 @@ export function formatProps(format) {
|
|
|
36
15
|
if (cached)
|
|
37
16
|
return cached;
|
|
38
17
|
const resolved = format.resolvedOptions();
|
|
39
|
-
const
|
|
40
|
-
const fractionDigits = resolved.maximumFractionDigits ?? 0;
|
|
41
|
-
// A value with every part: groups, and a fraction when the format has one.
|
|
42
|
-
// Compact notation would print it "1.2M", so its affixes come per value.
|
|
43
|
-
const parts = format.formatToParts(compact ? 1 : fractionDigits > 0 ? 1234567.5 : 1234567);
|
|
44
|
-
const negative = format.formatToParts(-1);
|
|
45
|
-
const minus = negative.findIndex((p) => p.type === 'minusSign');
|
|
46
|
-
const currency = negative.findIndex((p) => p.type === 'currency' || p.type === 'percentSign');
|
|
47
|
-
const integers = parts.filter((p) => p.type === 'integer').map((p) => p.value.length);
|
|
18
|
+
const layout = layoutOf(format);
|
|
48
19
|
const props = {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
20
|
+
prefix: layout.prefix,
|
|
21
|
+
suffix: layout.suffix,
|
|
22
|
+
groupingSeparator: layout.groupingSeparator,
|
|
23
|
+
decimalSeparator: layout.decimalSeparator,
|
|
24
|
+
fractionDigits: resolved.maximumFractionDigits ?? 0,
|
|
53
25
|
minimumIntegerDigits: resolved.minimumIntegerDigits,
|
|
54
|
-
signPlacement:
|
|
26
|
+
signPlacement: layout.signAfterAffix ? 'afterAffix' : 'beforeAffix',
|
|
55
27
|
signDisplay: (resolved.signDisplay ?? 'auto'),
|
|
56
|
-
minusSign:
|
|
28
|
+
minusSign: layout.minusSign,
|
|
57
29
|
plusSign: '+',
|
|
58
|
-
digitGlyphs:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
compact,
|
|
30
|
+
digitGlyphs: layout.digitGlyphs,
|
|
31
|
+
groupingSizes: layout.groupingSizes,
|
|
32
|
+
compact: resolved.notation === 'compact',
|
|
62
33
|
};
|
|
63
34
|
cache.set(key, props);
|
|
64
35
|
return props;
|
|
65
36
|
}
|
|
66
|
-
const digitGlyphCache = new Map();
|
|
67
|
-
/** The ten digits of a numbering system, or [] for Latin ones. */
|
|
68
|
-
function digitGlyphsOf(locale, numberingSystem) {
|
|
69
|
-
if (!numberingSystem || numberingSystem === 'latn')
|
|
70
|
-
return [];
|
|
71
|
-
const cached = digitGlyphCache.get(numberingSystem);
|
|
72
|
-
if (cached)
|
|
73
|
-
return cached;
|
|
74
|
-
const digits = new NumberFormat(locale, { numberingSystem, useGrouping: false });
|
|
75
|
-
const glyphs = Array.from({ length: 10 }, (_, d) => digits.format(d));
|
|
76
|
-
const result = glyphs.every((g, d) => g === String(d)) ? [] : glyphs;
|
|
77
|
-
digitGlyphCache.set(numberingSystem, result);
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
37
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* a
|
|
38
|
+
* `value` read in units of 10^-`minorDigits`, exactly: 123456n with 2 minor
|
|
39
|
+
* digits is "1234.56". A bigint or a numeric string is shifted as text, so no
|
|
40
|
+
* digit goes through a double: a decimal ("1234.5"), one with an exponent
|
|
41
|
+
* ("1e5" becomes "1e3"), or a 0x / 0o / 0b integer, as `Intl.NumberFormat`
|
|
42
|
+
* reads strings. A number that is a safe integer is shifted the same way, any
|
|
43
|
+
* other number is divided. A string that is not a number ("1,234.56") comes
|
|
44
|
+
* back as it is, which the format shows as NaN, never as a wrong amount.
|
|
84
45
|
*/
|
|
85
|
-
export function
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
46
|
+
export function withMinorDigits(value, minorDigits) {
|
|
47
|
+
const digits = minorDigits === undefined ? 0 : Math.max(0, Math.trunc(minorDigits));
|
|
48
|
+
if (digits === 0)
|
|
49
|
+
return value;
|
|
50
|
+
if (typeof value === 'number') {
|
|
51
|
+
if (!Number.isSafeInteger(value))
|
|
52
|
+
return value / 10 ** digits;
|
|
53
|
+
value = BigInt(value);
|
|
54
|
+
}
|
|
55
|
+
let text = typeof value === 'bigint' ? value.toString() : value.trim();
|
|
56
|
+
if (/^0[xob][0-9a-f]+$/i.test(text)) {
|
|
57
|
+
// A non-decimal integer: BigInt reads it as Intl does, exactly.
|
|
58
|
+
text = BigInt(text).toString();
|
|
59
|
+
}
|
|
60
|
+
const match = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(text);
|
|
61
|
+
if (!match || (match[2] === '' && match[3] === undefined && text !== ''))
|
|
62
|
+
return value;
|
|
63
|
+
const [, sign = '', whole = '', fraction = '', exponent] = match;
|
|
64
|
+
if (whole === '' && fraction === '' && text.includes('.'))
|
|
65
|
+
return value;
|
|
66
|
+
if (exponent !== undefined) {
|
|
67
|
+
// Moving the point is moving the exponent.
|
|
68
|
+
return `${sign}${whole}${fraction ? `.${fraction}` : ''}e${Number(exponent) - digits}`;
|
|
69
|
+
}
|
|
70
|
+
const all = (whole + fraction).padStart(digits + fraction.length + 1, '0');
|
|
71
|
+
const point = all.length - fraction.length - digits;
|
|
72
|
+
return `${sign}${all.slice(0, point)}.${all.slice(point)}`;
|
|
73
|
+
}
|
|
74
|
+
/** `value` as a double, for comparisons and for the engine when there is no format. */
|
|
75
|
+
export function toNumber(value) {
|
|
76
|
+
return typeof value === 'number' ? value : Number(value);
|
|
104
77
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { type ColorValue, type TextStyle, type ViewProps } from 'react-native';
|
|
2
|
+
import { type ColorValue, type StyleProp, 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';
|
|
@@ -67,7 +67,14 @@ export interface NitroInputKeyPressEvent {
|
|
|
67
67
|
target: number;
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
|
-
export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus' | 'onBlur'> {
|
|
70
|
+
export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus' | 'onBlur' | 'style'> {
|
|
71
|
+
/**
|
|
72
|
+
* As `TextInput`'s: a view style, and the text styles it reads its
|
|
73
|
+
* typography from (`fontSize`, `fontWeight`, `fontFamily`, `color`,
|
|
74
|
+
* `letterSpacing`, `lineHeight`, `textAlign`, `textAlignVertical`). The props
|
|
75
|
+
* of the same name win over it.
|
|
76
|
+
*/
|
|
77
|
+
style?: StyleProp<TextStyle>;
|
|
71
78
|
/**
|
|
72
79
|
* The text to show (controlled). The native side formats and shows what the
|
|
73
80
|
* user types on its own; this prop is applied when it changes to something
|
|
@@ -494,18 +501,4 @@ export interface NitroInputHandle {
|
|
|
494
501
|
* the glyph engine (`'reflow'`).
|
|
495
502
|
*/
|
|
496
503
|
export type NitroInputTransition = 'none' | 'reflow';
|
|
497
|
-
/**
|
|
498
|
-
* A native text input: a system field (`UITextField` / `EditText`) that owns
|
|
499
|
-
* the keyboard, editing, selection, paste and accessibility, with native
|
|
500
|
-
* formatting (`mode="number"`), native masking (`mode="mask"`), an outlined
|
|
501
|
-
* or filled frame with a floating label, and optionally the glyph engine
|
|
502
|
-
* (`transition="reflow"`), under which characters that stay glide to their new place, new
|
|
503
|
-
* ones slide or fade in and removed ones leave alongside their neighbours.
|
|
504
|
-
* In `'number'` mode every edit is formatted on the native side before a
|
|
505
|
-
* frame is drawn, with the caret kept in place, so there is never an
|
|
506
|
-
* unformatted frame and never a round trip through JS.
|
|
507
|
-
*
|
|
508
|
-
* Like a `TextInput`, it takes its width from its parent; `autoWidth` sizes
|
|
509
|
-
* it to its content instead, and is on by default when the text reflows.
|
|
510
|
-
*/
|
|
511
504
|
export declare const NitroInput: React.ForwardRefExoticComponent<NitroInputProps & React.RefAttributes<NitroInputHandle>>;
|