React Chinese
by karlovac
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
input {
margin-right: 5px;
}
React
class ChineseInput extends React.Component {
constructor(props) {
super(props)
this.state = {
text: 'hi'
}
}
isChinese = testString => {
const re = /^[\u4E00-\u9FA5]+$/;
return re.test(testString);
};
handleTextChange = (e) => {
console.log('e.target.value', e.target.value);
let newValue = e.target.value;
if (!this.isChinese(newValue)) {
newValue = ''
}
this.setState({
text: newValue
});
};
render() {
return (
<div>
<input type="text" value={this.state.text}
onChange={this.handleTextChange} />
<div>Text in state: {this.state.text} </div>
</div>
)
}
}
ReactDOM.render(<ChineseInput />, document.querySelector("#app"))