最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ant design - Update checked value of a checkbox inside a Form.Item using Form.setFieldsValue - Stack Overflow

programmeradmin3浏览0评论

I have a React Form ponent that receives a state object. The Form contains a couple of Form.Item ponents, one antd Input & one antd Checkbox. I have the following code in my Form ponent:

const RecordsForm = ({ selectedRecord, recordComponentForm }) => {
  useEffect(() => {
    debugger;
    recordComponentForm.setFieldsValue({
      Title: selectedRecord ? selectedRecord.Title : null,
      IsCurrentRecord: selectedRecord ? selectedRecord.IsCurrentRecord : true,
    });
  }, [recordComponentForm, selectedRecord]);

  return (
    <Form
      name="seasonForm"
      autoComplete="off"
      layout={"inline"}
      form={recordComponentForm}
    >
      <Form.Item
        label="Title"
        name="Title"
        rules={[{ required: true, message: "Add a Title" }]}
      >
        <Input
          name="Title"
          placeholder="Season Title"
        />
      </Form.Item>

      <Form.Item
        label="Is Active Record"
        name="IsCurrentRecord"
        valuePropName="checked"
      >
        <Checkbox name="IsCurrentRecord" />
      </Form.Item>
    </Form>
  );
};

export default RecordsForm;

where the supplied prop recordComponentForm is instantiated in the parent ponent: const [recordComponentForm] = Form.useForm();

When I run the application and force this ponent to reload with a new selectedRecord, I want the Form.Item Input value to change to the Title from the object and the Checkbox to reflect the IsCurrentRecord boolean value of the object. The object passed in looks like this:

The Title Input value changes to show the Title of the object, however the checkbox remains unchanged:

I would like the checkbox to change it's checked value depending on what es in on the selectedRecord.IsCurrentRecord object in the same way the Title does. Any advice would be greatly appreciated.

I have a React Form ponent that receives a state object. The Form contains a couple of Form.Item ponents, one antd Input & one antd Checkbox. I have the following code in my Form ponent:

const RecordsForm = ({ selectedRecord, recordComponentForm }) => {
  useEffect(() => {
    debugger;
    recordComponentForm.setFieldsValue({
      Title: selectedRecord ? selectedRecord.Title : null,
      IsCurrentRecord: selectedRecord ? selectedRecord.IsCurrentRecord : true,
    });
  }, [recordComponentForm, selectedRecord]);

  return (
    <Form
      name="seasonForm"
      autoComplete="off"
      layout={"inline"}
      form={recordComponentForm}
    >
      <Form.Item
        label="Title"
        name="Title"
        rules={[{ required: true, message: "Add a Title" }]}
      >
        <Input
          name="Title"
          placeholder="Season Title"
        />
      </Form.Item>

      <Form.Item
        label="Is Active Record"
        name="IsCurrentRecord"
        valuePropName="checked"
      >
        <Checkbox name="IsCurrentRecord" />
      </Form.Item>
    </Form>
  );
};

export default RecordsForm;

where the supplied prop recordComponentForm is instantiated in the parent ponent: const [recordComponentForm] = Form.useForm();

When I run the application and force this ponent to reload with a new selectedRecord, I want the Form.Item Input value to change to the Title from the object and the Checkbox to reflect the IsCurrentRecord boolean value of the object. The object passed in looks like this:

The Title Input value changes to show the Title of the object, however the checkbox remains unchanged:

I would like the checkbox to change it's checked value depending on what es in on the selectedRecord.IsCurrentRecord object in the same way the Title does. Any advice would be greatly appreciated.

Share Improve this question edited Nov 24, 2021 at 20:33 CJH asked Nov 24, 2021 at 20:16 CJHCJH 1,5943 gold badges36 silver badges80 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Maybe.. just simply remove name from Checkbox.

<Form.Item
  label="Is Active Record"
  name="IsCurrentRecord"
  valuePropName="checked"
  >
  <Checkbox />
</Form.Item>

Checkbox usually return a value of choices (not boolean), so you perhaps should use Switch ponent instead. But by using valuePropName="checked", we could get boolean from Checkbox.

This was simply a case of the Form.Items sharing the same name and the child ponent it contains. Once I renamed the Form.Item for the Checkbox to something like IsCurrentRecord_FI and referred to it as such, it started working.

You can use



<Checkbox name="IsCurrentRecord" checked={Form.useWatch("IsCurrentRecord", form)} />

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论