Created
December 16, 2020 21:16
-
-
Save eric-horodyski/3ab3689acbacf561868a83b3bc79cbc2 to your computer and use it in GitHub Desktop.
FW React Input Masking - IonInput
This file contains hidden or 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
const MyComponent: React.FC = () => { | |
const [myValue, setMyValue] = useState(''); | |
return ( | |
<MaskedIonInput value={myValue} onChange={(v) => setMyValue(v)} /> | |
); | |
} |
This file contains hidden or 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
const MaskedIonInput = ({ value, onChange }: Props) => { | |
const maskRef = useRef<IMask.InputMask<any> | null>(null); | |
const inputCallback = useCallback(async (input) => { | |
if (!input) { | |
return; | |
} | |
const nativeInput = await input.getInputElement(); | |
const mask = IMask(nativeInput, { | |
mask: Number, | |
thousandsSeparator: ",", | |
}).on('accept', (e: any) => { | |
onChange(mask.value); | |
}).on('complete', (e: any) => { | |
onChange(mask.value); | |
}); | |
maskRef.current = mask; | |
}, []); | |
return ( | |
<IonInput value={value} ref={inputCallback} /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment