-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
250 lines (229 loc) · 6.93 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import React, {
DependencyList,
forwardRef,
useEffect,
useImperativeHandle,
useRef,
useState
} from 'react'
import { findNodeHandle, NativeModules, Platform, TextInput, TextInputProps } from 'react-native'
const { RNTextInputMask } = NativeModules as { RNTextInputMask: MaskOperations }
if (!RNTextInputMask) {
throw new Error(`NativeModule: RNTextInputMask is null.
To fix this issue try these steps:
• Rebuild and restart the app.
• Run the packager with \`--clearCache\` flag.
• If happening on iOS, run \`pod install\` in the \`ios\` directory and then rebuild and re-run the app.
• If this happens while testing with Jest, make sure to follow instructions in https://github.com/react-native-text-input-mask/react-native-text-input-mask#testing
`);
}
export const { mask, unmask, setMask } = RNTextInputMask
const TextInputMask = forwardRef<Handles, TextInputMaskProps>(({
mask: primaryFormat,
defaultValue,
value ,
multiline,
onChangeText,
affineFormats,
customNotations,
affinityCalculationStrategy,
autocomplete= true,
autoskip = true,
rightToLeft,
...rest
}, ref) => {
const input = useRef<TextInput>(null)
const [ maskedValue, setMaskedValue ] = useState<string>()
useEffectAsync(async () => {
const initialValue = value ?? defaultValue
if (!initialValue) return
if (primaryFormat) {
const masked = await mask(primaryFormat, initialValue, false)
setMaskedValue(masked)
} else {
setMaskedValue(initialValue)
}
}, [])
useEffectAsync(async () => {
if (value === maskedValue) return
if (primaryFormat && value) {
const masked = await mask(primaryFormat, value, false)
setMaskedValue(masked)
} else {
setMaskedValue(value)
}
}, [value])
useEffect(() => {
const nodeId = findNodeHandle(input.current)
if (primaryFormat && nodeId) {
setMask(nodeId, primaryFormat, { affineFormats, affinityCalculationStrategy, customNotations, autocomplete, autoskip, rightToLeft })
}
}, [primaryFormat])
useImperativeHandle(ref, () => ({
focus: () => {
input.current?.focus()
},
blur: () => {
input.current?.blur()
}
}))
return (
<TextInput
{...rest}
ref={input}
value={maskedValue}
multiline={primaryFormat && Platform.OS === 'ios' ? false : multiline}
onChangeText={async (masked) => {
setMaskedValue(masked)
if (primaryFormat) {
const unmasked = await unmask(primaryFormat, masked, true)
onChangeText?.(masked, unmasked)
} else {
onChangeText?.(masked)
}
}}
/>
)
})
export const useEffectAsync = (
operation: () => Promise<void>,
deps?: DependencyList
) => {
useEffect(() => {
operation().then()
}, deps)
}
interface MaskOperations {
mask: (mask: string, value: string, autocomplete: boolean) => Promise<string>,
unmask: (mask: string, value: string, autocomplete: boolean) => Promise<string>
setMask: (reactNode: number, primaryFormat: string, options?: MaskOptions) => void
}
interface MaskOptions {
affineFormats?: string[]
customNotations?: Notation[]
affinityCalculationStrategy?: AffinityCalculationStrategy
/**
* autocomplete pattern while editing text
*/
autocomplete?: boolean
/**
* automatically remove mask characters on backspace
*/
autoskip?: boolean
rightToLeft?: boolean
}
type AffinityCalculationStrategy =
/**
* Default strategy.
*
* Uses ```Mask``` built-in mechanism to calculate total affinity between the text and the mask format.
*
* For example:
* ```
* format: [00].[00]
*
* input1: 1234
* input2: 12.34
* input3: 1.234
*
* affinity1 = 4 (symbols) - 1 (missed dot) = 3
* affinity2 = 5 (symbols) = 5
* affinity3 = 5 (symbols) - 1 (superfluous dot) - 1 (missed dot) = 3
* ```
*/
'WHOLE_STRING' |
/**
* Finds the longest common prefix between the original text and the same text after applying the mask.
*
* For example:
* ```
* format1: +7 [000] [000]
* format2: 8 [000] [000]
*
* input: +7 12 345
* affinity1 = 5
* affinity2 = 0
*
* input: 8 12 345
* affinity1 = 0
* affinity2 = 4
* ```
*/
'PREFIX' |
/**
* Affinity is tolerance between the length of the input and the total amount of text current mask can accommodate.
*
* If current mask can't accommodate all the text, the affinity equals `Int.min`.
*
* For example:
* ```
* format1: [00]-[0]
* format2: [00]-[000]
* format3: [00]-[00000]
*
* input affinity1 affinity2 affinity3
* 1 -3 -5 -7
* 12 -2 -4 -6
* 123 -1 -3 -5
* 12-3 0 -2 -4
* 1234 0 -2 -4
* 12345 Int.MIN_VALUE -1 -3
* 123456 Int.MIN_VALUE 0 -2
* ```
*
* This affinity calculation strategy comes in handy when the mask format radically changes depending on the input
* length.
*
* N.B.: Make sure the widest mask format is the primary mask format.
*/
'CAPACITY' |
/**
* Affinity is tolerance between the length of the extracted value and the total extracted value length current mask can accommodate.
*
* If current mask can't accommodate all the text, the affinity equals `Int.min`.
*
* For example:
* ```
* format1: [00]-[0]
* format2: [00]-[000]
* format3: [00]-[00000]
*
* input affinity1 affinity2 affinity3
* 1 -2 -4 -6
* 12 -1 -3 -5
* 123 0 -2 -4
* 12-3 0 -2 -4
* 1234 Int.MIN_VALUE -1 -3
* 12345 Int.MIN_VALUE 0 -2
* 123456 Int.MIN_VALUE Int.MIN_VALUE -1
* ```
*
* This affinity calculation strategy comes in handy when the mask format radically changes depending on the value
* length.
*
* N.B.: Make sure the widest mask format is the primary mask format.
*/
'EXTRACTED_VALUE_CAPACITY'
interface Notation {
/**
* A symbol in format string.
*/
character: string,
/**
* An associated character set of acceptable input characters.
*/
characterSet: string,
/**
* Is it an optional symbol or mandatory?
*/
isOptional: boolean
}
export interface TextInputMaskProps extends TextInputProps, MaskOptions{
mask?: string
onChangeText?: (formatted: string, extracted?: string) => void
}
interface Handles {
focus: () => void
blur: () => void
}
export default TextInputMask