如何在react-jsonschema-form中同步两个字段?

huangapple go评论52阅读模式
英文:

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:

Codesandbox Link

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:

Codesandbox Link

import &quot;./styles.css&quot;;

export default function App() {
  const uiSchema = {
    limitSlider: { &quot;ui:widget&quot;: &quot;range&quot; }
  };

  const schema = {
    required: [&quot;limit&quot;, &quot;limitSlider&quot;],
    properties: {
      limit: {
        title: &quot;New Limit&quot;,
        type: &quot;integer&quot;
      },
      limitSlider: {
        title: &quot;New Limit Range&quot;,
        type: &quot;number&quot;,
        minimum: 0,
        maximum: 100
      }
    }
  };

  const onChange = (updatedValues) =&gt; {
    console.log(&quot;onchange&quot;, updatedValues);
  };

  const formData = { limit: 10, limitSlider: 10 };
  return (
    &lt;MuiForm
      uiSchema={uiSchema}
      schema={schema}
      formData={formData}
      onChange={onChange}
    /&gt;
  );
}

答案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) =&gt; {
  if(value !== updatedValues.formData.limitSlider){
setValue(updatedValues.formData.limitSlider)
  } else {
  setValue(updatedValues.formData.limit)
  }
}


huangapple
  • 本文由 发表于 2023年5月22日 01:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定