Skip to content

Commit 23731dc

Browse files
committed
Introduce auto slashes to text input.
1 parent 1c84b45 commit 23731dc

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

src/TextInputMask.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,30 @@ function TextInputWithMask(
8282
)
8383

8484
const onInnerChange = (text: string) => {
85-
if (text.length === mask.length) {
86-
onChangeText && onChangeText(text)
85+
let trimmedText = text.trim()
86+
const format = 'mm/dd/yyyy'
87+
const match = new RegExp(
88+
format
89+
.replace(/(\w+)\W(\w+)\W(\w+)/, '^\\s*($1)\\W*($2)?\\W*($3)?([0-9]*).*')
90+
.replace(/m|d|y/g, '\\d')
91+
)
92+
const replaceValue = format.match(/\W/)
93+
const replace = '$1/$2/$3$4'.replace(/\//g, (replaceValue ?? '') as string)
94+
95+
const isBackSpace = controlledValue.length > trimmedText.length
96+
97+
if (!isBackSpace) {
98+
trimmedText = trimmedText
99+
.replace(/(^|\W)(?=\d\W)/g, '$10')
100+
.replace(match, replace)
101+
.replace(/(\W)+/g, '$1')
87102
}
88-
setControlledValue(text)
103+
104+
if (trimmedText.length === mask.length) {
105+
onChangeText && onChangeText(trimmedText)
106+
}
107+
108+
setControlledValue(trimmedText)
89109
}
90110

91111
const onInnerBlur = () => {
@@ -107,6 +127,7 @@ function TextInputWithMask(
107127
onChange={(e) => {
108128
onChange && onChange(e)
109129
}}
130+
maxLength={10}
110131
onBlur={onInnerBlur}
111132
/>
112133
)

src/__tests__/Date/__snapshots__/AnimatedCrossView.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,6 +3652,7 @@ exports[`renders collapsed AnimatedCrossView 1`] = `
36523652
keyboardAppearance="default"
36533653
keyboardType="number-pad"
36543654
maxFontSizeMultiplier={1.5}
3655+
maxLength={10}
36553656
multiline={false}
36563657
onBlur={[Function]}
36573658
onChange={[Function]}

src/__tests__/Date/__snapshots__/CalendarEdit.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ exports[`renders CalendarEdit 1`] = `
174174
keyboardAppearance="default"
175175
keyboardType="number-pad"
176176
maxFontSizeMultiplier={1.5}
177+
maxLength={10}
177178
multiline={false}
178179
onBlur={[Function]}
179180
onChange={[Function]}

src/__tests__/Date/__snapshots__/DatePickerInput.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ exports[`renders DatePickerInput 1`] = `
168168
keyboardAppearance="default"
169169
keyboardType="number-pad"
170170
maxFontSizeMultiplier={1.5}
171+
maxLength={10}
171172
multiline={false}
172173
onBlur={[Function]}
173174
onChange={[Function]}

src/__tests__/Date/__snapshots__/DatePickerInputWithoutModal.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ exports[`renders DatePickerInput 1`] = `
167167
keyboardAppearance="default"
168168
keyboardType="number-pad"
169169
maxFontSizeMultiplier={1.5}
170+
maxLength={10}
170171
multiline={false}
171172
onBlur={[Function]}
172173
onChange={[Function]}

0 commit comments

Comments
 (0)