Ant Design - Modal with Datepicker(表单)

Ant Design - Modal with Datepicker (form)

复制link

codesandbox example

到底发生了什么?

我在Datepicker<Form.Item中输入了属性name,应用程序只是returns一个错误。

期待什么?

我想在 Modal 中使用日期,能够从对象中检索值并将 Datepicker 更改发送到表单,就像文本输入一样

我打算使用这个对象

{
  name: "Wagner Fillio",
  gender: "male",
  birthday: "2022-02-20"
}

但是我得到下面的对象

import { useState } from "react";
import { Modal, Row, Col, Form, Input, DatePicker, Select, Button } from "antd";

import "./style.css";

export function Test() {
  const [form] = Form.useForm();
  const [isModalVisible, setIsModalVisible] = useState < boolean > false;

  const initialValues = {
    name: "Wagner Fillio",
    gender: "male",
    birthday: "2022-02-20",
  };

  const showModal = () => {
    setIsModalVisible(true);
  };

  const handleOk = () => {
    setIsModalVisible(false);
  };

  const handleCancel = () => {
    form.resetFields();
    setIsModalVisible(false);
  };

  const onFinish = async () => {
    try {
      const values = await form.validateFields();
      console.log("Success:", values);

      // Intended object
      // {{name: 'Wagner Fillio', gender: 'male', birthday: '2022-02-20'}}
    } catch (err) {
      console.log(err);
    }
  };

  return (
    <>
      <Button type="primary" onClick={showModal}>
        Open Modal
      </Button>
      <Modal
        width={300}
        title="User"
        visible={isModalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        footer={[
          <Button key="back" onClick={handleCancel}>
            Cancel
          </Button>,
          <Button key="submit" type="primary" onClick={onFinish}>
            Submit
          </Button>,
        ]}
      >
        <Form
          form={form}
          name="form-name"
          layout="vertical"
          initialValues={initialValues}
          onFinish={onFinish}
        >
          <Form.Item name="name" label="Name">
            <Input placeholder="Name" title="Name" />
          </Form.Item>
          <Form.Item name="gender" label="Gender">
            <Select>
              <Select.Option value="female">Female</Select.Option>
              <Select.Option value="male">Male</Select.Option>
            </Select>
          </Form.Item>
          <Form.Item label="Birthday">
            <DatePicker />
          </Form.Item>
        </Form>
      </Modal>
    </>
  );
}


<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

为 Form.Item 添加名称 属性 并在 OnFinish() 函数中将日期转换为所需格式

import { useState } from "react";
import { Modal, Row, Col, Form, Input, DatePicker, Select, Button } from "antd";
import "./styles.css";

export default function App() {
  const [form] = Form.useForm();
  const [isModalVisible, setIsModalVisible] = useState<boolean>(false);

  const initialValues = {
  name: "Wagner Fillio",
  gender: "male",
  birthday: "2022-02-20"
};

 const showModal = () => {
 setIsModalVisible(true);
};

 const handleOk = () => {
 setIsModalVisible(false);
};

 const handleCancel = () => {
  form.resetFields();
  setIsModalVisible(false);
};

const onFinish = async () => {
  try {
    const values = await form.validateFields();
    values.birthday = values['birthday'].format('YYYY-MM-DD')
    console.log("Success:", values);

  // Intended object
  // {{name: 'Wagner Fillio', gender: 'male', birthday: '2022-02-20'}}
} catch (err) {
  console.log(err);
}
};

return (
<>
  <Button type="primary" onClick={showModal}>
    Open Modal
  </Button>
  <Modal
    width={300}
    title="User"
    visible={isModalVisible}
    onOk={handleOk}
    onCancel={handleCancel}
    footer={[
      <Button key="back" onClick={handleCancel}>
        Cancel
      </Button>,
      <Button key="submit" type="primary" onClick={onFinish}>
        Submit
      </Button>
    ]}
  >
    <Form
      form={form}
      name="form-name"
      layout="vertical"
      initialValues={initialValues}
      onFinish={onFinish}
    >
      <Form.Item name="name" label="Name">
        <Input placeholder="Name" title="Name" />
      </Form.Item>
      <Form.Item name="gender" label="Gender">
        <Select>
          <Select.Option value="female">Female</Select.Option>
          <Select.Option value="male">Male</Select.Option>
        </Select>
      </Form.Item>
      <Form.Item name="birthday" label="Birthday">
        <DatePicker />
      </Form.Item>
    </Form>
  </Modal>
</>
);
}