adição da mascara do CPF

This commit is contained in:
jp-lima 2025-09-01 11:39:06 -03:00
parent a489d84009
commit fb9d783f33

View File

@ -2,6 +2,23 @@ import React, { useState } from 'react';
import InputMask from "react-input-mask"; import InputMask from "react-input-mask";
function PatientForm({ onSave, onCancel }) { function PatientForm({ onSave, onCancel }) {
const FormatTelefones = () => {}
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
}
const [formData, setFormData] = useState({ const [formData, setFormData] = useState({
nome: '', nome: '',
nomeSocial: '', nomeSocial: '',
@ -39,8 +56,22 @@ function PatientForm({ onSave, onCancel }) {
...formData, ...formData,
[name]: value [name]: value
}); });
console.log(name)
if(e.target.name.include('cpf')){
let x = FormatCPF(e.target.value)
console.log(x)
setFormData({...formData,
[name]: x,}
)
}
}; };
// Função para buscar endereço pelo CEP // Função para buscar endereço pelo CEP
const handleCepBlur = async () => { const handleCepBlur = async () => {
const cep = formData.cep.replace(/\D/g, ''); const cep = formData.cep.replace(/\D/g, '');
@ -66,8 +97,8 @@ function PatientForm({ onSave, onCancel }) {
}; };
const handleSubmit = () => { const handleSubmit = () => {
if (!formData.nome || !formData.cpf) { if (!formData.nome || !formData.cpf || !formData.genero || !formData.dataNascimento){
alert('Por favor, preencha Nome e CPF.'); alert('Por favor, preencha Nome ,CPF, Gênero e data de nascimento.');
return; return;
} }
onSave( onSave(
@ -149,7 +180,7 @@ function PatientForm({ onSave, onCancel }) {
</div> </div>
<div className="col-md-6 mb-3"> <div className="col-md-6 mb-3">
<label>CPF:</label> <label>CPF:</label>
<input type="text" className="form-control" name="cpf" value={formData.cpf} onChange={handleChange} /> <input type="text" className="form-control" name="cpf" value={formData.cpf} onChange={ handleChange} />
</div> </div>
<div className="col-md-6 mb-3"> <div className="col-md-6 mb-3">
<label>Profissão:</label> <label>Profissão:</label>