import React, { useState } from 'react'; import { createColumnHelper, flexRender, getCoreRowModel, useReactTable, getPaginationRowModel, getSortedRowModel, getFilteredRowModel, FilterFn, } from '@tanstack/react-table'; interface CodeInfo { email: string; name: string; passport_id: string } const globalFilterFn: FilterFn = (row, columnId, filterValue: string) => { const value = row.getValue(columnId); return String(value).toLowerCase().includes(filterValue.toLowerCase()); }; const columnHelper = createColumnHelper(); const columns = [ columnHelper.accessor('name', { cell: info => info.getValue(), header: () => 'Name', }), columnHelper.accessor('passport_id', { cell: info => info.getValue(), header: () => 'Passport/National ID', }), columnHelper.accessor('email', { cell: info => info.getValue(), header: () => 'E-mail', }), ]; const CodegenTable: React.FC<{ infos: CodeInfo[]; }> = ({ infos }) => { const [globalFilter, setGlobalFilter] = useState(''); const table = useReactTable({ data: infos, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), globalFilterFn: globalFilterFn, state: { globalFilter, }, onGlobalFilterChange: setGlobalFilter, initialState: { pagination: { pageSize: 5, }, } }); return (
setGlobalFilter(e.target.value)} placeholder="Search ..." className="p-2 border rounded flex-grow" />
{table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map(header => ( ))} ))} {table.getRowModel().rows.map((row, index, array) => { const isLastRow = index === array.length - 1; return ( {row.getVisibleCells().map((cell) => { return ( ); })} ); })}
{header.isPlaceholder ? null : (
{flexRender( header.column.columnDef.header, header.getContext() )} {{ asc: ' 🔼', desc: ' 🔽', }[header.column.getIsSorted() as string] ?? null}
)}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
Page{' '} {table.getState().pagination.pageIndex + 1} of{' '} {table.getPageCount()}
); }; export default CodegenTable;