javascript – 当formData作为prop传递给表单时,带有输入的自定义窗口小部件在react-jsonschema-form中失去焦点
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 当formData作为prop传递给表单时,带有输入的自定义窗口小部件在react-jsonschema-form中失去焦点,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2276字,纯文字阅读大概需要4分钟。
内容图文
我有一个使用react-jsonschema-form的表单,我为formData维护一个状态,并将它作为prop传递给表单以及onChange事件.我需要维护这个formData状态并传递为prop,因为我还有其他状态,当setState重新呈现表单并重置表单字段时.我有一个自定义小部件,在这个窗体中有一个输入框.以下是我的示例代码.
问题 – 对于我在输入框中键入的每个字符,自定义窗口小部件都会重新渲染,输入框会失去焦点.
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;
解决方法:
如果要跟踪表单更改,可以在每次更改时不设置State来处理它,而是可以在Submit上设置state.如果要以编程方式传递formData,可以使用以下内容:
this.state = {
formData : {
firstname:'Test first name'
}
}
以下代码按预期工作:(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"))
内容总结
以上是互联网集市为您收集整理的javascript – 当formData作为prop传递给表单时,带有输入的自定义窗口小部件在react-jsonschema-form中失去焦点全部内容,希望文章能够帮你解决javascript – 当formData作为prop传递给表单时,带有输入的自定义窗口小部件在react-jsonschema-form中失去焦点所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。