@@ -7,65 +7,6 @@ function detectCharacter(mask: string): string {
77 return c || ''
88}
99
10- function enhanceTextWithMask (
11- text : string ,
12- mask : string ,
13- previousValue : string
14- ) : string {
15- const isBackSpace = previousValue . length > text . length
16- const splitCharacter = detectCharacter ( mask )
17-
18- const maskParts = mask . split ( splitCharacter )
19- const textParts = text . split ( splitCharacter )
20-
21- let finalString : string [ ] = [ ]
22- for ( let maskPartIndex = 0 ; maskPartIndex < mask . length ; maskPartIndex ++ ) {
23- let partString : string [ ] = [ ]
24-
25- const maskPart = maskParts [ maskPartIndex ]
26- const textPart = textParts [ maskPartIndex ]
27- if ( ! textPart ) {
28- continue
29- }
30-
31- for (
32- let maskDigitIndex = 0 ;
33- maskDigitIndex < maskPart . length ;
34- maskDigitIndex ++
35- ) {
36- const currentCharacter = textPart [ maskDigitIndex ]
37-
38- if ( isBackSpace && currentCharacter === undefined ) {
39- continue
40- }
41-
42- const character = textPart [ maskDigitIndex ]
43-
44- if ( character !== undefined ) {
45- partString . push ( character )
46- }
47- }
48-
49- finalString . push ( partString . join ( '' ) )
50- }
51-
52- const lastPart = finalString [ finalString . length - 1 ]
53- const lastMaskPart = maskParts [ finalString . length - 1 ]
54- if (
55- // if mask is completed
56- finalString . length !== maskParts . length &&
57- // or ...
58- lastPart &&
59- lastMaskPart &&
60- lastPart . length === lastMaskPart . length
61- ) {
62- return (
63- finalString . join ( splitCharacter ) + ( isBackSpace ? '' : splitCharacter )
64- )
65- }
66- return finalString . join ( splitCharacter )
67- }
68-
6910function TextInputWithMask (
7011 {
7112 onChangeText,
@@ -82,15 +23,41 @@ function TextInputWithMask(
8223 )
8324
8425 const onInnerChange = ( text : string ) => {
85- if ( text . length === mask . length ) {
86- onChangeText && onChangeText ( text )
26+ const splitCharacter = detectCharacter ( mask )
27+ const maskParts = mask . split ( splitCharacter )
28+
29+ let trimmedText = text . trim ( )
30+ const format =
31+ maskParts [ 0 ] . toLowerCase ( ) +
32+ splitCharacter +
33+ maskParts [ 1 ] . toLowerCase ( ) +
34+ splitCharacter +
35+ maskParts [ 2 ] . toLowerCase ( )
36+ const match = new RegExp (
37+ format
38+ . replace ( / ( \w + ) \W ( \w + ) \W ( \w + ) / , '^\\s*($1)\\W*($2)?\\W*($3)?([0-9]*).*' )
39+ . replace ( / m | d | y / g, '\\d' )
40+ )
41+ const replaceValue = format . match ( / \W / )
42+ const replace = `$1${ splitCharacter } $2${ splitCharacter } $3$4` . replace (
43+ new RegExp ( splitCharacter , 'g' ) ,
44+ ( replaceValue ?? '' ) as string
45+ )
46+
47+ const isBackSpace = controlledValue . length > trimmedText . length
48+
49+ if ( ! isBackSpace ) {
50+ trimmedText = trimmedText
51+ . replace ( / ( ^ | \W ) (? = \d \W ) / g, '$10' )
52+ . replace ( match , replace )
53+ . replace ( / ( \W ) + / g, '$1' )
54+ }
55+
56+ if ( trimmedText . length === mask . length ) {
57+ onChangeText && onChangeText ( trimmedText )
8758 }
88- setControlledValue ( text )
89- }
9059
91- const onInnerBlur = ( ) => {
92- const enhancedText = enhanceTextWithMask ( value , mask , controlledValue )
93- setControlledValue ( enhancedText )
60+ setControlledValue ( trimmedText )
9461 }
9562
9663 React . useEffect ( ( ) => {
@@ -107,7 +74,7 @@ function TextInputWithMask(
10774 onChange = { ( e ) => {
10875 onChange && onChange ( e )
10976 } }
110- onBlur = { onInnerBlur }
77+ maxLength = { 10 }
11178 />
11279 )
11380}
0 commit comments