"use client" /** * 表格组件模板 * * 包含项目中常用的各种表格组件 */ import type React from "react" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Checkbox } from "@/components/ui/checkbox" import { Badge } from "@/components/ui/badge" interface Column { header: string accessorKey: keyof T | ((row: T) => React.ReactNode) cell?: (row: T) => React.ReactNode } interface DataTableProps { data: T[] columns: Column[] keyField: keyof T selectable?: boolean selectedRows?: (string | number)[] onSelectRow?: (id: string | number, checked: boolean) => void onSelectAll?: (checked: boolean) => void onRowClick?: (row: T) => void emptyMessage?: string } /** * 通用数据表格 * 用于展示表格数据 */ export function DataTable>({ data, columns, keyField, selectable = false, selectedRows = [], onSelectRow, onSelectAll, onRowClick, emptyMessage = "没有数据", }: DataTableProps) { const handleRowClick = (row: T) => { if (onRowClick) { onRowClick(row) } } return (
{selectable && onSelectRow && onSelectAll && ( 0 && selectedRows.length === data.length} onCheckedChange={(checked) => onSelectAll(checked === true)} /> )} {columns.map((column, index) => ( {column.header} ))} {data.length === 0 ? ( {emptyMessage} ) : ( data.map((row) => ( handleRowClick(row)} > {selectable && onSelectRow && ( e.stopPropagation()}> onSelectRow(row[keyField], checked === true)} /> )} {columns.map((column, index) => ( {column.cell ? column.cell(row) : typeof column.accessorKey === "function" ? column.accessorKey(row) : row[column.accessorKey]} ))} )) )}
) } interface DeviceTableProps { devices: Array<{ id: string name: string imei: string status: "online" | "offline" battery?: number wechatId?: string lastActive?: string }> selectedDevices: string[] onSelectDevice: (deviceId: string, checked: boolean) => void onSelectAll: (checked: boolean) => void onDeviceClick: (deviceId: string) => void } /** * 设备表格 * 用于展示设备列表 */ export function DeviceTableTemplate({ devices, selectedDevices, onSelectDevice, onSelectAll, onDeviceClick, }: DeviceTableProps) { const columns: Column<(typeof devices)[0]>[] = [ { header: "设备名称", accessorKey: "name", cell: (row) => (
{row.name}
IMEI: {row.imei}
), }, { header: "状态", accessorKey: "status", cell: (row) => ( {row.status === "online" ? "在线" : "离线"} ), }, { header: "微信号", accessorKey: "wechatId", cell: (row) => row.wechatId || "-", }, { header: "电量", accessorKey: "battery", cell: (row) => (
20 ? "bg-green-500" : "bg-red-500"}`} style={{ width: `${row.battery || 0}%` }} >
{row.battery || 0}%
), }, { header: "最后活跃", accessorKey: "lastActive", cell: (row) => row.lastActive || "-", }, ] return ( onDeviceClick(row.id)} emptyMessage="没有找到设备" /> ) }