-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5edecc6
commit 27f39de
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useOnClickOutside' | ||
export * from './useMask' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable no-param-reassign */ | ||
import { useCallback, useMemo } from 'react' | ||
|
||
import VMasker from 'vanilla-masker' | ||
|
||
export const useMask = (customPattern: string) => { | ||
const pattern = useMemo(() => customPattern, [customPattern]) | ||
|
||
const ref = useCallback( | ||
(instance: any) => { | ||
VMasker(instance).maskPattern(pattern) | ||
}, | ||
[pattern] | ||
) | ||
|
||
const refWrapper = useCallback( | ||
(currentRef: any) => { | ||
if (currentRef && typeof currentRef === 'object') { | ||
return (instance: any) => { | ||
currentRef.current = instance | ||
} | ||
} | ||
|
||
if (currentRef && typeof currentRef === 'function') { | ||
return (instance: any) => { | ||
if (instance) { | ||
VMasker(instance).maskPattern(pattern) | ||
|
||
currentRef(instance) | ||
} | ||
} | ||
} | ||
|
||
return undefined | ||
}, | ||
[pattern] | ||
) | ||
|
||
const refWrapperFromObject = useCallback( | ||
(object: any) => { | ||
const { ref: currentRef, ...rest } = object | ||
|
||
return { ref: refWrapper(ref), ...rest } | ||
}, | ||
[ref, refWrapper] | ||
) | ||
|
||
return { | ||
ref, | ||
refWrapper, | ||
refWrapperFromObject, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters