Novas funcionalidades EM BREVE!

Máscara de CPF

export function maskCPF(cpf: string) {
return cpf
.replace(/\D/g, "")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d{1,2})/, "$1-$2")
.replace(/(-\d{2})\d+?$/, "$1");
}
import React from "react";

export function maskCPF(cpf: string) {
return cpf
.replace(/\D/g, "")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d)/, "$1.$2")
.replace(/(\d{3})(\d{1,2})/, "$1-$2")
.replace(/(-\d{2})\d+?$/, "$1");
}

export default function App() {
const [cpf, setCPF] = React.useState("");
return (
<div className="App">
<input
type="tel"
placeholder="XXX.XXX.XXX-XX"
value={cpf}
onChange={(e) => setCPF(maskCPF(e.target.value))}
/>
</div>
);
}

Observação:

Ao utilizar máscaras, use input com o type='tel', dessa forma o input abrirá o teclado numérico em dispositivos mobile e permitirá a escrita de caracteres especiais.

A função de máscara no exemplo acima já previne que o usuário digite letras quando não for permitido.