Display.tsx 5.2 KB
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.tagCloseButton}
          type="button"
          aria-label={`关闭${children}`}
          onClick={(event) => {
            event.stopPropagation();
            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>;
}