简体   繁体   English

ReactJS Redux表单

[英]ReactJS Redux-Form

I have form with couple of input fields which submits values on BLUR action. 我有几个输入字段的表单,它们在BLUR动作上提交值。 When user changes something in text field CHANGE is invoked. 当用户更改文本字段中的某些内容时,将调用CHANGE。 But only BLUR invokes successful submitting values from text field. 但是只有BLUR会调用文本字段中的成功提交值。

So you have to click outside or use tab to blur out. 因此,您必须单击外部或使用选项卡使其模糊。 Or use classic button submit click. 或使用经典按钮提交点击。

I need to make it work with pressing enter. 我需要使其按回车键。 On enter pressed in input it invokes Touch and then submits form. 在输入中按下Enter键时,它会调用Touch,然后提交表单。

Did i miss something ? 我错过了什么 ? Or is it default behaviour of form ? 还是表单的默认行为?

In redux-form, when you connect a component to it you get various props which includes handleSubmit which when called submits your form. 在redux-form中,当您将组件连接到它时,您会得到各种道具,其中包括handleSubmit,当被调用时提交您的表单。 For pressing enter and submitting the form on last input you could pass an onKeyPress event callback to your custom Input passed to Field. 要按下Enter键并在最后一次输入时提交表单,您可以将onKeyPress事件回调传递给传递给Field的自定义输入。

I achieved this using the below code. 我使用以下代码实现了这一点。

import React from "react";
import { Field, reduxForm } from "redux-form";


class SimpleForm extends React.Component {

  handleKeyPressEvent = event => {
    console.log("event", event.charCode);
    if (event.charCode === 13) {
      this.props.handleSubmit();
    }
  };

  renderInput = props => (
    <input type={props.type} {...props.input} placeholder={props.placeholder} onKeyPress={this.handleKeyPressEvent} />
  );

  render() {
    const { handleSubmit, pristine, submitting } = this.props;
    return (
      <form onSubmit={handleSubmit}>
        <div>
          <label>First Name</label>
          <div>
            <Field
              name="firstName"
              component={this.renderInput}
              type="text"
              placeholder="First Name"
            />
          </div>
        </div>
        <div>
          <button type="submit" disabled={pristine || submitting}>
            Submit
          </button>
        </div>
      </form>
    );
  }
};

export default reduxForm({
  form: "simple" // a unique identifier for this form
})(SimpleForm);

I hope it helps. 希望对您有所帮助。 Thanks !! 谢谢 !!

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

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