Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在Form.List的场景下使用 setFields 对应的ListItem不会触发更新 #496

Open
SYZhenYuZhao opened this issue Aug 31, 2022 · 2 comments

Comments

@SYZhenYuZhao
Copy link

JSX

<Form.List name="sights">
  {(fields, { add, remove }) => (
    <>
      {fields.map((field) => (
        <Space key={field.key} align="baseline">
          <Form.Item
            noStyle
            shouldUpdate={(prevValues, curValues) =>
              prevValues.area !== curValues.area ||
              prevValues.sights !== curValues.sights
            }
          >
            {() => (
              <Form.Item
                {...field}
                label="Sight"
                name={[field.name, "sight"]}
                rules={[
                  {
                    required: true,
                    message: "Missing sight"
                  }
                ]}
              >
                <Select
                  disabled={!form.getFieldValue("area")}
                  style={{
                    width: 130
                  }}
                >
                  {(sights[form.getFieldValue("area")] || []).map(
                    (item) => (
                      <Option key={item} value={item}>
                        {item}
                      </Option>
                    )
                  )}
                </Select>
              </Form.Item>
            )}
          </Form.Item>
          <Form.Item
            {...field}
            label="Price"
            name={[field.name, "price"]}
            rules={[
              {
                required: true,
                message: "Missing price"
              }
            ]}
          >
            <Input />
          </Form.Item>

          <MinusCircleOutlined onClick={() => remove(field.name)} />
        </Space>
      ))}

      <Form.Item>
        <Button
          type="dashed"
          onClick={() => add()}
          block
          icon={<PlusOutlined />}
        >
          Add sights
        </Button>
        <Button onClick={resetFirstLine}>reset first line</Button>
      </Form.Item>
    </>
  )}
</Form.List>

调用JS

 const resetFirstLine = () => {
    form.setFieldValue(["sights", 0], {
      sight: 1,
      price: 123
    });
  };

当触发 resetFirstLine 的时候页面上的listItem不刷新

codesandbox地址

@shaunhurryup
Copy link

没看懂问题在哪,name='sights' 的第一个 Field 没刷新?

@SYZhenYuZhao
Copy link
Author

对的,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants