简体   繁体   English

当formData作为prop传递给表单时,带有输入的自定义窗口小部件会在react-jsonschema-form中失去焦点

[英]Custom widget with input loses focus in react-jsonschema-form when formData is passed as a prop to the form

I have a form made using react-jsonschema-form and I maintain a state for formData and pass it as a prop to the form along with an onChange event. 我有一个使用react-jsonschema-form的表单,我为formData维护一个状态,并将它作为prop传递给表单以及onChange事件。 I need to maintain this formData state and pass as prop because I have other states as well which when setState re-render the form and the form fields reset. 我需要维护这个formData状态并传递为prop,因为我还有其他状态,当setState重新呈现表单并重置表单字段时。 I have a custom widget with an input box in this form. 我有一个自定义小部件,在这个窗体中有一个输入框。 Below is my sample code. 以下是我的示例代码。 Problem - For every character I type in the input box, the custom widget gets re-rendered and the input box loses focus. 问题 - 对于我在输入框中键入的每个字符,自定义窗口小部件都会重新渲染,输入框会失去焦点。
https://jsfiddle.net/niteenautade/fce9x2ny/ https://jsfiddle.net/niteenautade/fce9x2ny/

import React, { Component } from 'react';
import Form from "react-jsonschema-form";

class App extends Component {
  constructor(props){
    super(props)
    this.state = {
      formData : {}
    }
    this.getSchema = this.getSchema.bind(this)
    this.getUiSchema = this.getUiSchema.bind(this)
  }
  render() {
    var schema = this.getSchema()
    var uiSchema = this.getUiSchema()

    return (
      <div className="App">
        <div>
          <Form schema={schema}
          uiSchema={uiSchema}
          onChange={(e)=>{this.setState({formData:e.formData})}}
          formData={this.state.formData}
          onSubmit={(data)=>{ console.log(data)  }}
          />
        </div>
      </div>
    );
  }
  getSchema = () => {
    const schema = {
      title: "Test Form",
      type: "object",
      properties: {
        firstname: {
          type: "string",
          title: "firstname"
        },
      }
    };
    return schema
  }
  getUiSchema = () => {
    const uiSchema = {
      firstname : {
        "ui:widget": (props) => {
          return (
            <div>
              <input type="text"
                value={props.value}
                required={props.required}
                onChange={(event) => props.onChange(event.target.value)} />
            </div>
          );
      }
      }
    };
    return uiSchema
  }
}

export default App;

If you want to track form changes, you can handle it without settingState on every change, instead you can setState on Submit. 如果要跟踪表单更改,可以在每次更改时不设置State来处理它,而是可以在Submit上设置state。 If you want to pass formData programmatically you can use something as following: 如果要以编程方式传递formData,可以使用以下内容:

this.state = {
  formData : {
    firstname:'Test first name'
  }
}

Following code works as you expect: ( https://jsfiddle.net/tpjL2z06/ ) 以下代码按预期工作:( https://jsfiddle.net/tpjL2z06/

const Form = JSONSchemaForm.default;
const schema = {
      title: "Test Form",
      type: "object",
      properties: {
        firstname: {
          type: "string",
          title: "firstname"
        },
      }
};

const uiSchema = {
  firstname : {
    "ui:widget": (props) => {
      return (
        <div>
          <input type="text"
            value={props.value}
            required={props.required}
            onChange={(event) => props.onChange(event.target.value)} />
        </div>
      );
    }
  }
};

class App extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      formData : {}
    }
  }

  render() {
    return (
      <div className="App">
        <div>
          <Form 
          schema={schema}
          uiSchema={uiSchema}
          formData={this.state.formData}
          onSubmit={(data)=>{ console.log(data)  }}
          />
        </div>
      </div>
    );
  }

}

ReactDOM.render(<App />, document.querySelector("#app"))

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

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