简体   繁体   English

如何在 Ant 设计的 Form.Item 中为 DatePicker 使用 initialValue

[英]How can I use initialValue for DatePicker in Form.Item of Ant Design

Ant Design says I can't use defaultValue if I wrapped it with Form.Item with name . Ant 设计说我不能使用defaultValue如果我用带有nameForm.Item包装它。

https://ant.design/components/form/ https://ant.design/components/form/

After wrapped by Form.Item with name property, value(or other property defined by valuePropName) onChange(or other property defined by trigger) props will be added to form controls, the flow of form data will be handled by Form which will cause:

You shouldn't use onChange on each form control to collect data(use onValuesChange of Form), but you can still listen to onChange.

You cannot set value for each form control via value or defaultValue prop, you should set default value with initialValues of Form. Note that initialValues cannot be updated by setState dynamiclly, you should use setFieldsValue in that situation.

So I set initialValues to Form component and I got this error when I use Form with DatePicker.所以我将initialValues设置为Form组件,当我将 Form 与 DatePicker 一起使用时出现此错误。

moment.js:93 Uncaught TypeError: date.clone is not a function
    at Object.format (moment.js:93)
    at useValueTexts.js:13
    at Array.map (<anonymous>)
    at useValueTexts.js:12
    at useMemo (useMemo.js:6)
    at useValueTexts (useValueTexts.js:7)
    at InnerPicker (Picker.js:158)
    at renderWithHooks (react-dom.development.js:14803)
    at mountIndeterminateComponent (react-dom.development.js:17482)
    at beginWork (react-dom.development.js:18596)

My code is like this.我的代码是这样的。
worker object has a dateOfBirth which is ISOString. worker object 的dateOfBirth是 ISOString。

const dateFormat = 'YYYY/MM/DD';

return (
  <Form
    layout="vertical"
    hideRequiredMark
    onFinish={values => onFinish(values)}
    initialValues={worker}
  >
    <Form.Item
      label="Date Of Birth"
      name="dateOfBirth"
    >
      <DatePicker format={dateFormat} />
    </Form.Item>
  </Form>
);

How can I get and use default value of worker's dateOfBirth?如何获取和使用工人 dateOfBirth 的默认值?
Thank you.谢谢你。

The initialValue of the DatePicker component must be a moment object. DatePicker组件的initialValue必须是moment object。 You will need to convert dateOfBirth field on the worker object from an ISOString to a moment object as illustrated below:您需要将worker object 上的dateOfBirth字段从 ISOString 转换为时刻 object,如下所示:

const dateFormat = "YYYY/MM/DD";

const worker = {
  dateOfBirth: moment('2020-06-09T12:40:14+0000')
};

ReactDOM.render(
  <Form
    layout="vertical"
    hideRequiredMark
    onFinish={values => onFinish(values)}
    initialValues={worker}
  >
    <Form.Item label="Date Of Birth" name="dateOfBirth">
      <DatePicker format={dateFormat} />
    </Form.Item>
  </Form>,
  document.getElementById("container")
);

CodeSandbox Link:代码沙盒链接:

https://codesandbox.io/s/antdesign-setting-initialvalue-on-datepicker-inside-form-ypi4u?file=/index.js:239-652 https://codesandbox.io/s/antdesign-setting-initialvalue-on-datepicker-inside-form-ypi4u?file=/index.js:239-652

I had the similar problem but the issue was different.我有类似的问题,但问题不同。 I wan not using moment.js but I was not able to set initialValues from props in Antd version 4 form.我不想使用 moment.js,但我无法从Antd版本 4 形式的道具中设置 initialValues。

So, I tried a different workaround and install formik and formik-antd .因此,我尝试了不同的解决方法并安装了 formikformik-antd After installing, I modified my function component something like this:安装后,我修改了我的 function 组件,如下所示:

import React from "react"
import {
  Input,
  ResetButton,
  SubmitButton,
  Form,
} from "formik-antd"
import { Formik } from "formik"
import { Button } from "antd"
import "antd/dist/antd.css"

export function MyForm(props: any) {
  
    const onFinish = (values: any) => {
      console.log(values);
    };

  return (
    <Formik
      enableReinitialize
      initialValues={{
        company: props.myobject.company,
        account: props.myobject.account
      }}
      onSubmit={onFinish}
      validate={values => {
        if (!values.company) {
          return { company: "required" }
        }
        if (!values.account) {
          return { account: "required" }
        }
        return undefined
      }}
      render={formik => (
        <Form>
          <div className="container">
            <div className="component-container">
              <Form.Item
                name="company"
                label="Company"
                hasFeedback={true}
                showValidateSuccess={true}
              >
              <Input name="company" placeholder="Basic Input" />
              </Form.Item>
              <Form.Item
                name="account"
                label="Account"
                hasFeedback={true}
                showValidateSuccess={true}
              >
                <Input name="account" placeholder="Validated input" />
              </Form.Item>
              <Button.Group size="large">
                <ResetButton>Reset</ResetButton>
                <SubmitButton type="primary" disabled={false}>
                  Submit
                </SubmitButton>
              </Button.Group>
            </div>
          </div>
        </Form>
      )}
    />
  )
}

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

相关问题 如何使用 ant 设计表格 initialValue - how to use ant design form initialValue 蚂蚁设计Form中Form.Item中如何迭代添加Form.Item - How to iteratively add Form.Item in Form.Item in ant design Form 如何设置 layout=&quot;horizo​​ntal&quot; 里面的几个<Form.Item>同时保持<Form layout="vertical">在蚂蚁设计中 - How to set the layout="horizontal" inside for few <Form.Item> while keeping <Form layout="vertical"> in ant design 如何为动态表单字段设置initialValue(蚂蚁设计)? - How to set initialValue for dynamic form field (ant design)? ant 设计 - 使用 Form.setFieldsValue 更新 Form.Item 内复选框的选中值 - ant design - Update checked value of a checkbox inside a Form.Item using Form.setFieldsValue 如何在 React-Final-Form 中使用 debounce 和 initialValue? - How can I use debounce with initialValue in React-Final-Form? #Antd#如何将自制组件与Form.item绑定 - #Antd# How can I bind self-made component with Form.item 如何自定义 Ant 设计的表格? - How can I customised the form of Ant Design? React - Ant 设计、带有 getFieldDecorator 和 initialValue 的表单在单个检查框中不起作用 - React - Ant Design, Form with getFieldDecorator and initialValue not working in single chechbox Form.Item 在 antd 中经过验证后,如何提供回调? - How do I provide a callback when Form.Item has been validated in antd?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM