我想問一下,當我按下回車鍵後,當我清空文本字段的狀態值時,如何防止光標移到下一行。到目前爲止,我擁有的是當你按下輸入它清除文本字段的值,但光標將進入下一行。這裏是我得到的:輸入時防止textfield換行 - reactjs
import React, { Component } from 'react';
import Textfield from 'react-mdl/lib/Textfield';
import "./style.css";
class ChatMessageBox extends Component {
constructor(props){
super(props);
this.state = {
messageBox: ''
};
}
handleMessageBox(event){
if(event.key === "Enter"){
this.setState({
messageBox: ''
});
}
}
handleMessageBoxOnChange(event){
this.setState({
messageBox: event.target.value
});
}
render(){
return (
<div className="chat-message-box">
<Textfield
onKeyPress={(e) => { this.handleMessageBox(e) }}
onChange={ (e) => { this.handleMessageBoxOnChange(e) }}
value={this.state.messageBox}
label="Type a message"
rows={4}
/>
</div>
);
}
}
export default ChatMessageBox;
謝謝兄弟。這工作! –