isFieldsTouched在使用setFieldsValue antd表单时会变为“true”。

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

isFieldsTouched turn "true" when use setFieldsValue antd form

问题

我想使用 isFieldsTouched 在表单中检查用户类型,但在第一次渲染时,当我向表单填充数据时,isFieldsTouched 变为 "true",即使我一点都没有触摸。我该如何修复这个问题。

我的版本:
antd: 4.21.5,
react: 18.2.0

示例:在这里

我希望在使用 setFieldsValue 时,isFieldsTouched 为 false。有人有解决方案。非常感谢。

英文:

I want to check user type in form with isFieldsTouched but when the first render, i fill data to the form and isFieldsTouched turn to "true", even i'm not touch at all. How can I fix this.

My version:
antd:4.21.5,
react:18.2.0

demo:here

I want isFieldsTouched false when I setFieldsValue. Anyone have solution. Thank alot

答案1

得分: 1

The way you are trying to detect if the field is touched is wrong because the useEffect dependency never changes. Assuming that you want to detect if the field is touched or not after you set the field values. There are 2 ways to solve this problem.

First Approach: (Use onFieldsChange) and passing trigger='onFocus' in Form.Item

You can pass onFieldsChange to the Form component and check the changedValues array if the required field is touched or not. Note that we pass trigger='onFocus' that will trigger onFieldsChange, otherwise onFieldsChange will only be called when any field value changes.

Remember changedValues is an array. The second value of value is an object containing the following fields:

  • name (Your field name is note, so the name will be ['note'])
  • value
  • errors
  • warnings
  • touched
  • validating
import { Form, Input } from "antd";
import React, { useEffect, useState } from "react";

const App: React.FC = () => {
  const [form] = Form.useForm();
  const [touch, setTouch] = useState(false);

  useEffect(() => {
    form.setFieldsValue({ note: "aaaaa" });
  }, [form]);

  console.log(touch);

  return (
    <Form
      form={form}
      onFieldsChange={(changedValue, allFields) => {
        changedValue.forEach((value) => {
          if (value.name.toString() === "note") {
            setTouch(value.touched ?? false);
          }
        });
      }}
    >
      <Form.Item name="note" trigger='onFocus'>
        <Input />
      </Form.Item>
    </Form>
  );
};

export default App;

Second Approach: (Use onFocus)

import { Form, Input } from "antd";
import React, { useEffect, useState } from "react";

const App: React.FC = () => {
  const [form] = Form.useForm();
  const [touch, setTouch] = useState(false);

  useEffect(() => {
    form.setFieldsValue({ note: "aaaaa" });
  }, [form]);

  console.log(touch);

  return (
    <Form form={form}>
      <Form.Item name="note">
        <Input onFocus={() => setTouch(true)} />
      </Form.Item>
    </Form>
  );
};

export default App;

Use the first approach if your field value can be changed by any external field; otherwise, use the second approach.

Hope this solves your problem.

英文:

The way you are trying to detect if field is touched is wrong because useEffect dependency never changes. Assuming that you want to detect if field is touched or not after you set the field values. There are 2 ways to solve this problem.

First Approach: (Use onFieldsChange) and passing trigger='onFocus' in Form.Item

You can pass onFieldsChange to Form component and check the changedValues array if required field is touched or not. Note that we pass trigger='onFocus' that will trigger onFieldsChange otherwise onFieldsChange will only be called when any field value changes.

Remember changedValues is an array. Second value of value is an object containing following fields:

  • name (Your field name is note. so name will be ['note'])
  • value
  • errors
  • warnings
  • touched
  • validating
import { Form, Input } from &quot;antd&quot;;
import React, { useEffect, useState } from &quot;react&quot;;

const App: React.FC = () =&gt; {
  const [form] = Form.useForm();
  const [touch, setTouch] = useState(false);

  useEffect(() =&gt; {
    form.setFieldsValue({ note: &quot;aaaaa&quot; });
  }, [form]);

  console.log(touch);

  return (
    &lt;Form
      form={form}
      onFieldsChange={(changedValue, allFields) =&gt; {
        changedValue.forEach((value) =&gt; {
          if (value.name.toString() === &quot;note&quot;) {
            setTouch(value.touched ?? false);
          }
        });
      }}
    &gt;
      &lt;Form.Item name=&quot;note&quot; trigger=&#39;onFocus&#39;&gt;
        &lt;Input /&gt;
      &lt;/Form.Item&gt;
    &lt;/Form&gt;
  );
};

export default App;

Second Approach: (Use onFocus)

import { Form, Input } from &quot;antd&quot;;
import React, { useEffect, useState } from &quot;react&quot;;

const App: React.FC = () =&gt; {
  const [form] = Form.useForm();
  const [touch, setTouch] = useState(false);

  useEffect(() =&gt; {
    form.setFieldsValue({ note: &quot;aaaaa&quot; });
  }, [form]);

  console.log(touch);

  return (
    &lt;Form form={form}&gt;
      &lt;Form.Item name=&quot;note&quot;&gt;
        &lt;Input onFocus={() =&gt; setTouch(true)} /&gt;
      &lt;/Form.Item&gt;
    &lt;/Form&gt;
  );
};

export default App;

Use first approach if your field value can be changed by any external field otherwise use second approach.

Hope this solves your problem

huangapple
  • 本文由 发表于 2023年6月27日 16:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563225.html
匿名

发表评论

匿名网友

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

确定