import React, { useState } from "react"; import { useReactTable, getCoreRowModel, getSortedRowModel, getPaginationRowModel, getFilteredRowModel, flexRender, ColumnDef, SortingState } from "@tanstack/react-table"; import { ArrowUpDown, ChevronLeft, ChevronRight, Search } from "lucide-react"; interface WeatherTableProps { data: T[]; columns: ColumnDef[]; defaultSorting?: SortingState; onRowDoubleClick?: (row: T) => void; searchPlaceholder?: string; searchKey?: keyof T; mobileLayout?: "table" | "cards"; } export function WeatherTable({ data, columns, defaultSorting = [], onRowDoubleClick, searchPlaceholder = "Filter records...", searchKey, mobileLayout = "table" }: WeatherTableProps) { const [sorting, setSorting] = useState(() => defaultSorting); const [globalFilter, setGlobalFilter] = useState(""); // eslint-disable-next-line react-hooks/incompatible-library const table = useReactTable({ data, columns, state: { sorting, globalFilter }, onSortingChange: setSorting, onGlobalFilterChange: setGlobalFilter, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getPaginationRowModel: getPaginationRowModel(), getFilteredRowModel: getFilteredRowModel(), globalFilterFn: (row, columnId, filterValue) => { const val = row.getValue(columnId); if (val === undefined || val === null) return false; return String(val).toLowerCase().includes(String(filterValue).toLowerCase()); }, initialState: { pagination: { pageSize: 25 } } }); return (
{/* Search Input bar */} {searchKey && (
setGlobalFilter(e.target.value)} placeholder={searchPlaceholder} className="w-full bg-white/70 border border-slate-200 rounded-xl pl-9 pr-4 py-2.5 text-sm font-semibold text-slate-700 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all" />
)} {mobileLayout === "cards" && (
{table.getRowModel().rows.map((row) => ( ))} {table.getRowModel().rows.length === 0 && (

No records matching your filters.

)}
)} {/* Grid container with sticky headers */}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header, index) => { const metaClassName = (header.column.columnDef.meta as { className?: string })?.className || ""; return ( )})} ))} {table.getRowModel().rows.map((row) => ( { if (window.innerWidth < 640 && onRowDoubleClick) onRowDoubleClick(row.original); }} onDoubleClick={() => onRowDoubleClick && onRowDoubleClick(row.original)} className={`group hover:bg-slate-50/80 transition-colors cursor-pointer ${ onRowDoubleClick ? "select-none" : "" }`} > {row.getVisibleCells().map((cell, index) => { const metaClassName = (cell.column.columnDef.meta as { className?: string })?.className || ""; return ( )})} ))} {table.getRowModel().rows.length === 0 && ( )}
{header.isPlaceholder ? null : (
{flexRender(header.column.columnDef.header, header.getContext())} {header.column.getCanSort() && ( )}
)}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
No records matching your filters.
{/* Pagination Controls */}
Page {table.getState().pagination.pageIndex + 1} of{" "} {table.getPageCount()}
); }