英文:
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 "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 first approach if your field value can be changed by any external field otherwise use second approach.
Hope this solves your problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论