Skip to content

Commit 78f3175

Browse files
committed
Parameterize mask for any locale.
1 parent 23731dc commit 78f3175

File tree

1 file changed

+13
-67
lines changed

1 file changed

+13
-67
lines changed

src/TextInputMask.tsx

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
6910
function TextInputWithMask(
7011
{
7112
onChangeText,
@@ -82,15 +23,26 @@ function TextInputWithMask(
8223
)
8324

8425
const onInnerChange = (text: string) => {
26+
const splitCharacter = detectCharacter(mask)
27+
const maskParts = mask.split(splitCharacter)
28+
8529
let trimmedText = text.trim()
86-
const format = 'mm/dd/yyyy'
30+
const format =
31+
maskParts[0].toLowerCase() +
32+
splitCharacter +
33+
maskParts[1].toLowerCase() +
34+
splitCharacter +
35+
maskParts[2].toLowerCase()
8736
const match = new RegExp(
8837
format
8938
.replace(/(\w+)\W(\w+)\W(\w+)/, '^\\s*($1)\\W*($2)?\\W*($3)?([0-9]*).*')
9039
.replace(/m|d|y/g, '\\d')
9140
)
9241
const replaceValue = format.match(/\W/)
93-
const replace = '$1/$2/$3$4'.replace(/\//g, (replaceValue ?? '') as string)
42+
const replace = `$1${splitCharacter}$2${splitCharacter}$3$4`.replace(
43+
new RegExp(splitCharacter, 'g'),
44+
(replaceValue ?? '') as string
45+
)
9446

9547
const isBackSpace = controlledValue.length > trimmedText.length
9648

@@ -108,11 +60,6 @@ function TextInputWithMask(
10860
setControlledValue(trimmedText)
10961
}
11062

111-
const onInnerBlur = () => {
112-
const enhancedText = enhanceTextWithMask(value, mask, controlledValue)
113-
setControlledValue(enhancedText)
114-
}
115-
11663
React.useEffect(() => {
11764
setControlledValue(value || '')
11865
}, [value])
@@ -128,7 +75,6 @@ function TextInputWithMask(
12875
onChange && onChange(e)
12976
}}
13077
maxLength={10}
131-
onBlur={onInnerBlur}
13278
/>
13379
)
13480
}

0 commit comments

Comments
 (0)