Skip to content

Instantly share code, notes, and snippets.

@eric-horodyski
Created December 16, 2020 21:16
Show Gist options
  • Save eric-horodyski/3ab3689acbacf561868a83b3bc79cbc2 to your computer and use it in GitHub Desktop.
Save eric-horodyski/3ab3689acbacf561868a83b3bc79cbc2 to your computer and use it in GitHub Desktop.
FW React Input Masking - IonInput
const MyComponent: React.FC = () => {
const [myValue, setMyValue] = useState('');
return (
<MaskedIonInput value={myValue} onChange={(v) => setMyValue(v)} />
);
}
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