I wanted to add the -
char after typing 5 characters like this:
but I got this:
My code :
interface IAddress {
bairro: string;
cep: string;
cidade: string;
complemento: string;
rua: string;
numero: string;
}
export default function App() {
const [address, setAddress] = useState<IAddress>({
bairro: "",
cep: "",
cidade: "",
complemento: "",
rua: "",
numero: ""
});
const handlerCep = (e: any) => {
const { value } = e.target;
const cep = value.replace(/[^0-9]/g, "");
setAddress({
...address,
cep: address.cep.length === 5 ? `${cep}-` : cep
});
};
return (
<div className="App">
<input
type="tel"
maxLength={9}
value={address.cep}
onChange={(e) => handlerCep(e)}
// onBlur={(ev) => findCep(ev)}
required
id="cep"
/>
</div>
);
}
I'm freaking out at some point and I can't imagine how I can solve this live example here :
Code sandbox: https://codesandbox.io/s/silly-sea-o4gd5?file=/src/App.tsx