简体   繁体   English

按下回车键后如何触发onSubmit功能?

[英]How do I trigger my onSubmit function when the enter key is pressed?

I have a component that is supposed to submit when the user presses enter or clicks a button. 我有一个组件,当用户按下Enter或单击按钮时应该提交。 The big thing is that the user is going to expect to be able to use Enter to submit this form, and the send button is really for only when they notice it's there, and decide to press the button. 最重要的是,用户希望能够使用Enter来提交此表单,并且发送按钮仅在他们注意到它时才会发送,并决定按下按钮。

My issue is that I have the form setup and all, and I have my submit handler working properly, but I can't seem to capture the enter key for this at all! 我的问题是我有表单设置和所有,我让我的提交处理程序正常工作,但我似乎无法捕获这个的输入键!

first I have my submit function like this: 首先,我有这样的提交功能:

  onSubmit(e) {
    e.preventDefault();
    this.state.socket.emit('message', this.state.message);
    console.log('emitted');
    this.setState(
      {
        message: '',
      },
      () => {
        // e.target.value = '';
        console.log('state should be cleared.');
      },
    );
  }

and my form like this: 我的表格是这样的:

<Form>
 <FormGroup>
   <div className="chatBoxRow2">
      <input
         type="text"
         name="message"
         onChange={this.onChange}
         value={this.state.message}
         className="chatBoxOut"
       />
       <Button className="chatBoxButton" onClick={this.onSubmit}>
          Send
       </Button>
     </div>
  </FormGroup>
</Form>

I did try to get this done with a solution found here: react-redux forms github issue. 我确实尝试使用此处找到的解决方案完成此操作: react-redux形成github问题。

but to no avail, I still get an refreshed page..... 但无济于事,我仍然得到一个更新的页面.....

Listen to onSubmit on the form rather than onclick on the button. onSubmit的形式,而不是onclick上的按钮。 Buttons are type "submit" by default, so clicking it or pressing 'Enter' inside the text field will submit your form. 默认情况下,按钮是“提交”类型,因此单击它或在文本字段内按“输入”将提交您的表单。

<Form onSubmit={this.onSubmit}>
 <FormGroup>
   <div className="chatBoxRow2">
      <input
         type="text"
         name="message"
         onChange={this.onChange}
         value={this.state.message}
         className="chatBoxOut"
       />
       <Button className="chatBoxButton">
          Send
       </Button>
     </div>
  </FormGroup>
</Form>

Refactor this: 重构这个:

<Form>
 <FormGroup>
   <div className="chatBoxRow2">
      <input
         type="text"
         name="message"
         onChange={this.onChange}
         value={this.state.message}
         className="chatBoxOut"
       />
       <Button className="chatBoxButton" onClick={this.onSubmit}>
          Send
       </Button>
     </div>
  </FormGroup>
</Form>

into this: 进入这个:

Send 发送

So where did I get handleSubmit from? 那么我从哪里获得handleSubmit Well, if you do a console.log(this.props); 好吧,如果你做一个console.log(this.props); here: 这里:

render() {
  console.log(this.props);
  return (
    <Form onSubmit={this.props.handleSubmit(this.onSubmit)}>
 <FormGroup>
   <div className="chatBoxRow2">
      <input
         type="text"
         name="message"
         onChange={this.onChange}
         value={this.state.message}
         className="chatBoxOut"
       />
       <button className="chatBoxButton">Send</button>
     </div>
  </FormGroup>
</Form>
  )
}

You will find there are tremendous number of properties inside that props object by Redux Form, one in particular, is called handleSubmit which is a function we are supposed to call anytime that your form gets submitted and its provided by Redux Form. 您会发现Redux Form中的props对象中有大量属性,特别是一个名为handleSubmit的函数,它是我们应该在您的表单被提交并由Redux Form提供的任何时候调用的函数。

Okay, but why not just onSubmit={this.onSubmit} ? 好的,但为什么不只是onSubmit={this.onSubmit} Short answer is because now you are using Redux Form. 简短的回答是因为现在您使用的是Redux Form。

This new process of calling onSubmit={this.props.handleSubmit(this.onSubmit)} will change how onSubmit gets called. 这个调用onSubmit={this.props.handleSubmit(this.onSubmit)}新进程将改变调用onSubmit

So instead of: 所以代替:

onSubmit(event) {
  event.preventDefault();
}

You can now do: 你现在可以这样做:

onSubmit(formValues) {

}

You see you no longer have to deal with the event object, instead, it will be called with all the values out of your form. 您会发现不再需要处理event对象,而是使用表单中的所有值调用它。 So handleSubmit handles the event.preventDefault() behind the curtain, leaving our onSubmit() callback to get called with the formValues . 所以handleSubmit处理幕后的event.preventDefault() ,让我们的onSubmit()回调用formValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM