Skip to content

Commit 826db06

Browse files
committed
feat: 添加事件总线
1 parent 5a45969 commit 826db06

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { useEmit, useOn } from '@sa/hooks';
2+
3+
const { Text, Title } = ATypography;
4+
5+
// Sender 组件:负责发送消息
6+
function Sender() {
7+
const emit = useEmit();
8+
const sendMessage = () => {
9+
emit('message', 'Hello from Sender!', new Date().toLocaleTimeString());
10+
};
11+
12+
return (
13+
<ACard
14+
className="shadow-sm transition-shadow duration-300 hover:shadow-md"
15+
title={
16+
<ASpace>
17+
<IconAntDesignSendOutlined className="text-blue-500" />
18+
<Text strong>Sender Component</Text>
19+
</ASpace>
20+
}
21+
>
22+
<AButton
23+
className="w-full"
24+
icon={<IconAntDesignSendOutlined />}
25+
size="large"
26+
type="primary"
27+
onClick={sendMessage}
28+
>
29+
Send Message
30+
</AButton>
31+
</ACard>
32+
);
33+
}
34+
35+
// Receiver 组件:负责接收消息
36+
function Receiver() {
37+
const [message, setMessage] = useState<string>('');
38+
const [time, setTime] = useState<string>('');
39+
40+
useOn('message', (msg: string, timestamp: string) => {
41+
setMessage(msg);
42+
setTime(timestamp);
43+
});
44+
45+
const MessageDisplay = () => (
46+
<ASpace
47+
className="w-full"
48+
direction="vertical"
49+
>
50+
<div className="rounded-lg">
51+
<Text type="secondary">Message:</Text>
52+
<Text
53+
strong
54+
className="block"
55+
>
56+
{message}
57+
</Text>
58+
<ADivider className="my-2" />
59+
<Text type="secondary">Received at:</Text>
60+
<Text className="block">{time}</Text>
61+
</div>
62+
</ASpace>
63+
);
64+
65+
const EmptyMessage = () => (
66+
<div className="py-4 text-center text-gray-400">
67+
<div className="mb-2 text-2xl">
68+
<IconAntDesignInboxOutlined />
69+
</div>
70+
<Text type="secondary">No message received.</Text>
71+
</div>
72+
);
73+
74+
return (
75+
<ACard
76+
className="shadow-sm transition-shadow duration-300 hover:shadow-md"
77+
title={
78+
<ASpace>
79+
<IconAntDesignInboxOutlined className="text-green-500" />
80+
<Text strong>Receiver Component</Text>
81+
</ASpace>
82+
}
83+
>
84+
{message ? <MessageDisplay /> : <EmptyMessage />}
85+
</ACard>
86+
);
87+
}
88+
89+
const EventBusDemo = () => {
90+
return (
91+
<ACard
92+
className="h-full card-wrapper"
93+
size="small"
94+
variant="borderless"
95+
>
96+
<Title
97+
className="mb-8 text-center"
98+
level={2}
99+
>
100+
Event Bus Example: Sibling Communication
101+
</Title>
102+
<ASpace
103+
className="w-full"
104+
direction="vertical"
105+
size="large"
106+
>
107+
<Sender />
108+
<Receiver />
109+
</ASpace>
110+
</ACard>
111+
);
112+
};
113+
114+
export const handle = {
115+
i18nKey: 'route.function_event-bus',
116+
icon: 'ant-design:send-outlined',
117+
title: 'function_event-bus'
118+
};
119+
120+
export default EventBusDemo;

src/pages/(base)/projects/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Projects = () => {
44

55
export const handle = {
66
i18nKey: 'route.(base)_projects',
7-
order: 6,
7+
order: 10,
88
title: '(base)_projects'
99
};
1010

0 commit comments

Comments
 (0)