Feedback.tsx
6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import { Button } from './Button';
import { Icon } from './Icon';
import type { IconName } from './Icon';
import styles from './Shared.module.css';
import { cx } from './utils';
export interface AlertProps {
/** Alert 用于页面内反馈,type 决定语义色,不承载业务状态计算。 */
type?: 'info' | 'success' | 'warning' | 'error';
title?: ReactNode;
closable?: boolean;
onClose?: () => void;
children?: ReactNode;
}
export function Alert({ children, closable, onClose, title, type = 'info' }: AlertProps) {
return (
<div className={cx(styles.card, styles[type])} role="alert">
<div className={styles.between}>
<strong>{title}</strong>
{closable ? <button className={styles.plainButton} type="button" aria-label={`关闭${title}`} onClick={onClose}>×</button> : null}
</div>
{children}
</div>
);
}
export interface MessageItem {
/** key 用于区分消息实例,并作为自动关闭和手动关闭回调的稳定标识。 */
key: string;
type?: 'info' | 'success' | 'warning' | 'error';
content: ReactNode;
duration?: number;
closable?: boolean;
onClose?: (key: string) => void;
}
export interface MessageProps {
/** Message 用于右上角全局轻量反馈,不打断用户当前操作流。 */
items: MessageItem[];
defaultDuration?: number;
onClose?: (key: string) => void;
}
const messageIcon: Record<Required<MessageItem>['type'], { label: string; name: IconName }> = {
error: { label: '错误提示', name: 'close-circle' },
info: { label: '信息提示', name: 'info-circle' },
success: { label: '成功提示', name: 'check-circle' },
warning: { label: '警告提示', name: 'warning-circle' },
};
function MessageNotice({ defaultDuration, item, onClose }: { defaultDuration: number; item: MessageItem; onClose?: (key: string) => void }) {
const [visible, setVisible] = useState(true);
const type = item.type ?? 'info';
const close = () => {
setVisible(false);
item.onClose?.(item.key);
onClose?.(item.key);
};
useEffect(() => {
const duration = item.duration ?? defaultDuration;
if (duration <= 0) return undefined;
const timer = window.setTimeout(close, duration);
return () => window.clearTimeout(timer);
}, [defaultDuration, item.duration, item.key]);
if (!visible) return null;
return (
<div className={cx(styles.messageItem, styles[type])} role={type === 'error' ? 'alert' : 'status'} aria-label={String(item.content)}>
<span className={styles.messageContent}>
<Icon className={styles.messageIcon} label={messageIcon[type].label} name={messageIcon[type].name} size={16} />
<span>{item.content}</span>
</span>
{item.closable ? (
<button className={styles.plainButton} type="button" aria-label={`关闭${item.content}`} onClick={close}>
×
</button>
) : null}
</div>
);
}
export function Message({ defaultDuration = 3000, items, onClose }: MessageProps) {
return (
<div className={styles.messageTopRight} role="region" aria-label="全局消息提示">
{items.map((item) => (
<MessageNotice defaultDuration={defaultDuration} item={item} key={item.key} onClose={onClose} />
))}
</div>
);
}
export interface TooltipProps {
/** Tooltip 只展示辅助说明,不承载可点击操作。 */
content: ReactNode;
children: ReactNode;
}
export function Tooltip({ children, content }: TooltipProps) {
const [open, setOpen] = useState(false);
return (
<span className={styles.stack} onMouseEnter={() => setOpen(true)} onFocus={() => setOpen(true)}>
{children}
{open ? <span className={styles.popup} role="tooltip">{content}</span> : null}
</span>
);
}
export interface PopconfirmProps {
/** 二次确认用于破坏性或高风险操作,确认和取消都交给业务侧处理。 */
title: ReactNode;
children: ReactNode;
onConfirm?: () => void;
onCancel?: () => void;
}
export function Popconfirm({ children, onCancel, onConfirm, title }: PopconfirmProps) {
const [open, setOpen] = useState(false);
return (
<span className={styles.stack}>
<span onClick={() => setOpen(true)}>{children}</span>
{open ? (
<span className={cx(styles.popup, styles.popconfirmPopup)} role="dialog" aria-label={String(title)}>
<strong>{title}</strong>
<span className={styles.popconfirmActions} data-testid="popconfirm-actions">
<Button size="small" onClick={() => { setOpen(false); onCancel?.(); }}>取消</Button>
<Button size="small" variant="primary" onClick={() => { setOpen(false); onConfirm?.(); }}>确认</Button>
</span>
</span>
) : null}
</span>
);
}
export interface ModalProps {
/** Modal 是受控浮层,open 为唯一显示来源,避免内部状态和业务状态分叉。 */
open?: boolean;
title: ReactNode;
confirmLoading?: boolean;
footer?: ReactNode;
children?: ReactNode;
onOk?: () => void;
onCancel?: () => void;
}
export function Modal({ children, confirmLoading, footer, onCancel, onOk, open, title }: ModalProps) {
if (!open) return null;
return (
<div className={styles.overlay}>
<section className={styles.dialog} role="dialog" aria-modal="true" aria-label={String(title)}>
<header className={cx(styles.section, styles.between)}>
<strong>{title}</strong>
<button className={styles.plainButton} type="button" aria-label={`关闭${title}`} onClick={onCancel}>×</button>
</header>
<div className={styles.section}>{children}</div>
<footer className={cx(styles.section, styles.row)}>
{footer ?? (
<>
<Button onClick={onCancel}>取消</Button>
<button className={styles.control} type="button" aria-busy={confirmLoading || undefined} onClick={onOk}>确定</button>
</>
)}
</footer>
</section>
</div>
);
}
export interface DrawerProps {
/** Drawer 保留底层页面上下文,适合列表页侧边编辑或详情查看。 */
open?: boolean;
title: ReactNode;
placement?: 'left' | 'right';
footer?: ReactNode;
children?: ReactNode;
onClose?: () => void;
}
export function Drawer({ children, footer, onClose, open, placement = 'right', title }: DrawerProps) {
if (!open) return null;
return (
<aside className={styles.drawer} style={placement === 'left' ? { left: 0, right: 'auto' } : undefined} role="dialog" aria-label={String(title)}>
<header className={cx(styles.section, styles.between)}>
<strong>{title}</strong>
<button className={styles.plainButton} type="button" aria-label={`关闭${title}`} onClick={onClose}>×</button>
</header>
<div className={styles.section}>{children}</div>
{footer ? <footer className={styles.section}>{footer}</footer> : null}
</aside>
);
}