@@ -50,7 +50,7 @@ export interface UseMaskOptions {
5050 /**
5151 * Reference to the mask element.
5252 */
53- inputRef : React . RefObject < { elementRef : React . RefObject < HTMLInputElement > } | null > ;
53+ target : HTMLInputElement ;
5454}
5555
5656export interface UseMaskExposes {
@@ -103,7 +103,7 @@ export interface UseMaskExposes {
103103 * const { onMaskInput, onMaskKeyDown, onMaskKeyPress, onMaskFocus, onMaskBlur, onMaskPaste } = useMask({
104104 * mask: '99/99/9999',
105105 * onMaskChange: (event: UseMaskChangeEvent) => setValue(event.value ?? ''),
106- * inputRef : ref
106+ * target : ref
107107 * });
108108 *
109109 * return (
@@ -113,7 +113,7 @@ export interface UseMaskExposes {
113113 * );
114114 */
115115export function useMask ( options : UseMaskOptions ) : UseMaskExposes {
116- const { mask, unmask, slotChar = '_' , autoClear = true , readOnly = false , onMaskChange, inputRef } = options ;
116+ const { mask, unmask, slotChar = '_' , autoClear = true , readOnly = false , onMaskChange, target } = options ;
117117
118118 const len = React . useRef ( 0 ) ;
119119 const tests = React . useRef < Array < RegExp | null > > ( [ ] ) ;
@@ -149,7 +149,7 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
149149 let pos , begin , end ;
150150 const iPhone = / i p h o n e / i. test ( getUserAgent ( ) ) ;
151151
152- oldVal . current = inputRef . current ?. elementRef . current ?. value as string ;
152+ oldVal . current = target ?. value as string ;
153153
154154 //backspace, delete, and escape get special treatment
155155 if ( k === 'Backspace' || k === 'Delete' || ( iPhone && k === 'Escape' ) ) {
@@ -174,12 +174,12 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
174174 event . preventDefault ( ) ;
175175 } else if ( k === 'Enter' ) {
176176 // enter
177- inputRef . current ?. elementRef . current ? .blur ( ) ;
177+ target . blur ( ) ;
178178 updateModelValue ( ( event . target as HTMLInputElement ) . value ) ;
179179 } else if ( k === 'Escape' ) {
180180 // escape
181- if ( inputRef . current ?. elementRef . current ) {
182- inputRef . current . elementRef . current . value = focusText . current ?? '' ;
181+ if ( target ) {
182+ target . value = focusText . current ?? '' ;
183183 }
184184
185185 caret ( 0 , checkVal ( ) ) ;
@@ -251,19 +251,19 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
251251 }
252252
253253 focus . current = true ;
254- focusText . current = inputRef . current ?. elementRef . current ? .value as string ;
254+ focusText . current = target . value as string ;
255255
256- if ( ! inputRef . current ?. elementRef . current ?. value || inputRef . current . elementRef . current . value === defaultBuffer . current ) {
256+ if ( ! target . value || target . value === defaultBuffer . current ) {
257257 requestAnimationFrame ( ( ) => {
258- if ( inputRef . current ?. elementRef . current === document . activeElement ) {
258+ if ( target === document . activeElement ) {
259259 caret ( 0 , 0 ) ;
260260 }
261261 } ) ;
262262 } else {
263263 const pos = checkVal ( ) ;
264264
265265 caretTimeoutId . current = setTimeout ( ( ) => {
266- if ( inputRef . current ?. elementRef . current !== document . activeElement ) {
266+ if ( target !== document . activeElement ) {
267267 return ;
268268 }
269269
@@ -283,10 +283,10 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
283283 checkVal ( ) ;
284284 updateModelValue ( event . target . value ) ;
285285
286- if ( inputRef . current ?. elementRef . current ? .value !== focusText . current ) {
286+ if ( target . value !== focusText . current ) {
287287 const e = new Event ( 'change' , { bubbles : true , cancelable : false } ) ;
288288
289- inputRef . current ?. elementRef . current ? .dispatchEvent ( e ) ;
289+ target . dispatchEvent ( e ) ;
290290 }
291291 } ;
292292
@@ -302,11 +302,11 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
302302 begin = first ;
303303 end = typeof last === 'number' ? last : begin ;
304304
305- inputRef . current ?. elementRef . current ? .setSelectionRange ( begin , end ) ;
305+ target . setSelectionRange ( begin , end ) ;
306306 } else {
307- if ( inputRef . current ?. elementRef . current ) {
308- begin = inputRef . current . elementRef . current . selectionStart ?? 0 ;
309- end = inputRef . current . elementRef . current . selectionEnd ?? 0 ;
307+ if ( target ) {
308+ begin = target . selectionStart ?? 0 ;
309+ end = target . selectionEnd ?? 0 ;
310310 }
311311 }
312312
@@ -376,7 +376,7 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
376376 } ;
377377
378378 const handleAndroidInput = ( ) => {
379- const curVal = inputRef . current ?. elementRef . current ? .value as string ;
379+ const curVal = target . value as string ;
380380 const pos = caret ( ) ;
381381
382382 if ( ! pos ) {
@@ -412,14 +412,14 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
412412 } ;
413413
414414 const writeBuffer = ( ) => {
415- if ( inputRef . current ?. elementRef . current ) {
416- inputRef . current . elementRef . current . value = buffer . current . join ( '' ) ;
415+ if ( target ) {
416+ target . value = buffer . current . join ( '' ) ;
417417 }
418418 } ;
419419
420420 const checkVal = ( allow = false ) : number => {
421421 //try to place characters where they belong
422- const test = inputRef . current ?. elementRef . current ? .value ;
422+ const test = target . value ;
423423 let lastMatch = - 1 ,
424424 i ,
425425 c ,
@@ -462,8 +462,8 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
462462 if ( autoClear || buffer . current . join ( '' ) === defaultBuffer . current ) {
463463 // Invalid value. Remove it and replace it with the
464464 // mask, which is the default behavior.
465- if ( inputRef . current ?. elementRef . current ? .value ) {
466- inputRef . current . elementRef . current . value = '' ;
465+ if ( target . value ) {
466+ target . value = '' ;
467467 }
468468
469469 clearBuffer ( 0 , len . current ) ;
@@ -475,8 +475,8 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
475475 } else {
476476 writeBuffer ( ) ;
477477
478- if ( inputRef . current ?. elementRef . current ) {
479- inputRef . current . elementRef . current . value = inputRef . current . elementRef . current . value . substring ( 0 , lastMatch + 1 ) ;
478+ if ( target ) {
479+ target . value = target . value . substring ( 0 , lastMatch + 1 ) ;
480480 }
481481 }
482482
@@ -519,8 +519,8 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
519519
520520 currentVal . current = value ;
521521
522- if ( inputRef . current ?. elementRef . current ) {
523- inputRef . current . elementRef . current . value = finalValue ;
522+ if ( target ) {
523+ target . value = finalValue ;
524524 }
525525
526526 // Call the onMaskChange callback to update React state
@@ -530,9 +530,9 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
530530 } ;
531531
532532 const updateValue = ( updateModel = true ) => {
533- if ( inputRef . current ?. elementRef . current ) {
534- if ( inputRef . current . elementRef . current . value == null ) {
535- inputRef . current . elementRef . current . value = '' ;
533+ if ( target ) {
534+ if ( target . value == null ) {
535+ target . value = '' ;
536536
537537 if ( updateModel ) {
538538 updateModelValue ( '' ) ;
@@ -541,16 +541,16 @@ export function useMask(options: UseMaskOptions): UseMaskExposes {
541541 checkVal ( ) ;
542542
543543 setTimeout ( ( ) => {
544- if ( inputRef . current ?. elementRef . current ) {
544+ if ( target ) {
545545 writeBuffer ( ) ;
546546 checkVal ( ) ;
547547
548- if ( updateModel ) updateModelValue ( inputRef . current . elementRef . current . value ) ;
548+ if ( updateModel ) updateModelValue ( target . value ) ;
549549 }
550550 } , 10 ) ;
551551 }
552552
553- focusText . current = inputRef . current . elementRef . current . value ;
553+ focusText . current = target . value ;
554554 }
555555 } ;
556556
0 commit comments