import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react' import { createPortal } from 'react-dom' import { X, Plus, Trash2, ChevronUp, ChevronDown, Loader2 } from 'lucide-react' import { cx } from './lib' /* ---------- 提示 ---------- */ type Toast = { id: number; text: string; tone: 'info' | 'ok' | 'err' } const ToastCtx = createContext<(text: string, tone?: Toast['tone']) => void>(() => {}) export const useToast = () => useContext(ToastCtx) export function ToastHost({ children }: { children: React.ReactNode }) { const [list, setList] = useState([]) const push = useCallback((text: string, tone: Toast['tone'] = 'info') => { const id = Date.now() + Math.random() setList(l => [...l.slice(-3), { id, text, tone }]) setTimeout(() => setList(l => l.filter(t => t.id !== id)), tone === 'err' ? 5200 : 2800) }, []) return ( {children}
{list.map(t =>
{t.text}
)}
) } /* ---------- 弹窗 ---------- */ export function Modal({ open, onClose, title, children, footer, width = 520, className }: { open: boolean; onClose: () => void; title?: React.ReactNode; children: React.ReactNode; footer?: React.ReactNode; width?: number; className?: string }) { useEffect(() => { if (!open) return const on = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } window.addEventListener('keydown', on) return () => window.removeEventListener('keydown', on) }, [open, onClose]) if (!open) return null return createPortal(
{ if (e.target === e.currentTarget) onClose() }}>
{title != null && (

{title}

)}
{children}
{footer &&
{footer}
}
, document.body) } export function Drawer({ open, onClose, title, children, width = 560, footer }: { open: boolean; onClose: () => void; title?: React.ReactNode; children: React.ReactNode; width?: number; footer?: React.ReactNode }) { useEffect(() => { if (!open) return const on = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } window.addEventListener('keydown', on) return () => window.removeEventListener('keydown', on) }, [open, onClose]) if (!open) return null return createPortal(
{ if (e.target === e.currentTarget) onClose() }}>
, document.body) } /* ---------- 确认 ---------- */ type ConfirmOpts = { title: string; body?: React.ReactNode; ok?: string; danger?: boolean; typeToConfirm?: string } const ConfirmCtx = createContext<(o: ConfirmOpts) => Promise>(async () => false) export const useConfirm = () => useContext(ConfirmCtx) export function ConfirmHost({ children }: { children: React.ReactNode }) { const [state, setState] = useState<(ConfirmOpts & { resolve: (v: boolean) => void }) | null>(null) const [typed, setTyped] = useState('') const ask = useCallback((o: ConfirmOpts) => new Promise(resolve => { setTyped(''); setState({ ...o, resolve }) }), []) const close = (v: boolean) => { state?.resolve(v); setState(null) } const blocked = !!state?.typeToConfirm && typed.trim() !== state.typeToConfirm return ( {children} close(false)} title={state?.title} width={440} footer={<> }> {state?.body &&
{state.body}
} {state?.typeToConfirm && ( )}
) } /* ---------- 小部件 ---------- */ export const Spinner = ({ size = 16, label }: { size?: number; label?: string }) => ( {label && {label}} ) export const Loading = ({ label = '正在加载…' }: { label?: string }) =>
export const ErrorNote = ({ error, onRetry }: { error: string; onRetry?: () => void }) => (
{error}{onRetry && }
) export function Empty({ icon, title, children }: { icon?: React.ReactNode; title: string; children?: React.ReactNode }) { return (
{icon &&
{icon}
}
{title}
{children &&
{children}
}
) } export function Tabs({ value, onChange, items, className }: { value: T; onChange: (v: T) => void; items: { id: T; label: React.ReactNode; count?: number }[]; className?: string }) { const ref = useRef(null) const [edge, setEdge] = useState('') useEffect(() => { const el = ref.current; if (!el) return const upd = () => { const l = el.scrollLeft > 2, r = el.scrollLeft + el.clientWidth < el.scrollWidth - 2 setEdge(l && r ? 'both' : l ? 'l' : r ? 'r' : '') } upd() el.addEventListener('scroll', upd, { passive: true }) const ro = new ResizeObserver(upd); ro.observe(el) return () => { el.removeEventListener('scroll', upd); ro.disconnect() } }, [items.length]) useEffect(() => { const el = ref.current; const on = el?.querySelector('.tab.on') if (!el || !on || el.scrollWidth <= el.clientWidth) return const l = on.offsetLeft - el.offsetLeft, r = l + on.offsetWidth if (l < el.scrollLeft + 24 || r > el.scrollLeft + el.clientWidth - 24) el.scrollTo({ left: l - (el.clientWidth - on.offsetWidth) / 2, behavior: 'smooth' }) }, [value]) const onKey = (e: React.KeyboardEvent) => { if (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft') return const i = items.findIndex(it => it.id === value); const n = items[(i + (e.key === 'ArrowRight' ? 1 : items.length - 1)) % items.length] if (n) { e.preventDefault(); onChange(n.id); setTimeout(() => ref.current?.querySelector('.tab.on')?.focus(), 0) } } return (
{items.map(it => ( ))}
) } export function Segmented({ value, onChange, items }: { value: T; onChange: (v: T) => void; items: { id: T; label: React.ReactNode }[] }) { return (
{items.map(it => )}
) } export function Field({ label, hint, children, className }: { label: React.ReactNode; hint?: React.ReactNode; children: React.ReactNode; className?: string }) { return ( ) } /* 自动增高文本框 */ export function AutoText({ value, onChange, minRows = 2, className, ...rest }: { value: string; onChange: (v: string) => void; minRows?: number; className?: string } & Omit, 'value' | 'onChange'>) { // 自动撑高到能显示全部文字;右下角可以往下拖,拖大后的高度会保留 const ref = useRef(null) const manual = useRef(0) const fit = () => { const el = ref.current if (!el || !el.offsetParent) return el.style.height = 'auto' el.style.height = `${Math.max(el.scrollHeight + 2, manual.current)}px` } useEffect(fit, [value]) useEffect(() => { const el = ref.current if (!el || typeof ResizeObserver === 'undefined') return let w = el.clientWidth const ro = new ResizeObserver(() => { if (el.clientWidth !== w) { w = el.clientWidth; fit() } else if (!el.style.height || el.style.height === 'auto') fit() }) ro.observe(el) return () => ro.disconnect() }, []) const onUp = () => { const el = ref.current; if (el && el.offsetHeight > el.scrollHeight + 4) manual.current = el.offsetHeight } return