I have the following code:
import React from "react";
import ReactDOM from "react-dom";
import MaskedInput from "react-text-mask";
import "./styles.scss";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
zip: ''
};
}
render() {
return (
<div className="App">
<h1>Testing the Zip Code</h1>
<MaskedInput
className="zip"
type="tel"
placeholder="XXXXX"
placeholderChar={"\u2000"}
mask={[/\d/, /\d/, /\d/, /\d/, /\d/]}
name="zip"
value={this.state.zip}
/>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
which creates a masked input for the zip code.
Here you have a live demo: https://codesandbox.io/s/3v18yzn5rm
You can see my problem by doing the following:
- Try typing a partial zip code on the input box, e.g.: "123".
- Then click outside the input box.
- Then click again inside the input box.
- You will see that there are two spaces at the end: "123__" (no underscores).
- This behavior prevents you to continue typing straigth forward unless you remove those extra spaces.
I need that when I click inside the input box again I have: "123" (no extra spaces).
Any idea on how to achieve that? It is really annoying.
Please, fork the code above with your solution and paste the link here.
Thanks!