import React, { useEffect, useRef, useState } from 'react'; import { SmileOutlined, UserOutlined } from '@ant-design/icons'; import { Avatar, Button, Form, Input, InputNumber, Modal, Space, Typography } from 'antd'; import type { GetRef } from 'antd'; type FormInstance = GetRef; const layout = { labelCol: { span: 8 }, wrapperCol: { span: 16 }, }; const tailLayout = { wrapperCol: { offset: 8, span: 16 }, }; interface UserType { name: string; age: string; } interface ModalFormProps { open: boolean; onCancel: () => void; } // reset form fields when modal is form, closed const useResetFormOnCloseModal = ({ form, open }: { form: FormInstance; open: boolean }) => { const prevOpenRef = useRef(); useEffect(() => { prevOpenRef.current = open; }, [open]); const prevOpen = prevOpenRef.current; useEffect(() => { if (!open && prevOpen) { form.resetFields(); } }, [form, prevOpen, open]); }; const ModalForm: React.FC = ({ open, onCancel }) => { const [form] = Form.useForm(); useResetFormOnCloseModal({ form, open, }); const onOk = () => { form.submit(); }; return (
); }; const App: React.FC = () => { const [open, setOpen] = useState(false); const showUserModal = () => { setOpen(true); }; const hideUserModal = () => { setOpen(false); }; const onFinish = (values: any) => { console.log('Finish:', values); }; return ( { if (name === 'userForm') { const { basicForm } = forms; const users = basicForm.getFieldValue('users') || []; basicForm.setFieldsValue({ users: [...users, values] }); setOpen(false); } }} >
{/* Create a hidden field to make Form instance record this */}
); }; export default App;