react-native-transformer-text-input 0.1.0-alpha.5 → 0.2.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/README.md +18 -2
- package/android/src/main/java/com/appandflow/transformertextinput/TransformerTextInputDecoratorView.kt +30 -0
- package/cpp/TransformerTextInputDecoratorViewShadowNode.cpp +24 -1
- package/ios/TransformerTextInputDecoratorView.mm +29 -1
- package/lib/module/TransformerTextInput.js +24 -0
- package/lib/module/TransformerTextInput.js.map +1 -1
- package/lib/module/formatters/currency.js +122 -0
- package/lib/module/formatters/currency.js.map +1 -0
- package/lib/module/formatters/phone-data.js +153 -27
- package/lib/module/formatters/phone-data.js.map +1 -1
- package/lib/module/formatters/phone-number.js +14 -2
- package/lib/module/formatters/phone-number.js.map +1 -1
- package/lib/module/registry.js +4 -3
- package/lib/module/registry.js.map +1 -1
- package/lib/typescript/src/TransformerTextInput.d.ts.map +1 -1
- package/lib/typescript/src/formatters/currency.d.ts +17 -0
- package/lib/typescript/src/formatters/currency.d.ts.map +1 -0
- package/lib/typescript/src/formatters/phone-data.d.ts +1 -0
- package/lib/typescript/src/formatters/phone-data.d.ts.map +1 -1
- package/lib/typescript/src/formatters/phone-number.d.ts.map +1 -1
- package/lib/typescript/src/registry.d.ts.map +1 -1
- package/package.json +6 -1
- package/src/TransformerTextInput.tsx +23 -1
- package/src/formatters/currency.ts +142 -0
- package/src/formatters/phone-data.ts +158 -28
- package/src/formatters/phone-number.ts +18 -2
- package/src/registry.ts +4 -3
package/lib/module/registry.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import { runOnUI } from 'react-native-worklets';
|
|
3
|
+
import { runOnUI, executeOnUIRuntimeSync } from 'react-native-worklets';
|
|
4
4
|
import NativeTransformerTextInputModule from "./NativeTransformerTextInputModule.js";
|
|
5
5
|
import { computeUncontrolledSelection, validateSelection } from "./selection.js";
|
|
6
6
|
let initialized = false;
|
|
@@ -9,8 +9,9 @@ function initializeIfNeeded() {
|
|
|
9
9
|
return;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
|
|
12
|
+
// Set up registry on UI runtime synchronously so it is guaranteed to exist
|
|
13
|
+
// when native code accesses it after install().
|
|
14
|
+
executeOnUIRuntimeSync(() => {
|
|
14
15
|
'worklet';
|
|
15
16
|
|
|
16
17
|
const transformersMap = new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["runOnUI","NativeTransformerTextInputModule","computeUncontrolledSelection","validateSelection","initialized","initializeIfNeeded","transformersMap","Map","globalThis","__rntti_registerTransformerRegistry","register","id","transformer","set","unregister","transformerId","delete","get","install","currentId","registerTransformer","worklet","previousValue","previousSelection","transformerWrapper","value","selectionStart","selectionEnd","transform","result","selection","start","end","newValue","newSelection","length","unregisterTransformer","global"],"sourceRoot":"../../src","sources":["registry.ts"],"mappings":";;AAAA,SAASA,OAAO,QAAQ,uBAAuB;
|
|
1
|
+
{"version":3,"names":["runOnUI","executeOnUIRuntimeSync","NativeTransformerTextInputModule","computeUncontrolledSelection","validateSelection","initialized","initializeIfNeeded","transformersMap","Map","globalThis","__rntti_registerTransformerRegistry","register","id","transformer","set","unregister","transformerId","delete","get","install","currentId","registerTransformer","worklet","previousValue","previousSelection","transformerWrapper","value","selectionStart","selectionEnd","transform","result","selection","start","end","newValue","newSelection","length","unregisterTransformer","global"],"sourceRoot":"../../src","sources":["registry.ts"],"mappings":";;AAAA,SAASA,OAAO,EAAEC,sBAAsB,QAAQ,uBAAuB;AACvE,OAAOC,gCAAgC,MAAM,uCAAoC;AAEjF,SAASC,4BAA4B,EAAEC,iBAAiB,QAAQ,gBAAa;AAqB7E,IAAIC,WAAW,GAAG,KAAK;AAEvB,SAASC,kBAAkBA,CAAA,EAAG;EAC5B,IAAID,WAAW,EAAE;IACf;EACF;;EAEA;EACA;EACAJ,sBAAsB,CAAC,MAAM;IAC3B,SA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AAIA,KAAK;MACvC,IAAIS,YAAuB;MAC3B,IAAIL,MAAM,EAAEC,SAAS,IAAI,IAAI,EAAE;QAC7BI,YAAY,GAAGL,MAAM,CAACC,SAAS;QAC/B3B,iBAAiB,CAAC+B,YAAY,EAAED,QAAQ,CAACE,MAAM,CAAC;MAClD,CAAC,MAAM;QACLD,YAAY,GAAGhC,4BAA4B,CACzCuB,KAAK,EACLQ,QAAQ,EACRP,cAAc,EACdC,YACF,CAAC;MACH;MACAL,aAAa,GAAGW,QAAQ;MACxBV,iBAAiB,GAAGW,YAAY;MAChC,OAAO;QACLT,KAAK,EAAEQ,QAAQ;QACfH,SAAS,EAAEI;MACb,CAAC;IACH,CAAC;IAED1B,UAAU,CAACC,mCAAmC,EAAEC,QAAQ,CACtDC,EAAE,EACFa,kBACF,CAAC;EACH,CAAC,CAAC,CAAC,CAAC;EAEJ,OAAOb,EAAE;AACX;AAEA,OAAO,SAASyB,qBAAqBA,CAACrB,aAAqB,EAAE;EAC3DhB,OAAO,CAAC,MAAM;IACZ,SAAS;;IAETsC,MAAM,CAAC5B,mCAAmC,EAAEK,UAAU,CAACC,aAAa,CAAC;EACvE,CAAC,CAAC,CAAC,CAAC;AACN","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransformerTextInput.d.ts","sourceRoot":"","sources":["../../../src/TransformerTextInput.tsx"],"names":[],"mappings":"AASA,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,cAAc,EACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAkB,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAQjE,KAAK,mCAAmC,GAAG;IACzC;;OAEG;IACH,QAAQ,EAAE,MAAM,MAAM,CAAC;IACvB;;OAEG;IACH,MAAM,EAAE,CAAC,OAAO,EAAE;QAChB;;WAEG;QACH,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACtB;;WAEG;QACH,SAAS,CAAC,EAAE;YAAE,KAAK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OAEG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG,YAAY,GACrD,mCAAmC,CAAC;AAEtC,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC,GAAG;IACtE;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC;CAC1B,CAAC;AAEF,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA4G6imB,CAAC;;;;;;;;;;IAlH7kmB;;OAEG;iBACU,WAAW;gEA0GzB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Transformer } from '../Transformer';
|
|
2
|
+
export type CurrencyTransformerOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* ISO 4217 currency code, e.g. 'USD', 'EUR', 'JPY'. Drives the symbol and
|
|
5
|
+
* the number of fraction digits.
|
|
6
|
+
*/
|
|
7
|
+
currency: string;
|
|
8
|
+
/**
|
|
9
|
+
* BCP 47 locale tag, e.g. 'en-US', 'de-DE'. Drives separators and symbol
|
|
10
|
+
* position. Defaults to the runtime's default locale.
|
|
11
|
+
*/
|
|
12
|
+
locale?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare class CurrencyTransformer extends Transformer {
|
|
15
|
+
constructor(options: CurrencyTransformerOptions);
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=currency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../../../../src/formatters/currency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,EAAE,0BAA0B;CA6HhD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"phone-data.d.ts","sourceRoot":"","sources":["../../../../src/formatters/phone-data.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,
|
|
1
|
+
{"version":3,"file":"phone-data.d.ts","sourceRoot":"","sources":["../../../../src/formatters/phone-data.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAw5K/D,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,gBAAgB,EAsP1C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"phone-number.d.ts","sourceRoot":"","sources":["../../../../src/formatters/phone-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAkB,MAAM,gBAAgB,CAAC;AAG7D,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAuOF,qBAAa,sBAAuB,SAAQ,WAAW;gBACzC,EACV,OAAc,EACd,kBAAyB,EACzB,KAAa,GACd,GAAE,6BAAkC;
|
|
1
|
+
{"version":3,"file":"phone-number.d.ts","sourceRoot":"","sources":["../../../../src/formatters/phone-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAkB,MAAM,gBAAgB,CAAC;AAG7D,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAuOF,qBAAa,sBAAuB,SAAQ,WAAW;gBACzC,EACV,OAAc,EACd,kBAAyB,EACzB,KAAa,GACd,GAAE,6BAAkC;CAyKtC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../../src/registry.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAGjE,KAAK,kBAAkB,GAAG,CACxB,KAAK,EAA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AM,CAyDpE;AAED,wBAAgB,qBAAqB,CAAC,aAAa,EAAE,MAAM,QAM1D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-transformer-text-input",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "TextInput component that allows transforming text synchronously with a worklet",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -20,6 +20,11 @@
|
|
|
20
20
|
"types": "./lib/typescript/src/formatters/pattern.d.ts",
|
|
21
21
|
"default": "./lib/module/formatters/pattern.js"
|
|
22
22
|
},
|
|
23
|
+
"./formatters/currency": {
|
|
24
|
+
"source": "./src/formatters/currency.ts",
|
|
25
|
+
"types": "./lib/typescript/src/formatters/currency.d.ts",
|
|
26
|
+
"default": "./lib/module/formatters/currency.js"
|
|
27
|
+
},
|
|
23
28
|
"./formatters/phone-data": {
|
|
24
29
|
"source": "./src/formatters/phone-data.ts",
|
|
25
30
|
"types": "./lib/typescript/src/formatters/phone-data.d.ts",
|
|
@@ -61,13 +61,34 @@ export type TransformerTextInputProps = Omit<TextInputProps, 'value'> & {
|
|
|
61
61
|
|
|
62
62
|
export const TransformerTextInput = forwardRef(
|
|
63
63
|
(
|
|
64
|
-
{
|
|
64
|
+
{
|
|
65
|
+
transformer,
|
|
66
|
+
onChangeText,
|
|
67
|
+
defaultValue,
|
|
68
|
+
...others
|
|
69
|
+
}: TransformerTextInputProps,
|
|
65
70
|
forwardedRef: Ref<TransformerTextInputInstance>,
|
|
66
71
|
) => {
|
|
67
72
|
const transformerId = useMemo(() => {
|
|
68
73
|
return registerTransformer(transformer);
|
|
69
74
|
}, [transformer]);
|
|
70
75
|
|
|
76
|
+
// Pre-transform defaultValue on the JS thread so Yoga measures the correct
|
|
77
|
+
// text from the start. Without this the native-side transformation happens
|
|
78
|
+
// after layout and doesn't trigger a remeasure.
|
|
79
|
+
const transformedDefaultValue = useMemo(() => {
|
|
80
|
+
if (defaultValue == null) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const result = transformer.worklet({
|
|
84
|
+
value: defaultValue,
|
|
85
|
+
previousValue: defaultValue,
|
|
86
|
+
selection: { start: defaultValue.length, end: defaultValue.length },
|
|
87
|
+
previousSelection: { start: 0, end: 0 },
|
|
88
|
+
});
|
|
89
|
+
return result?.value ?? defaultValue;
|
|
90
|
+
}, [defaultValue, transformer]);
|
|
91
|
+
|
|
71
92
|
useEffect(() => {
|
|
72
93
|
return () => {
|
|
73
94
|
unregisterTransformer(transformerId);
|
|
@@ -135,6 +156,7 @@ export const TransformerTextInput = forwardRef(
|
|
|
135
156
|
// @ts-expect-error
|
|
136
157
|
ref={inputRef}
|
|
137
158
|
onChangeText={handleChangeText}
|
|
159
|
+
defaultValue={transformedDefaultValue}
|
|
138
160
|
{...others}
|
|
139
161
|
/>
|
|
140
162
|
</TransformerTextInputDecoratorViewNativeComponent>
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Transformer } from '../Transformer';
|
|
2
|
+
|
|
3
|
+
export type CurrencyTransformerOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* ISO 4217 currency code, e.g. 'USD', 'EUR', 'JPY'. Drives the symbol and
|
|
6
|
+
* the number of fraction digits.
|
|
7
|
+
*/
|
|
8
|
+
currency: string;
|
|
9
|
+
/**
|
|
10
|
+
* BCP 47 locale tag, e.g. 'en-US', 'de-DE'. Drives separators and symbol
|
|
11
|
+
* position. Defaults to the runtime's default locale.
|
|
12
|
+
*/
|
|
13
|
+
locale?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export class CurrencyTransformer extends Transformer {
|
|
17
|
+
constructor(options: CurrencyTransformerOptions) {
|
|
18
|
+
const { currency, locale } = options;
|
|
19
|
+
|
|
20
|
+
const fractionDigits =
|
|
21
|
+
new Intl.NumberFormat(locale, {
|
|
22
|
+
style: 'currency',
|
|
23
|
+
currency,
|
|
24
|
+
}).resolvedOptions().maximumFractionDigits ?? 0;
|
|
25
|
+
const cacheKey = `${locale ?? ''}|${currency}`;
|
|
26
|
+
|
|
27
|
+
super(({ value, previousValue, selection }) => {
|
|
28
|
+
'worklet';
|
|
29
|
+
|
|
30
|
+
const isDigitAt = (s: string, i: number) => {
|
|
31
|
+
const c = s.charCodeAt(i);
|
|
32
|
+
return c >= 48 && c <= 57;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const prevValue = previousValue ?? value;
|
|
36
|
+
let digits = value.replace(/\D/g, '');
|
|
37
|
+
const prevDigits = prevValue.replace(/\D/g, '');
|
|
38
|
+
|
|
39
|
+
let cursorDigitIndex = 0;
|
|
40
|
+
for (let i = 0; i < selection.start && i < value.length; i++) {
|
|
41
|
+
if (isDigitAt(value, i)) cursorDigitIndex++;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Backspace next to a separator doesn't change the digit count, so the
|
|
45
|
+
// formatter would re-add it. Drop the digit before the cursor so the
|
|
46
|
+
// keystroke has a visible effect (drops the last digit when the cursor
|
|
47
|
+
// sits past the end of the digits).
|
|
48
|
+
if (
|
|
49
|
+
digits.length === prevDigits.length &&
|
|
50
|
+
value.length < prevValue.length &&
|
|
51
|
+
cursorDigitIndex > 0
|
|
52
|
+
) {
|
|
53
|
+
digits =
|
|
54
|
+
digits.slice(0, cursorDigitIndex - 1) +
|
|
55
|
+
digits.slice(cursorDigitIndex);
|
|
56
|
+
cursorDigitIndex -= 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Treat all-zero digits as empty so leading zeros and the last-cent
|
|
60
|
+
// backspace both clear the input cleanly.
|
|
61
|
+
const amount = parseInt(digits, 10) / 10 ** fractionDigits;
|
|
62
|
+
if (!digits || amount === 0) {
|
|
63
|
+
return { value: '', selection: { start: 0, end: 0 } };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const cache: Record<string, Intl.NumberFormat> =
|
|
67
|
+
(
|
|
68
|
+
globalThis as unknown as {
|
|
69
|
+
__currencyFormatters?: Record<string, Intl.NumberFormat>;
|
|
70
|
+
}
|
|
71
|
+
).__currencyFormatters ?? {};
|
|
72
|
+
let formatter = cache[cacheKey];
|
|
73
|
+
if (!formatter) {
|
|
74
|
+
formatter = new Intl.NumberFormat(locale, {
|
|
75
|
+
style: 'currency',
|
|
76
|
+
currency,
|
|
77
|
+
});
|
|
78
|
+
cache[cacheKey] = formatter;
|
|
79
|
+
(
|
|
80
|
+
globalThis as unknown as {
|
|
81
|
+
__currencyFormatters?: Record<string, Intl.NumberFormat>;
|
|
82
|
+
}
|
|
83
|
+
).__currencyFormatters = cache;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const formatted = formatter.format(amount);
|
|
87
|
+
|
|
88
|
+
// Map cursor to "right after the Nth digit in formatted" where N is
|
|
89
|
+
// the user's digit-cursor in the raw value, adjusted for the digit
|
|
90
|
+
// count diff between raw and formatted. parseInt may strip leading
|
|
91
|
+
// zeros (raw `0123` → formatted `123`) and the formatter may pad
|
|
92
|
+
// with cents zeros (raw `1` → formatted `001`); the diff captures
|
|
93
|
+
// both so the cursor stays right after whatever digit the user just
|
|
94
|
+
// typed (or to the right of the dropped digit, on backspace).
|
|
95
|
+
let formattedDigitsCount = 0;
|
|
96
|
+
for (let i = 0; i < formatted.length; i++) {
|
|
97
|
+
if (isDigitAt(formatted, i)) formattedDigitsCount++;
|
|
98
|
+
}
|
|
99
|
+
const targetDigit =
|
|
100
|
+
cursorDigitIndex + (formattedDigitsCount - digits.length);
|
|
101
|
+
|
|
102
|
+
let newCursor = formatted.length;
|
|
103
|
+
if (targetDigit >= formattedDigitsCount) {
|
|
104
|
+
// Past the last digit — snap to right after the last digit so the
|
|
105
|
+
// cursor sits at the end of the amount (excludes any trailing
|
|
106
|
+
// symbol like " €").
|
|
107
|
+
for (let i = formatted.length - 1; i >= 0; i--) {
|
|
108
|
+
if (isDigitAt(formatted, i)) {
|
|
109
|
+
newCursor = i + 1;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} else if (targetDigit <= 0) {
|
|
114
|
+
// Before the first digit — snap to the first digit so the next
|
|
115
|
+
// keystroke lands inside the number rather than before any
|
|
116
|
+
// prefix symbol.
|
|
117
|
+
for (let i = 0; i < formatted.length; i++) {
|
|
118
|
+
if (isDigitAt(formatted, i)) {
|
|
119
|
+
newCursor = i;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
let seen = 0;
|
|
125
|
+
for (let i = 0; i < formatted.length; i++) {
|
|
126
|
+
if (isDigitAt(formatted, i)) {
|
|
127
|
+
seen++;
|
|
128
|
+
if (seen === targetDigit) {
|
|
129
|
+
newCursor = i + 1;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
value: formatted,
|
|
138
|
+
selection: { start: newCursor, end: newCursor },
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|