新增提示Toast组件

This commit is contained in:
2025-04-21 09:22:13 +08:00
parent 01d93522b8
commit 5c2c367856
36 changed files with 2609 additions and 478 deletions
+164
View File
@@ -0,0 +1,164 @@
import { useState } from 'react';
import { Toast, toastService } from '~/components/ui/Toast';
import type { ToastType } from '~/components/ui/Toast';
import { LinksFunction } from '@remix-run/node';
import toastStyles from '~/styles/components/toast.css?url';
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: toastStyles },
];
export default function ToastExample() {
const [isToastOpen, setIsToastOpen] = useState(false);
const [toastType, setToastType] = useState<ToastType>('info');
const [toastMessage, setToastMessage] = useState('');
const [autoClose, setAutoClose] = useState(true);
// 打开普通通知
const openToast = (type: ToastType, message: string) => {
setToastType(type);
setToastMessage(message);
setIsToastOpen(true);
};
// 使用服务显示不同类型通知
const showSuccessToast = () => {
toastService.success('操作成功完成!');
};
const showErrorToast = () => {
toastService.error('操作过程中发生错误,请重试。');
};
const showWarningToast = () => {
toastService.warning('此操作可能产生不可逆转的结果。');
};
const showInfoToast = () => {
toastService.info('系统将于今晚10点进行升级维护。');
};
// 显示多行文本的长通知
const showLongToast = () => {
toastService.info('这是一个具有很长内容的通知,将自动换行以适应容器宽度,并且最多显示三行,超出部分会被截断。系统会自动处理长文本的换行和截断,确保显示效果一致。');
};
// 短时间内显示多个通知
const showMultipleToasts = () => {
toastService.success('第一条通知');
setTimeout(() => {
toastService.info('第二条通知');
}, 300);
setTimeout(() => {
toastService.warning('第三条通知');
}, 600);
setTimeout(() => {
toastService.error('第四条通知');
}, 900);
};
return (
<div className="container mx-auto p-6">
<h1 className="text-2xl font-bold mb-6"></h1>
<div className="bg-white rounded-lg p-6 shadow-md mb-8">
<h2 className="text-xl font-semibold mb-4"></h2>
<p className="mb-4">使Toast组件来控制通知的显示和隐藏</p>
<div className="flex flex-wrap gap-3 mb-6">
<button
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
onClick={() => openToast('info', '这是一个信息通知')}
>
</button>
<button
className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600"
onClick={() => openToast('success', '操作已成功完成')}
>
</button>
<button
className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600"
onClick={() => openToast('error', '操作失败,请检查输入')}
>
</button>
<button
className="px-4 py-2 bg-yellow-500 text-white rounded hover:bg-yellow-600"
onClick={() => openToast('warning', '请注意,这是一个警告通知')}
>
</button>
</div>
<div className="mb-4">
<label className="flex items-center">
<input
type="checkbox"
checked={autoClose}
onChange={(e) => setAutoClose(e.target.checked)}
className="mr-2"
/>
(3)
</label>
</div>
<Toast
isOpen={isToastOpen}
onClose={() => setIsToastOpen(false)}
message={toastMessage}
type={toastType}
autoClose={autoClose}
/>
</div>
<div className="bg-white rounded-lg p-6 shadow-md mb-8">
<h2 className="text-xl font-semibold mb-4"></h2>
<p className="mb-4">使toastService可以在任何组件中方便地显示通知</p>
<div className="flex flex-wrap gap-3 mb-6">
<button
className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600"
onClick={showSuccessToast}
>
</button>
<button
className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600"
onClick={showErrorToast}
>
</button>
<button
className="px-4 py-2 bg-yellow-500 text-white rounded hover:bg-yellow-600"
onClick={showWarningToast}
>
</button>
<button
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
onClick={showInfoToast}
>
</button>
</div>
<div className="flex flex-wrap gap-3">
<button
className="px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600"
onClick={showLongToast}
>
</button>
<button
className="px-4 py-2 bg-pink-500 text-white rounded hover:bg-pink-600"
onClick={showMultipleToasts}
>
</button>
</div>
</div>
</div>
);
}