@@ -118,17 +118,25 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
118
118
119
119
useEffect ( ( ) => {
120
120
if ( eventKeyPressRef . current && props . mask && inputRef ?. current && value ) {
121
+ const { start, end } = eventKeyPressRef . current . cursor ;
122
+ // if multiple digits are selected, recovery the selected area
123
+ const area = Math . abs ( start - end ) ;
124
+ // this the mask representation of the selected area
125
+ const selected = props . mask . slice ( start , end ) ;
126
+ // match the mask representation with the mask character
127
+ const maskChart = selected . match ( / # / g) ;
128
+ // calculate the difference between the selected area and the mask representation
129
+ const diffStart = maskChart ? Math . abs ( maskChart . length - area ) : 0 ;
130
+ const diffEnd = maskChart ? maskChart . length : 0 ;
131
+
121
132
const position = getPosition (
122
133
eventKeyPressRef . current . key ,
123
134
value ,
124
135
eventKeyPressRef . current . cursor ,
125
136
positionRef . current
126
137
) ;
127
138
inputRef . current . focus ( ) ;
128
- inputRef . current . setSelectionRange (
129
- eventKeyPressRef . current . cursor . start + position ,
130
- eventKeyPressRef . current . cursor . end + position
131
- ) ;
139
+ inputRef . current . setSelectionRange ( start + diffStart + position , end - diffEnd + position ) ;
132
140
}
133
141
} , [ value ] ) ;
134
142
@@ -290,47 +298,53 @@ export const useInput = (props: ParamsTypeInputHook): ReturnTypeInputHook => {
290
298
key,
291
299
currentTarget : { selectionStart, selectionEnd } ,
292
300
} = event ;
293
- let start = selectionStart ;
294
- let end = selectionEnd ;
301
+ let start = selectionStart ?? 0 ;
302
+ let end = selectionEnd ?? 0 ;
303
+
304
+ const isSelected = Math . abs ( start - end ) > 0 ;
295
305
296
306
// Check if the input has a mask
297
307
if ( props . mask && inputRef . current && start !== null && end !== null ) {
298
308
// Check if the key pressed is "Backspace" or "Delete"
299
309
// Check if the character at the cursor position is a mask character
300
310
if ( key === BACKSPACE . key && props . mask [ start - 1 ] && props . mask [ start - 1 ] !== '#' ) {
301
- let count = 1 ;
302
- // Check if the character at the cursor position is a mask character
303
- for ( let index = start - 1 ; index >= 0 ; index -- ) {
304
- if ( props . mask [ index ] === '#' ) {
305
- count = start - 1 - index ;
306
- break ;
311
+ if ( ! isSelected ) {
312
+ let count = 1 ;
313
+ // Check if the character at the cursor position is a mask character
314
+ for ( let index = start - 1 ; index >= 0 ; index -- ) {
315
+ if ( props . mask [ index ] === '#' ) {
316
+ count = start - 1 - index ;
317
+ break ;
318
+ }
307
319
}
320
+ // Calculate the new cursor position
321
+ start = start - count ;
322
+ end = end - count ;
308
323
}
309
- // Calculate the new cursor position
310
- start = start - count ;
311
- end = end - count ;
312
324
// Set the cursor to the new position
313
325
inputRef . current . setSelectionRange ( start , end ) ;
314
326
} else if ( key === DELETE . key && props . mask [ end ] && props . mask [ end ] !== '#' ) {
315
- let count = 1 ;
316
- // Check if the character at the cursor position is a mask character
317
- for ( let index = end ; index < props . mask . length ; index ++ ) {
318
- if ( props . mask [ index ] === '#' ) {
319
- count = index - end ;
320
- break ;
327
+ if ( ! isSelected ) {
328
+ let count = 1 ;
329
+ // Check if the character at the cursor position is a mask character
330
+ for ( let index = end ; index < props . mask . length ; index ++ ) {
331
+ if ( props . mask [ index ] === '#' ) {
332
+ count = index - end ;
333
+ break ;
334
+ }
321
335
}
336
+ // Calculate the new cursor position
337
+ start = start + count ;
338
+ end = end + count ;
322
339
}
323
- // Calculate the new cursor position
324
- start = start + count ;
325
- end = end + count ;
326
340
// Set the cursor to the new position
327
341
inputRef . current . setSelectionRange ( start , end ) ;
328
342
}
329
343
}
330
344
331
345
const cursor = {
332
- start : start ?? 0 ,
333
- end : end ?? 0 ,
346
+ start : start ,
347
+ end : end ,
334
348
} ;
335
349
eventKeyPressRef . current = { key, cursor } ;
336
350
props . onKeyDown ?.( event ) ;
0 commit comments