Feedback.tsx 6.87 KB
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>
  );
}