release: bump version to 0.66.0
This commit is contained in:
1 parent
e65267fe21
commit
5bf5c73ca0
173 files changed
+8669
-13210
No files matched your search
@@ -0,0 +1,192 @@
|
||||
import {
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
getSortedRowModel,
|
||||
useReactTable,
|
||||
type ColumnDef,
|
||||
type SortingState,
|
||||
} from '@tanstack/react-table'
|
||||
import { ArrowDown, ArrowUp, ChevronsUpDown } from 'lucide-react'
|
||||
import { useMemo, useState } from 'react'
|
||||
import TableScrollRegion from '../../../components/Scrollbar/TableScrollRegion'
|
||||
import { Button } from '../ui/button'
|
||||
|
||||
interface DataTableProps<TData> {
|
||||
columns: Array<ColumnDef<TData>>
|
||||
data: TData[]
|
||||
getRowId?: (row: TData, index: number) => string
|
||||
getRowClassName?: (row: TData) => string | undefined
|
||||
selection?: {
|
||||
selectedRowIds: Set<string>
|
||||
onToggleAllVisible: (rowIds: string[]) => void
|
||||
onToggleRow: (rowId: string, row: TData) => void
|
||||
getCheckboxLabel?: (row: TData) => string
|
||||
isRowSelectable?: (row: TData) => boolean
|
||||
}
|
||||
loading?: boolean
|
||||
emptyText?: string
|
||||
className?: string
|
||||
footer?: React.ReactNode
|
||||
onRowClick?: (row: TData) => void
|
||||
}
|
||||
|
||||
export function DataTable<TData>({
|
||||
columns,
|
||||
data,
|
||||
getRowId,
|
||||
getRowClassName,
|
||||
selection,
|
||||
loading = false,
|
||||
emptyText = '暂无数据',
|
||||
className = '',
|
||||
footer,
|
||||
onRowClick,
|
||||
}: DataTableProps<TData>) {
|
||||
const [sorting, setSorting] = useState<SortingState>([])
|
||||
const memoizedColumns = useMemo(() => columns, [columns])
|
||||
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns: memoizedColumns,
|
||||
state: { sorting },
|
||||
onSortingChange: setSorting,
|
||||
getRowId,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
})
|
||||
const visibleSelectableRows = selection
|
||||
? table.getRowModel().rows.filter((row) => selection.isRowSelectable?.(row.original) ?? true)
|
||||
: []
|
||||
const visibleSelectableIds = visibleSelectableRows.map((row) => row.id)
|
||||
const allVisibleSelected = visibleSelectableIds.length > 0 && visibleSelectableIds.every((rowId) => selection?.selectedRowIds.has(rowId))
|
||||
const someVisibleSelected = visibleSelectableIds.some((rowId) => selection?.selectedRowIds.has(rowId))
|
||||
const columnCount = columns.length + (selection ? 1 : 0)
|
||||
|
||||
return (
|
||||
<div className={`an-data-table ${className}`}>
|
||||
<TableScrollRegion className="an-data-table__scroll" targetSelector=".an-data-table__viewport">
|
||||
<div className="an-data-table__viewport">
|
||||
<table className="an-data-table__table">
|
||||
<thead>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<tr key={headerGroup.id}>
|
||||
{selection ? (
|
||||
<th className="an-data-table__selection-cell">
|
||||
<input
|
||||
type="checkbox"
|
||||
aria-label="选择当前可见数据"
|
||||
checked={allVisibleSelected}
|
||||
disabled={!visibleSelectableIds.length}
|
||||
ref={(element) => {
|
||||
if (element) element.indeterminate = someVisibleSelected && !allVisibleSelected
|
||||
}}
|
||||
onChange={() => selection.onToggleAllVisible(visibleSelectableIds)}
|
||||
/>
|
||||
</th>
|
||||
) : null}
|
||||
{headerGroup.headers.map((header) => {
|
||||
const sorted = header.column.getIsSorted()
|
||||
const stickyEnd = header.column.id === 'actions' || header.column.id === 'action'
|
||||
return (
|
||||
<th key={header.id} style={{ width: header.getSize() }} data-sticky-end={stickyEnd || undefined}>
|
||||
{header.isPlaceholder ? null : (
|
||||
<button
|
||||
type="button"
|
||||
className="an-data-table__sort"
|
||||
disabled={!header.column.getCanSort()}
|
||||
onClick={header.column.getToggleSortingHandler()}
|
||||
>
|
||||
{flexRender(header.column.columnDef.header, header.getContext())}
|
||||
{header.column.getCanSort() ? (
|
||||
sorted === 'asc' ? <ArrowUp size={14} /> : sorted === 'desc' ? <ArrowDown size={14} /> : <ChevronsUpDown size={14} />
|
||||
) : null}
|
||||
</button>
|
||||
)}
|
||||
</th>
|
||||
)
|
||||
})}
|
||||
</tr>
|
||||
))}
|
||||
</thead>
|
||||
<tbody>
|
||||
{loading ? (
|
||||
<tr>
|
||||
<td colSpan={columnCount}>
|
||||
<div className="an-data-table__state">
|
||||
<span className="an-spinner" />
|
||||
加载中
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
) : table.getRowModel().rows.length > 0 ? (
|
||||
table.getRowModel().rows.map((row) => (
|
||||
<tr
|
||||
key={row.id}
|
||||
className={getRowClassName?.(row.original)}
|
||||
onClick={onRowClick ? () => onRowClick(row.original) : undefined}
|
||||
data-clickable={onRowClick ? 'true' : undefined}
|
||||
data-row-id={row.id}
|
||||
>
|
||||
{selection ? (
|
||||
<td className="an-data-table__selection-cell">
|
||||
<input
|
||||
type="checkbox"
|
||||
aria-label={selection.getCheckboxLabel?.(row.original) || '选择行'}
|
||||
checked={selection.selectedRowIds.has(row.id)}
|
||||
disabled={selection.isRowSelectable ? !selection.isRowSelectable(row.original) : false}
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
onChange={() => selection.onToggleRow(row.id, row.original)}
|
||||
/>
|
||||
</td>
|
||||
) : null}
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<td key={cell.id} data-sticky-end={cell.column.id === 'actions' || cell.column.id === 'action' || undefined}>
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
))
|
||||
) : (
|
||||
<tr>
|
||||
<td colSpan={columnCount}>
|
||||
<div className="an-data-table__state">{emptyText}</div>
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</TableScrollRegion>
|
||||
{footer ? <div className="an-data-table__footer">{footer}</div> : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function DataTablePager({
|
||||
page,
|
||||
pageSize,
|
||||
total,
|
||||
onPageChange,
|
||||
}: {
|
||||
page: number
|
||||
pageSize: number
|
||||
total: number
|
||||
onPageChange: (page: number) => void
|
||||
}) {
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize))
|
||||
return (
|
||||
<div className="an-data-table__pager">
|
||||
<span>
|
||||
第 {page} / {totalPages} 页,共 {total.toLocaleString()} 条
|
||||
</span>
|
||||
<div className="an-data-table__pager-actions">
|
||||
<Button size="sm" variant="subtle" disabled={page <= 1} onClick={() => onPageChange(page - 1)}>
|
||||
上一页
|
||||
</Button>
|
||||
<Button size="sm" variant="subtle" disabled={page >= totalPages} onClick={() => onPageChange(page + 1)}>
|
||||
下一页
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
import {
|
||||
ChevronDown,
|
||||
LogOut,
|
||||
Menu,
|
||||
Moon,
|
||||
Monitor,
|
||||
Search,
|
||||
Sun,
|
||||
X,
|
||||
} from 'lucide-react'
|
||||
import { type FocusEvent, type KeyboardEvent, type ReactNode, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
||||
import { Link, useLocation, useNavigate } from 'react-router-dom'
|
||||
import packageJson from '../../../../package.json'
|
||||
import Scrollbar from '../../../components/Scrollbar/Scrollbar'
|
||||
import SegmentedControl from '../../../components/SegmentedControl/SegmentedControl'
|
||||
import { useAuthStore } from '../../../stores/auth'
|
||||
import { useAdminTheme, type AdminThemeMode } from '../../design/theme'
|
||||
import { cn } from '../../lib/utils'
|
||||
import { adminRouteGroups, getVisibleAdminRoutes } from '../../routes/manifest'
|
||||
import { useAdminSearch } from '../../search/AdminSearchContext'
|
||||
import { Button } from '../ui/button'
|
||||
|
||||
const DEFAULT_OPEN_MENU_KEY = 'collection'
|
||||
let cachedOpenKeys: string[] = [DEFAULT_OPEN_MENU_KEY]
|
||||
let cachedMenuScrollTop = 0
|
||||
|
||||
export function AdminLayout({ children }: { children: ReactNode }) {
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const adminSearch = useAdminSearch()
|
||||
const { user, logout } = useAuthStore()
|
||||
const { mode, setMode } = useAdminTheme()
|
||||
const [collapsed, setCollapsed] = useState(false)
|
||||
const [mobileNavOpen, setMobileNavOpen] = useState(false)
|
||||
const [openKeys, setOpenKeys] = useState<string[]>(cachedOpenKeys)
|
||||
const [searchQuery, setSearchQuery] = useState('')
|
||||
const [searchOpen, setSearchOpen] = useState(false)
|
||||
const [highlightedSearchIndex, setHighlightedSearchIndex] = useState(0)
|
||||
const menuViewportRef = useRef<HTMLDivElement>(null)
|
||||
const searchInputRef = useRef<HTMLInputElement>(null)
|
||||
const isSuperAdmin = user?.role === 'super_admin'
|
||||
const visibleRoutes = useMemo(() => getVisibleAdminRoutes(isSuperAdmin), [isSuperAdmin])
|
||||
const navGroups = useMemo(() => {
|
||||
return adminRouteGroups.map((group) => ({
|
||||
...group,
|
||||
children: visibleRoutes.filter((route) => route.group === group.key),
|
||||
})).filter((group) => group.children.length > 0)
|
||||
}, [visibleRoutes])
|
||||
const selectedKey = location.pathname === '/admin/' ? '/admin' : location.pathname.replace(/\/$/, '')
|
||||
const activeRoute = visibleRoutes.find((route) => route.path === selectedKey)
|
||||
const searchResults = useMemo(() => adminSearch.search(searchQuery), [adminSearch, searchQuery])
|
||||
const themeOptions = useMemo(() => [
|
||||
{ value: 'light' as const, label: '浅色', title: '浅色', icon: <Sun /> },
|
||||
{ value: 'system' as const, label: '系统', title: '跟随系统', icon: <Monitor /> },
|
||||
{ value: 'dark' as const, label: '深色', title: '深色', icon: <Moon /> },
|
||||
], [])
|
||||
|
||||
const updateOpenKeys = (nextKeys: string[]) => {
|
||||
cachedOpenKeys = nextKeys
|
||||
setOpenKeys(nextKeys)
|
||||
}
|
||||
|
||||
const selectSearchResult = (index = highlightedSearchIndex) => {
|
||||
const target = searchResults[index] || searchResults[0]
|
||||
if (!target) return
|
||||
setSearchOpen(false)
|
||||
setSearchQuery('')
|
||||
setHighlightedSearchIndex(0)
|
||||
searchInputRef.current?.blur()
|
||||
adminSearch.openTarget(target)
|
||||
}
|
||||
|
||||
const handleSearchKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === 'Escape') {
|
||||
setSearchOpen(false)
|
||||
setHighlightedSearchIndex(0)
|
||||
searchInputRef.current?.blur()
|
||||
return
|
||||
}
|
||||
if (event.key === 'ArrowDown') {
|
||||
event.preventDefault()
|
||||
setSearchOpen(true)
|
||||
setHighlightedSearchIndex((index) => Math.min(index + 1, Math.max(searchResults.length - 1, 0)))
|
||||
return
|
||||
}
|
||||
if (event.key === 'ArrowUp') {
|
||||
event.preventDefault()
|
||||
setSearchOpen(true)
|
||||
setHighlightedSearchIndex((index) => Math.max(index - 1, 0))
|
||||
return
|
||||
}
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault()
|
||||
selectSearchResult()
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearchBlur = (event: FocusEvent<HTMLDivElement>) => {
|
||||
if (event.currentTarget.contains(event.relatedTarget as Node | null)) return
|
||||
setSearchOpen(false)
|
||||
setHighlightedSearchIndex(0)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
void adminSearch.ensureDynamicIndex(searchQuery)
|
||||
}, [adminSearch, searchQuery])
|
||||
|
||||
const nav = (
|
||||
<>
|
||||
<div className="admin__brand" onClick={collapsed ? () => setCollapsed(false) : undefined}>
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="admin__collapse"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation()
|
||||
setCollapsed((value) => !value)
|
||||
}}
|
||||
aria-label={collapsed ? '展开菜单' : '折叠菜单'}
|
||||
>
|
||||
{collapsed ? <Menu size={18} /> : <X size={18} />}
|
||||
</Button>
|
||||
{!collapsed ? (
|
||||
<div className="admin__brand-copy">
|
||||
<span className="admin__brand-text">Planet</span>
|
||||
<span className="admin__brand-subtitle">Admin</span>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<Scrollbar className="admin__nav-scroll" viewportRef={menuViewportRef}>
|
||||
<nav className="admin__nav">
|
||||
{navGroups.map((group) => {
|
||||
const open = !collapsed && openKeys.includes(group.key)
|
||||
const GroupIcon = group.icon
|
||||
return (
|
||||
<section key={group.key} className="admin__nav-group">
|
||||
<button
|
||||
type="button"
|
||||
className="admin__nav-group-button"
|
||||
onClick={() => {
|
||||
if (collapsed) {
|
||||
setCollapsed(false)
|
||||
updateOpenKeys([group.key])
|
||||
return
|
||||
}
|
||||
updateOpenKeys(open ? openKeys.filter((key) => key !== group.key) : [...openKeys, group.key])
|
||||
}}
|
||||
>
|
||||
<GroupIcon size={18} />
|
||||
{!collapsed ? (
|
||||
<>
|
||||
<span>{group.label}</span>
|
||||
<ChevronDown className={cn('admin__nav-chevron', open && 'is-open')} size={14} />
|
||||
</>
|
||||
) : null}
|
||||
</button>
|
||||
{open ? (
|
||||
<div className="admin__nav-children">
|
||||
{group.children.map((item) => {
|
||||
const ItemIcon = item.icon
|
||||
return (
|
||||
<Link
|
||||
key={item.path}
|
||||
to={item.path}
|
||||
className={cn('admin__nav-link', selectedKey === item.path && 'is-active')}
|
||||
onClick={(event) => {
|
||||
cachedMenuScrollTop = menuViewportRef.current?.scrollTop ?? cachedMenuScrollTop
|
||||
setMobileNavOpen(false)
|
||||
if (item.path === selectedKey) event.preventDefault()
|
||||
}}
|
||||
>
|
||||
<ItemIcon size={16} />
|
||||
<span>{item.label}</span>
|
||||
</Link>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
) : null}
|
||||
</section>
|
||||
)
|
||||
})}
|
||||
</nav>
|
||||
</Scrollbar>
|
||||
|
||||
{!collapsed ? (
|
||||
<div className="admin__account">
|
||||
<div className="admin__account-row">
|
||||
<div>
|
||||
<strong>Hi, {user?.username || '-'}</strong>
|
||||
</div>
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="admin__account-logout"
|
||||
onClick={() => {
|
||||
logout()
|
||||
navigate('/login')
|
||||
}}
|
||||
aria-label="退出登录"
|
||||
title="退出登录"
|
||||
>
|
||||
<LogOut size={15} />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="admin__account-row">
|
||||
<span>版本号</span>
|
||||
<strong>v{packageJson.version}</strong>
|
||||
</div>
|
||||
<SegmentedControl<AdminThemeMode>
|
||||
ariaLabel="Admin 主题"
|
||||
className="admin__theme-control admin__theme-control--sider"
|
||||
options={themeOptions}
|
||||
scale={0.72}
|
||||
value={mode}
|
||||
onChange={setMode}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const viewport = menuViewportRef.current
|
||||
if (!viewport) return
|
||||
const restoreScroll = () => {
|
||||
viewport.scrollTop = cachedMenuScrollTop
|
||||
}
|
||||
restoreScroll()
|
||||
const frameId = window.requestAnimationFrame(restoreScroll)
|
||||
return () => window.cancelAnimationFrame(frameId)
|
||||
}, [collapsed, location.pathname, openKeys])
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const viewport = menuViewportRef.current
|
||||
if (!viewport) return
|
||||
const cacheScroll = () => {
|
||||
cachedMenuScrollTop = viewport.scrollTop
|
||||
}
|
||||
viewport.addEventListener('scroll', cacheScroll, { passive: true })
|
||||
return () => viewport.removeEventListener('scroll', cacheScroll)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="admin">
|
||||
<aside className={cn('admin__sider', collapsed && 'admin__sider--collapsed')}>
|
||||
{nav}
|
||||
</aside>
|
||||
{mobileNavOpen ? (
|
||||
<div className="admin__mobile-nav">
|
||||
<div className="admin__mobile-nav-panel">{nav}</div>
|
||||
<button className="admin__mobile-nav-backdrop" type="button" aria-label="关闭导航" onClick={() => setMobileNavOpen(false)} />
|
||||
</div>
|
||||
) : null}
|
||||
<main className="admin__content">
|
||||
<header className="admin__topbar">
|
||||
<Button size="icon" variant="ghost" className="admin__mobile-menu" onClick={() => setMobileNavOpen(true)} aria-label="打开导航">
|
||||
<Menu size={18} />
|
||||
</Button>
|
||||
<div className="admin__search" onBlur={handleSearchBlur}>
|
||||
<Search className="admin__search-icon" size={16} />
|
||||
<input
|
||||
ref={searchInputRef}
|
||||
aria-label="搜索功能、配置和文字"
|
||||
autoComplete="off"
|
||||
value={searchQuery}
|
||||
placeholder={activeRoute ? `搜索功能、配置和文字,当前:${activeRoute.label}` : '搜索功能、配置和文字'}
|
||||
onChange={(event) => {
|
||||
setSearchQuery(event.target.value)
|
||||
setSearchOpen(true)
|
||||
setHighlightedSearchIndex(0)
|
||||
}}
|
||||
onFocus={() => setSearchOpen(true)}
|
||||
onKeyDown={handleSearchKeyDown}
|
||||
/>
|
||||
{searchOpen ? (
|
||||
<div className="admin__search-results" role="listbox" aria-label="Admin 搜索结果">
|
||||
{searchResults.length > 0 ? searchResults.map((target, index) => {
|
||||
const ResultIcon = target.icon || Search
|
||||
return (
|
||||
<button
|
||||
key={target.id}
|
||||
type="button"
|
||||
className={cn('admin__search-result', index === highlightedSearchIndex && 'is-active')}
|
||||
role="option"
|
||||
aria-selected={index === highlightedSearchIndex}
|
||||
onMouseEnter={() => setHighlightedSearchIndex(index)}
|
||||
onMouseDown={(event) => event.preventDefault()}
|
||||
onClick={() => selectSearchResult(index)}
|
||||
>
|
||||
<ResultIcon size={16} />
|
||||
<span>
|
||||
<strong>{target.label}</strong>
|
||||
<small>{target.contextLabel || [target.routeLabel, target.sectionLabel].filter(Boolean).join(' / ')}</small>
|
||||
</span>
|
||||
</button>
|
||||
)
|
||||
}) : (
|
||||
<div className="admin__search-empty">{adminSearch.loading ? '正在加载搜索索引…' : '没有找到匹配内容'}</div>
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</header>
|
||||
<div className="admin__content-inner">{children}</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { type HTMLAttributes } from 'react'
|
||||
import { cn } from '../../lib/utils'
|
||||
|
||||
type BadgeTone = 'default' | 'blue' | 'green' | 'amber' | 'red' | 'purple' | 'cyan' | 'slate'
|
||||
|
||||
interface BadgeProps extends HTMLAttributes<HTMLSpanElement> {
|
||||
tone?: BadgeTone
|
||||
}
|
||||
|
||||
export function Badge({ className, tone = 'default', ...props }: BadgeProps) {
|
||||
return <span className={cn('an-badge', `an-badge--${tone}`, className)} {...props} />
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { forwardRef, type ButtonHTMLAttributes } from 'react'
|
||||
|
||||
import {
|
||||
TactileButton,
|
||||
type TactileButtonProps,
|
||||
type TactileButtonSize,
|
||||
type TactileButtonVariant,
|
||||
type TactileControlOptions,
|
||||
type TactileIconValue,
|
||||
} from '../../../components/tactile-ui'
|
||||
|
||||
type AdminButtonVariant = 'default' | 'primary' | 'ghost' | 'subtle' | 'danger' | 'link'
|
||||
type AdminButtonSize = 'sm' | 'md' | 'icon'
|
||||
|
||||
function mapVariant(variant?: AdminButtonVariant): TactileButtonVariant {
|
||||
if (variant === 'primary') return 'primary'
|
||||
if (variant === 'danger') return 'danger'
|
||||
if (variant === 'ghost' || variant === 'link') return 'ghost'
|
||||
if (variant === 'subtle') return 'subtle'
|
||||
return 'neutral'
|
||||
}
|
||||
|
||||
function mapSize(size?: AdminButtonSize): TactileButtonSize {
|
||||
if (size === 'sm') return 'sm'
|
||||
if (size === 'icon') return 'icon'
|
||||
return 'md'
|
||||
}
|
||||
|
||||
export interface ButtonProps
|
||||
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'size'> {
|
||||
asChild?: boolean
|
||||
loading?: boolean
|
||||
icon?: TactileIconValue
|
||||
iconSize?: number
|
||||
size?: AdminButtonSize
|
||||
tactile?: boolean | TactileControlOptions
|
||||
variant?: AdminButtonVariant
|
||||
}
|
||||
|
||||
export const Button = forwardRef<HTMLElement, ButtonProps>(function Button(
|
||||
{ asChild = false, loading = false, size, title, variant, ...props },
|
||||
ref,
|
||||
) {
|
||||
const tooltip = typeof title === 'string' ? title : undefined
|
||||
const searchText = [tooltip, typeof props['aria-label'] === 'string' ? props['aria-label'] : undefined]
|
||||
.filter(Boolean)
|
||||
.join(' ')
|
||||
const buttonProps = {
|
||||
...props,
|
||||
asChild,
|
||||
disabled: props.disabled || loading,
|
||||
loading,
|
||||
size: mapSize(size),
|
||||
title: tooltip,
|
||||
tooltip,
|
||||
variant: mapVariant(variant),
|
||||
'data-admin-search-text': searchText || undefined,
|
||||
} satisfies TactileButtonProps & { 'data-admin-search-text'?: string }
|
||||
if (variant === 'link') buttonProps.tactile = false
|
||||
return <TactileButton ref={ref} {...buttonProps} />
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
import { type HTMLAttributes } from 'react'
|
||||
import { cn } from '../../lib/utils'
|
||||
|
||||
export function Card({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
||||
return <div className={cn('an-card', className)} {...props} />
|
||||
}
|
||||
|
||||
export function CardHeader({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
||||
return <div className={cn('an-card__header', className)} {...props} />
|
||||
}
|
||||
|
||||
export function CardTitle({ className, ...props }: HTMLAttributes<HTMLHeadingElement>) {
|
||||
return <h3 className={cn('an-card__title', className)} {...props} />
|
||||
}
|
||||
|
||||
export function CardDescription({ className, ...props }: HTMLAttributes<HTMLParagraphElement>) {
|
||||
return <p className={cn('an-card__description', className)} {...props} />
|
||||
}
|
||||
|
||||
export function CardContent({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
||||
return <div className={cn('an-card__content', className)} {...props} />
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import * as DialogPrimitive from '@radix-ui/react-dialog'
|
||||
import { X } from 'lucide-react'
|
||||
import { type ReactNode } from 'react'
|
||||
import Scrollbar from '../../../components/Scrollbar/Scrollbar'
|
||||
import { Button } from './button'
|
||||
|
||||
interface DialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
title: string
|
||||
description?: string
|
||||
children: ReactNode
|
||||
footer?: ReactNode
|
||||
width?: number
|
||||
}
|
||||
|
||||
export function Dialog({ open, onOpenChange, title, description, children, footer, width }: DialogProps) {
|
||||
return (
|
||||
<DialogPrimitive.Root open={open} onOpenChange={onOpenChange}>
|
||||
<DialogPrimitive.Portal>
|
||||
<DialogPrimitive.Overlay className="an-dialog__overlay" />
|
||||
<DialogPrimitive.Content className="an-dialog" style={width ? { width } : undefined}>
|
||||
<div className="an-dialog__header">
|
||||
<div>
|
||||
<DialogPrimitive.Title className="an-dialog__title">{title}</DialogPrimitive.Title>
|
||||
{description ? (
|
||||
<DialogPrimitive.Description className="an-dialog__description">
|
||||
{description}
|
||||
</DialogPrimitive.Description>
|
||||
) : null}
|
||||
</div>
|
||||
<DialogPrimitive.Close asChild>
|
||||
<Button size="icon" variant="ghost" aria-label="关闭">
|
||||
<X size={16} />
|
||||
</Button>
|
||||
</DialogPrimitive.Close>
|
||||
</div>
|
||||
<Scrollbar className="an-dialog__body">{children}</Scrollbar>
|
||||
{footer ? <div className="an-dialog__footer">{footer}</div> : null}
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPrimitive.Portal>
|
||||
</DialogPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
interface ConfirmDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
title: string
|
||||
description?: string
|
||||
confirmLabel?: string
|
||||
cancelLabel?: string
|
||||
danger?: boolean
|
||||
loading?: boolean
|
||||
onConfirm: () => void
|
||||
}
|
||||
|
||||
export function ConfirmDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
title,
|
||||
description,
|
||||
confirmLabel = '确认',
|
||||
cancelLabel = '取消',
|
||||
danger = false,
|
||||
loading = false,
|
||||
onConfirm,
|
||||
}: ConfirmDialogProps) {
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title={title}
|
||||
description={description}
|
||||
width={420}
|
||||
footer={(
|
||||
<>
|
||||
<Button variant="subtle" onClick={() => onOpenChange(false)} disabled={loading}>
|
||||
{cancelLabel}
|
||||
</Button>
|
||||
<Button variant={danger ? 'danger' : 'primary'} onClick={onConfirm} loading={loading}>
|
||||
{confirmLabel}
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
>
|
||||
<span className="sr-only">{description || '请确认本次操作。'}</span>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { forwardRef, type InputHTMLAttributes, type TextareaHTMLAttributes } from 'react'
|
||||
import { cn } from '../../lib/utils'
|
||||
|
||||
export const Input = forwardRef<HTMLInputElement, InputHTMLAttributes<HTMLInputElement>>(
|
||||
({ className, ...props }, ref) => <input ref={ref} className={cn('an-input', className)} {...props} />,
|
||||
)
|
||||
|
||||
Input.displayName = 'Input'
|
||||
|
||||
export const Textarea = forwardRef<HTMLTextAreaElement, TextareaHTMLAttributes<HTMLTextAreaElement>>(
|
||||
({ className, ...props }, ref) => <textarea ref={ref} className={cn('an-input an-textarea', className)} {...props} />,
|
||||
)
|
||||
|
||||
Textarea.displayName = 'Textarea'
|
||||
@@ -0,0 +1,46 @@
|
||||
import * as SelectPrimitive from '@radix-ui/react-select'
|
||||
import { Check, ChevronDown } from 'lucide-react'
|
||||
import { cn } from '../../lib/utils'
|
||||
|
||||
export interface SelectOption {
|
||||
value: string
|
||||
label: string
|
||||
}
|
||||
|
||||
interface SelectProps {
|
||||
value: string
|
||||
onValueChange: (value: string) => void
|
||||
options: SelectOption[]
|
||||
placeholder?: string
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function Select({ value, onValueChange, options, placeholder, disabled, className }: SelectProps) {
|
||||
return (
|
||||
<SelectPrimitive.Root value={value} onValueChange={onValueChange} disabled={disabled}>
|
||||
<SelectPrimitive.Trigger className={cn('an-select', className)}>
|
||||
<span className="an-select__value">
|
||||
<SelectPrimitive.Value placeholder={placeholder} />
|
||||
</span>
|
||||
<SelectPrimitive.Icon>
|
||||
<ChevronDown size={16} />
|
||||
</SelectPrimitive.Icon>
|
||||
</SelectPrimitive.Trigger>
|
||||
<SelectPrimitive.Portal>
|
||||
<SelectPrimitive.Content className="an-select__content" position="popper">
|
||||
<SelectPrimitive.Viewport>
|
||||
{options.map((option) => (
|
||||
<SelectPrimitive.Item key={option.value} value={option.value} className="an-select__item">
|
||||
<SelectPrimitive.ItemText>{option.label}</SelectPrimitive.ItemText>
|
||||
<SelectPrimitive.ItemIndicator>
|
||||
<Check size={14} />
|
||||
</SelectPrimitive.ItemIndicator>
|
||||
</SelectPrimitive.Item>
|
||||
))}
|
||||
</SelectPrimitive.Viewport>
|
||||
</SelectPrimitive.Content>
|
||||
</SelectPrimitive.Portal>
|
||||
</SelectPrimitive.Root>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { TactileSwitch, type TactileControlOptions } from '../../../components/tactile-ui'
|
||||
|
||||
export function AdminSwitch({
|
||||
checked,
|
||||
onCheckedChange,
|
||||
label,
|
||||
className,
|
||||
disabled,
|
||||
tactile,
|
||||
}: {
|
||||
checked: boolean
|
||||
onCheckedChange: (checked: boolean) => void
|
||||
label: string
|
||||
className?: string
|
||||
disabled?: boolean
|
||||
tactile?: boolean | TactileControlOptions
|
||||
}) {
|
||||
return (
|
||||
<TactileSwitch
|
||||
checked={checked}
|
||||
onCheckedChange={onCheckedChange}
|
||||
label={label}
|
||||
tooltip={label}
|
||||
className={className}
|
||||
disabled={disabled}
|
||||
tactile={tactile}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export {
|
||||
ControlGroup as TactileControlGroup,
|
||||
mergeTactileOptions,
|
||||
tactileStyle,
|
||||
type TactileControlOptions,
|
||||
type TactileShadowDirection,
|
||||
} from '../../../components/tactile-ui'
|
||||
@@ -0,0 +1,64 @@
|
||||
import * as ToastPrimitive from '@radix-ui/react-toast'
|
||||
import { X } from 'lucide-react'
|
||||
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from 'react'
|
||||
|
||||
type ToastTone = 'default' | 'success' | 'error'
|
||||
|
||||
interface ToastItem {
|
||||
id: number
|
||||
title: string
|
||||
description?: string
|
||||
tone: ToastTone
|
||||
}
|
||||
|
||||
interface ToastContextValue {
|
||||
toast: (item: Omit<ToastItem, 'id' | 'tone'> & { tone?: ToastTone }) => void
|
||||
}
|
||||
|
||||
const ToastContext = createContext<ToastContextValue | null>(null)
|
||||
|
||||
export function ToastProvider({ children }: { children: ReactNode }) {
|
||||
const [items, setItems] = useState<ToastItem[]>([])
|
||||
|
||||
const toast = useCallback((item: Omit<ToastItem, 'id' | 'tone'> & { tone?: ToastTone }) => {
|
||||
const id = Date.now() + Math.random()
|
||||
setItems((current) => [...current, { id, tone: item.tone || 'default', title: item.title, description: item.description }])
|
||||
}, [])
|
||||
|
||||
const value = useMemo(() => ({ toast }), [toast])
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={value}>
|
||||
<ToastPrimitive.Provider swipeDirection="right">
|
||||
{children}
|
||||
{items.map((item) => (
|
||||
<ToastPrimitive.Root
|
||||
key={item.id}
|
||||
className={`an-toast an-toast--${item.tone}`}
|
||||
duration={3000}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) setItems((current) => current.filter((candidate) => candidate.id !== item.id))
|
||||
}}
|
||||
>
|
||||
<ToastPrimitive.Title className="an-toast__title">{item.title}</ToastPrimitive.Title>
|
||||
{item.description ? (
|
||||
<ToastPrimitive.Description className="an-toast__description">
|
||||
{item.description}
|
||||
</ToastPrimitive.Description>
|
||||
) : null}
|
||||
<ToastPrimitive.Close className="an-toast__close" aria-label="关闭通知">
|
||||
<X size={14} />
|
||||
</ToastPrimitive.Close>
|
||||
</ToastPrimitive.Root>
|
||||
))}
|
||||
<ToastPrimitive.Viewport className="an-toast__viewport" />
|
||||
</ToastPrimitive.Provider>
|
||||
</ToastContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useToast() {
|
||||
const context = useContext(ToastContext)
|
||||
if (!context) throw new Error('useToast must be used within ToastProvider')
|
||||
return context
|
||||
}
|
||||
Reference in new issue
Block a user