Display.tsx
5.04 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
import type { ReactNode } from 'react';
import styles from './Shared.module.css';
import { cx } from './utils';
export interface CardProps {
/** Card 仅作为内容容器,不嵌套页面大区块。 */
title?: ReactNode;
extra?: ReactNode;
children?: ReactNode;
}
export function Card({ children, extra, title }: CardProps) {
return (
<section className={styles.card}>
{(title || extra) ? <header className={styles.between}><strong>{title}</strong>{extra}</header> : null}
{children}
</section>
);
}
export interface TagProps {
/** Tag 表达小粒度状态,closable 时只通知业务侧关闭意图。 */
status?: 'default' | 'success' | 'warning' | 'error' | 'info';
closable?: boolean;
onClose?: () => void;
children?: ReactNode;
}
export function Tag({ children, closable, onClose, status = 'default' }: TagProps) {
return (
<span className={cx(styles.tag, status !== 'default' && styles[status])}>
{children}
{closable ? <button className={styles.plainButton} type="button" aria-label={`关闭${children}`} onClick={onClose}>×</button> : null}
</span>
);
}
export interface BadgeProps {
count?: number;
dot?: boolean;
status?: 'success' | 'processing' | 'error' | 'warning';
children?: ReactNode;
}
export function Badge({ children, count, dot, status }: BadgeProps) {
return (
<span className={styles.row}>
{children}
{dot ? <span className={cx(styles.statusDot, styles.info)}>{status === 'processing' ? '●' : '•'}</span> : null}
{count !== undefined ? <span className={cx(styles.tag, styles.error)}>{count}</span> : null}
</span>
);
}
export interface AvatarProps {
/** Avatar 文本态使用姓名首字作为圆形头像,视觉上固定为主色蓝底白字。 */
name?: string;
src?: string;
}
export function Avatar({ name = '?', src }: AvatarProps) {
return src ? (
<img className={cx(styles.avatar, styles.avatarCircle)} src={src} alt={name} width={32} height={32} />
) : (
<span className={cx(styles.avatar, styles.avatarCircle, styles.avatarText)} aria-label={name}>
{name.slice(0, 1)}
</span>
);
}
export interface AvatarGroupProps {
maxCount?: number;
children: ReactNode[];
}
export function AvatarGroup({ children, maxCount = children.length }: AvatarGroupProps) {
const visible = children.slice(0, maxCount);
const rest = children.length - visible.length;
return <span className={styles.avatarGroup}>{visible}{rest > 0 ? <span className={cx(styles.avatar, styles.avatarCircle, styles.avatarText)}>+{rest}</span> : null}</span>;
}
export interface StatisticProps {
title: ReactNode;
value: number | string;
prefix?: ReactNode;
suffix?: ReactNode;
trend?: 'up' | 'down';
}
export function Statistic({ prefix, suffix, title, trend, value }: StatisticProps) {
return <div className={styles.stack}><span className={styles.muted}>{title}</span><strong>{prefix}{value}{suffix}</strong>{trend ? <span>{trend === 'up' ? '上升' : '下降'}</span> : null}</div>;
}
export interface ProgressProps {
/** percentPosition 控制百分比文案相对进度条的位置,none 可用于紧凑场景。 */
percent: number;
status?: 'normal' | 'active' | 'success' | 'error';
percentPosition?: 'left' | 'right' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'none';
}
const progressPositionClass = {
left: styles.progressLeft,
right: styles.progressRight,
'top-left': styles.progressTopLeft,
'top-right': styles.progressTopRight,
'bottom-left': styles.progressBottomLeft,
'bottom-right': styles.progressBottomRight,
none: styles.progressNone,
};
export function Progress({ percent, percentPosition = 'right', status = 'normal' }: ProgressProps) {
const safePercent = Math.min(100, Math.max(0, percent));
const percentNode = percentPosition === 'none' ? null : <span className={styles.progressPercent}>{percent}%</span>;
return (
<div className={cx(styles.progressRoot, progressPositionClass[percentPosition])} data-testid="progress-root">
{(percentPosition === 'top-left' || percentPosition === 'top-right') ? percentNode : null}
<div className={styles.progressTrack} role="progressbar" aria-valuemin={0} aria-valuemax={100} aria-valuenow={safePercent}>
<div className={cx(styles.progressFill, status === 'error' && styles.progressError, status === 'success' && styles.progressSuccess)} data-testid="progress-fill" style={{ width: `${safePercent}%` }} />
</div>
{(percentPosition === 'left' || percentPosition === 'right' || percentPosition === 'bottom-left' || percentPosition === 'bottom-right') ? percentNode : null}
</div>
);
}
export interface TimelineItem {
title: ReactNode;
time?: ReactNode;
description?: ReactNode;
status?: 'done' | 'active' | 'wait' | 'error';
}
export function Timeline({ items }: { items: TimelineItem[] }) {
return <ol className={styles.stack}>{items.map((item, index) => <li key={index}><strong>{item.title}</strong> <span className={styles.muted}>{item.time}</span>{item.description ? <div>{item.description}</div> : null}</li>)}</ol>;
}