react-native-transformer-text-input 0.1.0-alpha.4 → 0.1.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.
Files changed (30) hide show
  1. package/README.md +17 -1
  2. package/android/src/main/java/com/appandflow/transformertextinput/TransformerTextInputDecoratorView.kt +30 -0
  3. package/cpp/TransformerTextInputDecoratorViewShadowNode.cpp +24 -1
  4. package/ios/TransformerTextInputDecoratorView.mm +42 -3
  5. package/lib/module/TransformerTextInput.js +24 -0
  6. package/lib/module/TransformerTextInput.js.map +1 -1
  7. package/lib/module/formatters/currency.js +122 -0
  8. package/lib/module/formatters/currency.js.map +1 -0
  9. package/lib/module/formatters/phone-data.js +4844 -0
  10. package/lib/module/formatters/phone-data.js.map +1 -0
  11. package/lib/module/formatters/phone-number.js +323 -61
  12. package/lib/module/formatters/phone-number.js.map +1 -1
  13. package/lib/module/registry.js +4 -3
  14. package/lib/module/registry.js.map +1 -1
  15. package/lib/typescript/scripts/generate-phone-data.d.ts +7 -0
  16. package/lib/typescript/scripts/generate-phone-data.d.ts.map +1 -0
  17. package/lib/typescript/src/TransformerTextInput.d.ts.map +1 -1
  18. package/lib/typescript/src/formatters/currency.d.ts +17 -0
  19. package/lib/typescript/src/formatters/currency.d.ts.map +1 -0
  20. package/lib/typescript/src/formatters/phone-data.d.ts +15 -0
  21. package/lib/typescript/src/formatters/phone-data.d.ts.map +1 -0
  22. package/lib/typescript/src/formatters/phone-number.d.ts +10 -4
  23. package/lib/typescript/src/formatters/phone-number.d.ts.map +1 -1
  24. package/lib/typescript/src/registry.d.ts.map +1 -1
  25. package/package.json +14 -1
  26. package/src/TransformerTextInput.tsx +23 -1
  27. package/src/formatters/currency.ts +142 -0
  28. package/src/formatters/phone-data.ts +5665 -0
  29. package/src/formatters/phone-number.ts +326 -68
  30. package/src/registry.ts +4 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-transformer-text-input",
3
- "version": "0.1.0-alpha.4",
3
+ "version": "0.1.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,16 @@
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
+ },
28
+ "./formatters/phone-data": {
29
+ "source": "./src/formatters/phone-data.ts",
30
+ "types": "./lib/typescript/src/formatters/phone-data.d.ts",
31
+ "default": "./lib/module/formatters/phone-data.js"
32
+ },
23
33
  "./package.json": "./package.json"
24
34
  },
25
35
  "files": [
@@ -45,6 +55,7 @@
45
55
  "example": "yarn workspace react-native-transformer-text-input-example",
46
56
  "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
47
57
  "prepare": "bob build",
58
+ "generate:phone-data": "tsx scripts/generate-phone-data.ts",
48
59
  "test": "jest",
49
60
  "lint": "yarn lint:eslint && yarn lint:ts && yarn lint:android",
50
61
  "lint:ts": "tsc",
@@ -99,6 +110,7 @@
99
110
  "eslint-plugin-prettier": "^5.5.4",
100
111
  "jest": "^30.2.0",
101
112
  "lefthook": "^2.0.3",
113
+ "libphonenumber-js": "^1.12.38",
102
114
  "prettier": "^2.8.8",
103
115
  "react": "19.2.0",
104
116
  "react-native": "0.83.0",
@@ -106,6 +118,7 @@
106
118
  "react-native-worklets": "^0.7.1",
107
119
  "react-test-renderer": "19.2.0",
108
120
  "release-it": "^19.0.4",
121
+ "tsx": "^4.21.0",
109
122
  "turbo": "^2.5.6",
110
123
  "typescript": "^5.9.2"
111
124
  },
@@ -61,13 +61,34 @@ export type TransformerTextInputProps = Omit<TextInputProps, 'value'> & {
61
61
 
62
62
  export const TransformerTextInput = forwardRef(
63
63
  (
64
- { transformer, onChangeText, ...others }: TransformerTextInputProps,
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
+ }