From 5b63fa26ef5b640db973c4f6a46aa7b8e98da22a Mon Sep 17 00:00:00 2001 From: jp-lima Date: Tue, 2 Sep 2025 10:14:58 -0300 Subject: [PATCH] Mascara telefones --- src/components/patients/PatientForm.jsx | 41 ++++++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/components/patients/PatientForm.jsx b/src/components/patients/PatientForm.jsx index a5bea423..6adb5cbb 100644 --- a/src/components/patients/PatientForm.jsx +++ b/src/components/patients/PatientForm.jsx @@ -4,18 +4,32 @@ import InputMask from "react-input-mask"; function PatientForm({ onSave, onCancel }) { - const FormatTelefones = () => {} + const FormatTelefones = (valor) => { + + const digits = String(valor).replace(/\D/g, '').slice(0, 11); + + + return digits + .replace(/(\d)/, '($1') // 123 -> 123. + .replace(/(\d{2})(\d)/, '$1) $2' ) + .replace(/(\d)(\d{4})/, '$1 $2') + .replace(/(\d{4})(\d{4})/, '$1-$2') + + + } const FormatCPF = (valor) => { const digits = String(valor).replace(/\D/g, '').slice(0, 11); + return digits .replace(/(\d{3})(\d)/, '$1.$2') // 123 -> 123. .replace(/(\d{3})(\d)/, '$1.$2') // 123.456 -> 123.456. .replace(/(\d{3})(\d{1,2})$/, '$1-$2'); // 123.456.789 -> 123.456.789-01 + } @@ -57,19 +71,30 @@ function PatientForm({ onSave, onCancel }) { [name]: value }); - console.log(name) - if(e.target.name.include('cpf')){ + if(name.includes('cpf')){ - let x = FormatCPF(e.target.value) + let cpfFormatado = FormatCPF(e.target.value) - console.log(x) setFormData({...formData, - [name]: x,} - ) + [name]: cpfFormatado,} + )} + else if(name.includes('telefone')){ + let telefoneFormatado = FormatTelefones(value) + + console.log(telefoneFormatado) + + + + setFormData({...formData, + [name]: telefoneFormatado + }) } - }; + + + + }; // Função para buscar endereço pelo CEP