172 lines
4.7 KiB
TypeScript
172 lines
4.7 KiB
TypeScript
'use client'
|
||
|
||
import { useMemo } from 'react'
|
||
import {
|
||
useReactTable,
|
||
getCoreRowModel,
|
||
getPaginationRowModel,
|
||
flexRender,
|
||
} from '@tanstack/react-table'
|
||
import { Level } from '@/types'
|
||
import { createColumns } from './level-columns'
|
||
import {
|
||
Table,
|
||
TableBody,
|
||
TableCell,
|
||
TableHead,
|
||
TableHeader,
|
||
TableRow,
|
||
} from '@/components/ui/table'
|
||
import { Button } from '@/components/ui/button'
|
||
import {
|
||
ChevronLeft,
|
||
ChevronRight,
|
||
ChevronsLeft,
|
||
ChevronsRight,
|
||
} from 'lucide-react'
|
||
|
||
interface LevelTableProps {
|
||
levels: Level[]
|
||
onEdit: (level: Level) => void
|
||
onDelete: (id: string) => void
|
||
deleteConfirmId: string | null
|
||
}
|
||
|
||
export function LevelTable({
|
||
levels,
|
||
onEdit,
|
||
onDelete,
|
||
deleteConfirmId,
|
||
}: LevelTableProps) {
|
||
const columns = useMemo(
|
||
() => createColumns({ onEdit, onDelete, deleteConfirmId }),
|
||
[onEdit, onDelete, deleteConfirmId]
|
||
)
|
||
|
||
const table = useReactTable({
|
||
data: levels,
|
||
columns,
|
||
getCoreRowModel: getCoreRowModel(),
|
||
getPaginationRowModel: getPaginationRowModel(),
|
||
initialState: {
|
||
pagination: {
|
||
pageSize: 10,
|
||
},
|
||
},
|
||
})
|
||
|
||
if (levels.length === 0) {
|
||
return (
|
||
<div className="text-center py-12 text-gray-500">
|
||
<p>暂无关卡数据</p>
|
||
<p className="text-sm mt-2">
|
||
点击上方“添加关卡”按钮创建第一个关卡
|
||
</p>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
return (
|
||
<div className="space-y-4">
|
||
<div className="rounded-md border">
|
||
<Table>
|
||
<TableHeader>
|
||
{table.getHeaderGroups().map((headerGroup) => (
|
||
<TableRow key={headerGroup.id}>
|
||
{headerGroup.headers.map((header) => (
|
||
<TableHead key={header.id}>
|
||
{header.isPlaceholder
|
||
? null
|
||
: flexRender(
|
||
header.column.columnDef.header,
|
||
header.getContext()
|
||
)}
|
||
</TableHead>
|
||
))}
|
||
</TableRow>
|
||
))}
|
||
</TableHeader>
|
||
<TableBody>
|
||
{table.getRowModel().rows.map((row) => (
|
||
<TableRow key={row.id}>
|
||
{row.getVisibleCells().map((cell) => (
|
||
<TableCell key={cell.id}>
|
||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||
</TableCell>
|
||
))}
|
||
</TableRow>
|
||
))}
|
||
</TableBody>
|
||
</Table>
|
||
</div>
|
||
|
||
{/* 分页控制栏 */}
|
||
<div className="flex items-center justify-between px-2">
|
||
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
||
<span>每页显示</span>
|
||
<select
|
||
className="h-8 rounded-md border border-input bg-background px-2 text-sm"
|
||
value={table.getState().pagination.pageSize}
|
||
onChange={(e) => {
|
||
table.setPageSize(Number(e.target.value))
|
||
}}
|
||
>
|
||
{[10, 20, 50].map((pageSize) => (
|
||
<option key={pageSize} value={pageSize}>
|
||
{pageSize}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<span>条</span>
|
||
</div>
|
||
|
||
<div className="flex items-center gap-2">
|
||
<span className="text-sm text-muted-foreground">
|
||
第 {table.getState().pagination.pageIndex + 1} /{' '}
|
||
{table.getPageCount()} 页,共 {levels.length} 条
|
||
</span>
|
||
|
||
<div className="flex items-center gap-1">
|
||
<Button
|
||
variant="outline"
|
||
size="icon"
|
||
className="h-8 w-8"
|
||
onClick={() => table.setPageIndex(0)}
|
||
disabled={!table.getCanPreviousPage()}
|
||
>
|
||
<ChevronsLeft className="h-4 w-4" />
|
||
</Button>
|
||
<Button
|
||
variant="outline"
|
||
size="icon"
|
||
className="h-8 w-8"
|
||
onClick={() => table.previousPage()}
|
||
disabled={!table.getCanPreviousPage()}
|
||
>
|
||
<ChevronLeft className="h-4 w-4" />
|
||
</Button>
|
||
<Button
|
||
variant="outline"
|
||
size="icon"
|
||
className="h-8 w-8"
|
||
onClick={() => table.nextPage()}
|
||
disabled={!table.getCanNextPage()}
|
||
>
|
||
<ChevronRight className="h-4 w-4" />
|
||
</Button>
|
||
<Button
|
||
variant="outline"
|
||
size="icon"
|
||
className="h-8 w-8"
|
||
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
|
||
disabled={!table.getCanNextPage()}
|
||
>
|
||
<ChevronsRight className="h-4 w-4" />
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)
|
||
}
|