import React, { useState } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, Typography, Box, IconButton, } from '@mui/material'; import { Close as CloseIcon, Info as InfoIcon, CheckCircle as SuccessIcon, Warning as WarningIcon, Error as ErrorIcon, Help as ConfirmIcon, } from '@mui/icons-material'; import { useTranslation } from 'react-i18next'; import { type IDialogInstance } from '../../interfaces/common'; interface DialogComponentProps { dialog: IDialogInstance; onClose: (result: boolean) => void; } const DialogComponent: React.FC = ({ dialog, onClose }) => { const [loading, setLoading] = useState(false); const { t } = useTranslation(); const { config } = dialog; // 获取对话框图标 const getDialogIcon = () => { const iconProps = { sx: { fontSize: 24, mr: 1 } }; switch (config.type) { case 'info': return ; case 'success': return ; case 'warning': return ; case 'error': return ; case 'confirm': return ; default: return null; } }; // 获取确认按钮颜色 const getConfirmButtonColor = () => { switch (config.type) { case 'error': case 'warning': return 'error'; case 'success': return 'success'; case 'info': return 'info'; default: return 'primary'; } }; // 处理确认操作 const handleConfirm = async () => { try { setLoading(true); if (config.onConfirm) { await config.onConfirm(); } onClose(true); } catch (error) { console.error('Dialog confirm error:', error); // 即使出错也关闭对话框,但返回false onClose(false); } finally { setLoading(false); } }; // 处理取消操作 const handleCancel = () => { if (config.onCancel) { config.onCancel(); } onClose(false); }; // 处理遮罩点击 const handleBackdropClick = () => { if (config.maskClosable !== false) { handleCancel(); } }; return ( {/* 标题栏 */} {getDialogIcon()} {config.title || t('dialog.confirm')} {/* 内容区域 */} {config.content} {/* 操作按钮 */} {config.showCancel !== false && ( )} ); }; export default DialogComponent;