简体   繁体   English

Antd Form中如何清除Datepicker值?

[英]How to clear Datepicker value in Antd Form?

If trigger button inside same Form.Item with Datepicker it works, but if button is outside datepicker does not clear.如果使用 Datepicker 在同一 Form.Item 内触发按钮,它可以工作,但如果按钮在 datepicker 之外,则不会清除。

I basicly try to clear Datepicker after submit.我基本上尝试在提交后清除 Datepicker。

It's strange, I could not understand it's antd bug or am I missing something else?很奇怪,我无法理解它是 antd bug 还是我错过了其他东西?

 <Form
    form={form}
    name="advanced_assessment_form"
    onFinish={() => setIsClear(!isClear);}
  >
    <Form.Item
      name="field"
      rules={[
        {
          required: true,
          message: "Input something!"
        }
      ]}
    >
      <DatePicker
        name="datepicker"
        onChange={(val) => {
          setResponseDate(val);
        }}
        value={isClear ? null : responseDate}
      />
      {/* This Button works! */}
      {/* <Button htmlType="submit" onClick={onButton}>
        {" "}
        Clear Datepicker{" "}
      </Button> */}
    </Form.Item>
    <Form.Item>
      {/* This Button does not work */}
      <Button htmlType="submit" onClick={onButton}>
        {" "}
        Clear Datepicker{" "}
      </Button>
    </Form.Item>
  </Form>

here sandbox code: CodeSandbox这里沙箱代码: CodeSandbox

I've found solution on antd Form API.我在 antd Form API 上找到了解决方案。

Just calling below function helps to clear all field.只需在 function 下面调用有助于清除所有字段。

form.resetFields();

if you want to set value to a specific field;如果要为特定字段设置值;

form.setFieldsValue({field: '01/01/2021/, 'DD/MM/YYYY')})

Cheer's干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM