Skip to content

Commit 4402a94

Browse files
committed
test antd-message
1 parent f656322 commit 4402a94

File tree

6 files changed

+1130
-8
lines changed

6 files changed

+1130
-8
lines changed

app/ui/programs/nav-links.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { useState } from 'react';
1010
import Link from 'next/link';
1111
import { usePathname } from 'next/navigation';
1212
import clsx from 'clsx';
13+
import React from 'react';
14+
import { message } from 'antd';
1315

1416
// Map of links to display in the side navigation.
1517
// Depending on the size of the application, this would be stored in a database.
@@ -46,21 +48,23 @@ export default function NavLinks() {
4648
//用fetch向服务器发声POST请求,提交用户输入的内容
4749
const response = await fetch('/api/submi', { // 待替换为服务器API
4850
method: 'POST',
49-
//请求体,将对象转换为json字符串
51+
//请求体
5052
body: formData,
5153
});
5254
//响应处理,根据响应结果显示提示信息,并重置输入框或关闭弹窗
5355
if (response.ok) {
54-
alert('内容提交成功!');//提交成功后重置输入框的值,并关闭弹窗
56+
message.success('提交成功');//提交成功后重置输入框的值,并关闭弹窗
57+
console.log('提交成功');
5558
setInputValue('');
5659
setFile(null);
5760
setModalOpen(false);
5861
} else {
59-
alert('提交失败,请重试。');
62+
message.warning('提交失败');
63+
console.log('提交失败');
6064
}
6165
} catch (error) {
62-
console.error('提交错误:', error);
63-
alert('提交失败,请检查网络连接。');
66+
message.error('提交失败,请检查网络设置');
67+
console.log('提交失败,请检查网络连接。');
6468
}
6569
};
6670

components/MessageContext.tsx

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// MessageContext.tsx
2+
import React, { createContext, useContext, ReactNode } from 'react';
3+
import { message } from 'antd';
4+
5+
// 定义消息上下文的类型
6+
interface MessageContextType {
7+
success: () => void;
8+
error: () => void;
9+
warning: () => void;
10+
}
11+
12+
// 创建上下文,默认值为 null
13+
const MessageContext = createContext<MessageContextType | null>(null);
14+
15+
// 定义 MessageProvider 的属性类型
16+
interface MessageProviderProps {
17+
children: ReactNode; // 允许任意子元素
18+
}
19+
20+
// 创建 MessageProvider 组件
21+
export const MessageProvider: React.FC<MessageProviderProps> = ({ children }) => {
22+
const success = () => {
23+
message.success('这是一个成功消息!');
24+
};
25+
26+
const error = () => {
27+
message.error('这是一个错误消息!');
28+
};
29+
30+
const warning = () => {
31+
message.warning('这是一个警告消息!');
32+
};
33+
34+
return (
35+
<MessageContext.Provider value={{ success, error, warning }}>
36+
{children}
37+
</MessageContext.Provider>
38+
);
39+
};
40+
41+
// 自定义 hook 使用上下文
42+
export const useMessage = () => {
43+
const context = useContext(MessageContext);
44+
45+
return context;
46+
};

layout.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import '@/app/ui/global.css';
22
import { inter } from '@/app/ui/fonts';
3-
3+
import React from 'react';
4+
import { message } from 'antd';
45

56
export default function RootLayout({
67
children,

0 commit comments

Comments
 (0)