[英]How can I use initialValue for DatePicker in Form.Item of Ant Design
Ant 设计说我不能使用defaultValue
如果我用带有name
的Form.Item
包装它。
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.
所以我将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)
我的代码是这样的。
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>
);
如何获取和使用工人 dateOfBirth 的默认值?
谢谢你。
DatePicker
组件的initialValue
必须是moment
object。 您需要将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")
);
代码沙盒链接:
我有类似的问题,但问题不同。 我不想使用 moment.js,但我无法从Antd版本 4 形式的道具中设置 initialValues。
因此,我尝试了不同的解决方法并安装了 formik和formik-antd 。 安装后,我修改了我的 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.