NotificationExample.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * 通知系统使用示例
  3. *
  4. * 这个文件展示了如何在组件中使用 useToast 和 useConfirm
  5. */
  6. import React from 'react';
  7. import { useToast } from '../contexts/NotificationContext';
  8. import { useConfirm } from '../contexts/NotificationContext';
  9. const NotificationExample: React.FC = () => {
  10. const { showToast } = useToast();
  11. const { showConfirm } = useConfirm();
  12. // ============ Toast 使用示例 ============
  13. // 替换 alert('操作成功')
  14. const handleSuccess = () => {
  15. showToast('操作成功', 'success');
  16. };
  17. // 替换 alert('操作失败')
  18. const handleError = () => {
  19. showToast('操作失败,请重试', 'error');
  20. };
  21. // ============ Confirm 使用示例 ============
  22. // 替换 if (!confirm('确定删除吗?')) return;
  23. const handleDelete = async () => {
  24. const confirmed = await showConfirm({
  25. title: '确认删除',
  26. message: '确定要删除这条记录吗?',
  27. confirmText: '删除',
  28. cancelText: '取消',
  29. danger: true
  30. });
  31. if (confirmed) {
  32. // 执行删除操作
  33. showToast('删除成功', 'success');
  34. }
  35. };
  36. // 普通确认对话框
  37. const handleSave = async () => {
  38. const confirmed = await showConfirm({
  39. title: '保存更改',
  40. message: '确定要保存当前的更改吗?',
  41. confirmText: '保存',
  42. cancelText: '取消'
  43. });
  44. if (confirmed) {
  45. // 执行保存操作
  46. showToast('保存成功', 'success');
  47. }
  48. };
  49. // 多行消息
  50. const handleComplexConfirm = async () => {
  51. const confirmed = await showConfirm({
  52. title: '批量删除',
  53. message: '确定要删除选中的 5 个文档吗?\n\n此操作不可恢复,请谨慎操作。',
  54. confirmText: '确定删除',
  55. cancelText: '取消',
  56. danger: true
  57. });
  58. if (confirmed) {
  59. showToast('批量删除成功', 'success');
  60. }
  61. };
  62. return (
  63. <div className="p-8 space-y-4">
  64. <h2 className="text-2xl font-bold mb-6">通知系统使用示例</h2>
  65. <div className="space-y-2">
  66. <h3 className="text-lg font-semibold">Toast 示例</h3>
  67. <button
  68. onClick={handleSuccess}
  69. className="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 mr-2"
  70. >
  71. 显示成功提示
  72. </button>
  73. <button
  74. onClick={handleError}
  75. className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700"
  76. >
  77. 显示错误提示
  78. </button>
  79. </div>
  80. <div className="space-y-2">
  81. <h3 className="text-lg font-semibold">Confirm 示例</h3>
  82. <button
  83. onClick={handleDelete}
  84. className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 mr-2"
  85. >
  86. 删除操作(危险)
  87. </button>
  88. <button
  89. onClick={handleSave}
  90. className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 mr-2"
  91. >
  92. 保存操作(普通)
  93. </button>
  94. <button
  95. onClick={handleComplexConfirm}
  96. className="px-4 py-2 bg-orange-600 text-white rounded-lg hover:bg-orange-700"
  97. >
  98. 复杂确认
  99. </button>
  100. </div>
  101. </div>
  102. );
  103. };
  104. export default NotificationExample;