-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (68 loc) · 1.87 KB
/
index.js
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
import React, { useState, useEffect, useRef } from "react";
import {
TextInput,
findNodeHandle,
NativeModules,
Platform,
} from "react-native";
const mask = NativeModules.RNTextInputMask.mask;
const unmask = NativeModules.RNTextInputMask.unmask;
const setMask = NativeModules.RNTextInputMask.setMask;
export { mask, unmask, setMask };
const TextInputMask = (props) => {
const [masked, setMasked] = useState(false);
const input = useRef();
const multilineOSCheck = Platform.OS === "ios" ? false : props.multiline;
useEffect(() => {
// Component Mount
if (props.maskDefaultValue && props.mask && props.value) {
mask(
props.mask,
"" + props.value,
(text) => input.current && input.current.setNativeProps({ text })
);
}
if (props.mask && !masked) {
setMasked(true);
setMask(findNodeHandle(input.current), props.mask);
}
}, []);
useEffect(() => {
// Receive Props
mask(
props.mask,
"" + props.value,
(text) => input.current && input.current.setNativeProps({ text })
);
}, [props.value]);
useEffect(() => {
// Change Mask
setMask(findNodeHandle(input.current), props.mask);
}, [props.mask]);
return (
<TextInput
{...props}
value={undefined}
ref={(ref) => {
input.current = ref;
if (typeof props.refInput === "function") {
props.refInput(ref);
}
}}
multiline={props.mask && multilineOSCheck}
onChangeText={(masked) => {
if (props.mask) {
const _unmasked = unmask(props.mask, masked, (unmasked) => {
props.onChangeText && props.onChangeText(masked, unmasked);
});
} else {
props.onChangeText && props.onChangeText(masked);
}
}}
/>
);
};
TextInputMask.defaultProps = {
maskDefaultValue: true,
};
export default TextInputMask;