新增提示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
+204
View File
@@ -0,0 +1,204 @@
import { useState } from 'react';
import { MessageModal, messageService, MessageModalProvider } from '~/components/ui/MessageModal';
import type { MessageType } from '~/components/ui/MessageModal';
import { LinksFunction } from '@remix-run/node';
import messageModalStyles from '~/styles/components/message-modal.css?url';
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: messageModalStyles },
];
export default function MessageModalExample() {
const [isModalOpen, setIsModalOpen] = useState(false);
const [modalType, setModalType] = useState<MessageType>('info');
const [modalTitle, setModalTitle] = useState('');
const [modalMessage, setModalMessage] = useState('');
const [withConfirm, setWithConfirm] = useState(false);
const [autoClose, setAutoClose] = useState(false);
// 打开普通模态框
const openModal = (type: MessageType, title: string, message: string) => {
setModalType(type);
setModalTitle(title);
setModalMessage(message);
setIsModalOpen(true);
};
// 打开各类型的服务提示框
const showSuccessMessage = () => {
messageService.success('操作成功完成!', {
title: '成功提示',
autoClose: true
});
};
const showErrorMessage = () => {
messageService.error('操作过程中发生错误,请重试。', {
title: '错误提示'
});
};
const showWarningMessage = () => {
messageService.warning('此操作可能产生不可逆转的结果。', {
title: '警告提示',
onConfirm: () => {
messageService.success('您已确认继续操作')
},
confirmText: '继续操作',
cancelText: '取消'
});
};
const showInfoMessage = () => {
messageService.info('系统将于今晚10点进行升级维护。', {
title: '通知',
autoClose: true,
autoCloseDelay: 5000
});
};
const showCustomMessage = () => {
messageService.show({
title: '自定义消息',
message: '这是一个带有自定义内容的消息',
type: 'info',
confirmText: '了解',
children: (
<div style={{
padding: '10px',
backgroundColor: '#f0f0f0',
borderRadius: '6px',
marginTop: '10px'
}}>
<p></p>
<div style={{
display: 'flex',
alignItems: 'center',
marginTop: '10px'
}}>
<i className="ri-information-line" style={{ marginRight: '8px', color: '#1890ff' }}></i>
<span></span>
</div>
</div>
)
});
};
// 处理确认
const handleConfirm = () => {
messageService.success('您点击了确认按钮!', { autoClose: true });
setIsModalOpen(false);
};
return (
<MessageModalProvider>
<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">使MessageModal组件来控制模态框的显示和隐藏</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={() => openModal('info', '信息提示', '这是一个普通的信息提示框')}
>
</button>
<button
className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600"
onClick={() => openModal('success', '成功提示', '操作已成功完成')}
>
</button>
<button
className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600"
onClick={() => openModal('error', '错误提示', '操作失败,请检查输入')}
>
</button>
<button
className="px-4 py-2 bg-yellow-500 text-white rounded hover:bg-yellow-600"
onClick={() => openModal('warning', '警告提示', '此操作将删除数据,是否继续?')}
>
</button>
</div>
<div className="flex flex-wrap gap-4 mb-4">
<label className="flex items-center">
<input
type="checkbox"
checked={withConfirm}
onChange={(e) => setWithConfirm(e.target.checked)}
className="mr-2"
/>
</label>
<label className="flex items-center">
<input
type="checkbox"
checked={autoClose}
onChange={(e) => setAutoClose(e.target.checked)}
className="mr-2"
/>
(3)
</label>
</div>
<MessageModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
title={modalTitle}
message={modalMessage}
type={modalType}
autoClose={autoClose}
onConfirm={withConfirm ? handleConfirm : undefined}
confirmText={withConfirm ? "确认" : "我知道了"}
cancelText="取消"
/>
</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">使messageService可以在任何组件中方便地显示消息提示</p>
<div className="flex flex-wrap gap-3">
<button
className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600"
onClick={showSuccessMessage}
>
()
</button>
<button
className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600"
onClick={showErrorMessage}
>
</button>
<button
className="px-4 py-2 bg-yellow-500 text-white rounded hover:bg-yellow-600"
onClick={showWarningMessage}
>
()
</button>
<button
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
onClick={showInfoMessage}
>
(5)
</button>
<button
className="px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600"
onClick={showCustomMessage}
>
</button>
</div>
</div>
</div>
</MessageModalProvider>
);
}