import React, { useEffect, useRef, useState } from 'react'; import { SmileOutlined, UserOutlined } from '@ant-design/icons'; import { Avatar, Button, Form, Input, InputNumber, Modal, Typography } from 'antd'; import type { FormInstance } from 'antd/es/form'; 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); } }} >
prevValues.users !== curValues.users} > {({ getFieldValue }) => { const users: UserType[] = getFieldValue('users') || []; return users.length ? (
    {users.map(user => (
  • } /> {user.name} - {user.age}
  • ))}
) : ( ( No user yet. ) ); }}
); }; export default App;