英文:
How to sync 2 fields in react-jsonschema-form?
问题
I am trying to create JsonSchema-Form component but I ran into a problem. I want inputs to change synchronously. There are 2 fields on the form, one is a "Limit" input and the other is a slider (range) input that can be dragged and change the limit value. In other words, I want to change the limit value by dragging the slider. But I cannot get them to work in sync. I am not really sure how to proceed with this and how to use the OnChange method to do what I want.
Here is a sample of what I'm trying to create:
import MuiForm from "@rjsf/material-ui";
import "./styles.css";
export default function App() {
const uiSchema = {
limitSlider: { "ui:widget": "range" }
};
const schema = {
required: ["limit", "limitSlider"],
properties: {
limit: {
title: "New Limit",
type: "integer"
},
limitSlider: {
title: "New Limit Range",
type: "number",
minimum: 0,
maximum: 100
}
}
};
const onChange = (updatedValues) => {
console.log("onchange", updatedValues);
};
const formData = { limit: 10, limitSlider: 10 };
return (
<MuiForm
uiSchema={uiSchema}
schema={schema}
formData={formData}
onChange={onChange}
/>
);
}
英文:
I am trying to create JsonSchema-Form component but I ran into a problem. I want inputs to change synchronously. There are 2 fields on the form, one is a "Limit" input and the other is a slider(range)input that can be dragged and change the limit value. In other words I want to change limit value by dragging the slider. But I cannot get them to work in sync.I am not really sure how to proceed with this and how to use the Onchange method to do what I want.
Here is a sample of what I'm trying to create:
import "./styles.css";
export default function App() {
const uiSchema = {
limitSlider: { "ui:widget": "range" }
};
const schema = {
required: ["limit", "limitSlider"],
properties: {
limit: {
title: "New Limit",
type: "integer"
},
limitSlider: {
title: "New Limit Range",
type: "number",
minimum: 0,
maximum: 100
}
}
};
const onChange = (updatedValues) => {
console.log("onchange", updatedValues);
};
const formData = { limit: 10, limitSlider: 10 };
return (
<MuiForm
uiSchema={uiSchema}
schema={schema}
formData={formData}
onChange={onChange}
/>
);
}
答案1
得分: 1
这个目前不起作用,因为您调用了两次setValue,并在第二次调用中使用未更改的限制进行更新。
您可以通过检查当前值是否等于滑块值,如果不等于,则将其设置为新的滑块值,否则将其设置为输入字段的值,以使两个组件的状态双向同步。
const onChange = (updatedValues) => {
if (value !== updatedValues.formData.limitSlider) {
setValue(updatedValues.formData.limitSlider);
} else {
setValue(updatedValues.formData.limit);
}
}
英文:
This isn’t working at the moment because you are calling setValue twice and updating it with the unchanged limit in the second call.
You can sync up the state of the two components bidirectionally by checking if the current value is equal to the slider value and if not, setting it to the new slider value, and otherwise setting it to the input field value
const onChange = (updatedValues) => {
if(value !== updatedValues.formData.limitSlider){
setValue(updatedValues.formData.limitSlider)
} else {
setValue(updatedValues.formData.limit)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论