import type { ProFormColumnsType } from '@ant-design/pro-components'; import { BetaSchemaForm } from '@ant-design/pro-components'; import { Button, type ColProps, Drawer, Space, Typography } from 'antd'; import type { FormInstance } from 'antd/lib'; import React, { forwardRef, useImperativeHandle } from 'react'; interface ConfigurableDrawerFormProps { title?: string; columns: ProFormColumnsType[]; onSubmit?: (values: any) => Promise; initialValues?: Record; width?: number | string; labelCol?: ColProps; wrapperCol?: ColProps; } export interface ConfigurableDrawerFormRef { open: (data?: Record) => void; close: () => void; } const ConfigurableDrawerForm = forwardRef< ConfigurableDrawerFormRef, ConfigurableDrawerFormProps >( ( { title = '表单', labelCol = { span: 4 }, wrapperCol = { span: 20 }, columns, onSubmit, initialValues, width = 600, }, ref, ) => { const [open, setOpen] = React.useState(false); const [formData, setFormData] = React.useState(initialValues || {}); const [loading, setLoading] = React.useState(false); // 添加表单实例引用 const formRef = React.useRef(null); useImperativeHandle(ref, () => ({ open: (data) => { if (data) { setFormData(data); } console.log('open'); setOpen(true); }, close: () => setOpen(false), })); const handleSubmit = async () => { try { if (onSubmit) { await formRef.current?.validateFields(); setLoading(true); const values = formRef.current?.getFieldsValue(); const success = await onSubmit(values); if (success) { setOpen(false); return true; } return false; } return true; } finally { setLoading(false); } }; // const customHeader = ( //
// // {title} // //
// ); return ( setOpen(false)} width={width} footer={ } > {/* {customHeader} */} ); }, ); export default ConfigurableDrawerForm;