ConfirmDialog.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React from 'react';
  2. import { AlertCircle, X } from 'lucide-react';
  3. export interface ConfirmDialogProps {
  4. isOpen: boolean;
  5. title?: string;
  6. message: string;
  7. confirmText?: string;
  8. cancelText?: string;
  9. danger?: boolean;
  10. onConfirm: () => void;
  11. onCancel: () => void;
  12. }
  13. const ConfirmDialog: React.FC<ConfirmDialogProps> = ({
  14. isOpen,
  15. title = '确认操作',
  16. message,
  17. confirmText = '确定',
  18. cancelText = '取消',
  19. danger = false,
  20. onConfirm,
  21. onCancel,
  22. }) => {
  23. if (!isOpen) return null;
  24. return (
  25. <div className="fixed inset-0 z-50 flex items-center justify-center">
  26. {/* 背景遮罩 */}
  27. <div
  28. className="absolute inset-0 bg-black/50 backdrop-blur-sm"
  29. onClick={onCancel}
  30. />
  31. {/* 对话框 */}
  32. <div className="relative bg-white rounded-2xl shadow-2xl max-w-md w-full mx-4 animate-in fade-in zoom-in duration-200">
  33. {/* 关闭按钮 */}
  34. <button
  35. onClick={onCancel}
  36. className="absolute top-4 right-4 text-gray-400 hover:text-gray-600 transition-colors"
  37. aria-label="关闭"
  38. >
  39. <X className="w-5 h-5" />
  40. </button>
  41. {/* 内容 */}
  42. <div className="p-6">
  43. <div className="flex items-start space-x-4">
  44. <div className={`flex-shrink-0 w-10 h-10 rounded-full flex items-center justify-center ${
  45. danger ? 'bg-red-100' : 'bg-blue-100'
  46. }`}>
  47. <AlertCircle className={`w-6 h-6 ${danger ? 'text-red-600' : 'text-blue-600'}`} />
  48. </div>
  49. <div className="flex-1 pt-1">
  50. <h3 className="text-lg font-semibold text-gray-900 mb-2">
  51. {title}
  52. </h3>
  53. <p className="text-sm text-gray-600 leading-relaxed whitespace-pre-wrap">
  54. {message}
  55. </p>
  56. </div>
  57. </div>
  58. </div>
  59. {/* 按钮 */}
  60. <div className="px-6 pb-6 flex items-center justify-end space-x-3">
  61. <button
  62. onClick={onCancel}
  63. className="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 transition-colors"
  64. >
  65. {cancelText}
  66. </button>
  67. <button
  68. onClick={onConfirm}
  69. className={`px-4 py-2 text-sm font-medium text-white rounded-lg transition-colors ${
  70. danger
  71. ? 'bg-red-600 hover:bg-red-700'
  72. : 'bg-blue-600 hover:bg-blue-700'
  73. }`}
  74. >
  75. {confirmText}
  76. </button>
  77. </div>
  78. </div>
  79. </div>
  80. );
  81. };
  82. export default ConfirmDialog;