/** * 通知系统使用示例 * * 这个文件展示了如何在组件中使用 useToast 和 useConfirm */ import React from 'react'; import { useToast } from '../contexts/NotificationContext'; import { useConfirm } from '../contexts/NotificationContext'; const NotificationExample: React.FC = () => { const { showToast } = useToast(); const { showConfirm } = useConfirm(); // ============ Toast 使用示例 ============ // 替换 alert('操作成功') const handleSuccess = () => { showToast('操作成功', 'success'); }; // 替换 alert('操作失败') const handleError = () => { showToast('操作失败,请重试', 'error'); }; // ============ Confirm 使用示例 ============ // 替换 if (!confirm('确定删除吗?')) return; const handleDelete = async () => { const confirmed = await showConfirm({ title: '确认删除', message: '确定要删除这条记录吗?', confirmText: '删除', cancelText: '取消', danger: true }); if (confirmed) { // 执行删除操作 showToast('删除成功', 'success'); } }; // 普通确认对话框 const handleSave = async () => { const confirmed = await showConfirm({ title: '保存更改', message: '确定要保存当前的更改吗?', confirmText: '保存', cancelText: '取消' }); if (confirmed) { // 执行保存操作 showToast('保存成功', 'success'); } }; // 多行消息 const handleComplexConfirm = async () => { const confirmed = await showConfirm({ title: '批量删除', message: '确定要删除选中的 5 个文档吗?\n\n此操作不可恢复,请谨慎操作。', confirmText: '确定删除', cancelText: '取消', danger: true }); if (confirmed) { showToast('批量删除成功', 'success'); } }; return (

通知系统使用示例

Toast 示例

Confirm 示例

); }; export default NotificationExample;