@@ -52,38 +52,62 @@ export const MaskedTextInputComponent: ForwardRefRenderFunction<
52
52
} ,
53
53
style
54
54
]
55
+
56
+ const isMasked = ( ) => pattern || type === 'currency'
57
+
55
58
const getMaskedValue = ( value : string ) =>
56
59
mask ( value , pattern , type , options , autoCapitalize )
60
+
57
61
const getUnMaskedValue = ( value : string ) =>
58
62
unMask ( value , type as 'custom' | 'currency' )
59
63
64
+ const updateStatesWithMasking = ( inputValue : string ) => {
65
+ const newUnMaskedValue = getUnMaskedValue ( inputValue )
66
+ const newMaskedValue = getMaskedValue ( newUnMaskedValue )
67
+
68
+ setMaskedValue ( newMaskedValue )
69
+ setUnmaskedValue ( newUnMaskedValue )
70
+ setRawValue ( inputValue )
71
+ }
72
+
73
+ const updateStatesWithoutMasking = ( inputValue : string ) => {
74
+ setMaskedValue ( inputValue )
75
+ setUnmaskedValue ( inputValue )
76
+ setRawValue ( inputValue )
77
+ }
78
+
79
+ const clearAllStates = ( ) => {
80
+ setMaskedValue ( '' )
81
+ setUnmaskedValue ( '' )
82
+ setRawValue ( '' )
83
+ }
84
+
60
85
const defaultValueCustom = defaultValue || ''
61
86
const defaultValueCurrency = defaultValue || '0'
87
+ const initialRawValue = value
62
88
63
- const initialRawValue = value ;
89
+ const initialMaskedValue = isMasked ( )
90
+ ? getMaskedValue ( type === 'currency' ? defaultValueCurrency : defaultValueCustom )
91
+ : ( value || defaultValueCustom )
64
92
65
- const initialMaskedValue = getMaskedValue (
66
- type === 'currency' ? defaultValueCurrency : defaultValueCustom
67
- )
68
-
69
- const initialUnMaskedValue = getUnMaskedValue (
70
- type === 'currency' ? defaultValueCurrency : defaultValueCustom
71
- )
93
+ const initialUnMaskedValue = isMasked ( )
94
+ ? getUnMaskedValue ( type === 'currency' ? defaultValueCurrency : defaultValueCustom )
95
+ : ( value || defaultValueCustom )
72
96
73
97
const [ maskedValue , setMaskedValue ] = useState ( initialMaskedValue )
74
98
const [ unMaskedValue , setUnmaskedValue ] = useState ( initialUnMaskedValue )
75
- const [ rawValue , setRawValue ] = useState ( initialRawValue ) ;
99
+ const [ rawValue , setRawValue ] = useState ( initialRawValue )
76
100
const [ isInitialRender , setIsInitialRender ] = useState ( true )
77
101
78
- const actualValue = pattern || type === "currency" ? maskedValue : rawValue ;
102
+ const actualValue = isMasked ( ) ? maskedValue : rawValue
79
103
80
- function onChange ( value : string ) {
81
- const newUnMaskedValue = unMask ( value , type as 'custom' | 'currency' )
82
- const newMaskedValue = mask ( newUnMaskedValue , pattern , type , options )
83
104
84
- setMaskedValue ( newMaskedValue )
85
- setUnmaskedValue ( newUnMaskedValue )
86
- setRawValue ( value ) ;
105
+ const handleChange = ( inputValue : string ) => {
106
+ if ( isMasked ( ) ) {
107
+ updateStatesWithMasking ( inputValue )
108
+ } else {
109
+ updateStatesWithoutMasking ( inputValue )
110
+ }
87
111
}
88
112
89
113
useEffect ( ( ) => {
@@ -92,23 +116,35 @@ export const MaskedTextInputComponent: ForwardRefRenderFunction<
92
116
return
93
117
}
94
118
95
- onChangeText ( maskedValue , unMaskedValue )
96
- } , [ maskedValue , unMaskedValue ] )
119
+ if ( isMasked ( ) ) {
120
+ onChangeText ( maskedValue , unMaskedValue )
121
+ } else {
122
+ onChangeText ( rawValue || '' , rawValue || '' )
123
+ }
124
+ } , [ maskedValue , unMaskedValue , rawValue ] )
97
125
98
126
useEffect ( ( ) => {
99
127
if ( value ) {
100
- setMaskedValue ( getMaskedValue ( value ) )
101
- setUnmaskedValue ( getUnMaskedValue ( value ) )
128
+ if ( isMasked ( ) ) {
129
+ setMaskedValue ( getMaskedValue ( value ) )
130
+ setUnmaskedValue ( getUnMaskedValue ( value ) )
131
+ } else {
132
+ updateStatesWithoutMasking ( value )
133
+ }
102
134
} else {
103
- setMaskedValue ( initialMaskedValue )
104
- setUnmaskedValue ( initialUnMaskedValue )
135
+ if ( isMasked ( ) ) {
136
+ setMaskedValue ( initialMaskedValue )
137
+ setUnmaskedValue ( initialUnMaskedValue )
138
+ } else {
139
+ clearAllStates ( )
140
+ }
105
141
}
106
142
} , [ value ] )
107
143
108
144
return (
109
145
< >
110
146
< TextInput
111
- onChangeText = { ( value ) => onChange ( value ) }
147
+ onChangeText = { handleChange }
112
148
ref = { ref }
113
149
maxLength = { pattern . length || undefined }
114
150
autoCapitalize = { autoCapitalize }
0 commit comments