"use client"; import { makeData } from "@/lib/utils/makeData"; import { ColumnDef, ColumnResizeDirection, ColumnResizeMode, flexRender, getCoreRowModel, getPaginationRowModel, getSortedRowModel, SortingState, useReactTable, } from "@tanstack/react-table"; import React, { FC, useEffect } from "react"; import { Breadcrumb, Button, Checkbox, Label, Modal, Table, TextInput, } from "flowbite-react"; import { useLocal } from "@/lib/utils/use-local"; import { FaArrowDownLong, FaArrowUp } from "react-icons/fa6"; import { init_column } from "../tablelist/lib/column"; export const List: React.FC = ({ name, column, onLoad, take = 20, header, }) => { const sideRight = typeof header?.sideRight === "function" ? header.sideRight : null; type Person = { firstName: string; lastName: string; age: number; visits: number; status: string; progress: number; }; const local = useLocal({ data: [] as any[], sort: {} as any, search: null as any, reload: async () => { const res: any = onLoad({ search: local.search, sort: local.sort, take, paging: 1, }); if (res instanceof Promise) { res.then((e) => { local.data = e; local.render(); }); } else { local.data = res; local.render(); } }, }); useEffect(() => { const res: any = onLoad({ search: local.search, sort: local.sort, take: 10, paging: 1, }); if (res instanceof Promise) { res.then((e) => { local.data = e; local.render(); }); } else { local.data = res; local.render(); } }, []); const defaultColumns: ColumnDef[] = init_column(column); const [sorting, setSorting] = React.useState([]); const [columns] = React.useState(() => [ ...defaultColumns, ]); const [columnResizeMode, setColumnResizeMode] = React.useState("onChange"); const [columnResizeDirection, setColumnResizeDirection] = React.useState("ltr"); // Create the table and pass your options const table = useReactTable({ data: local.data, columnResizeMode, columnResizeDirection, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), onSortingChange: setSorting, state: { pagination: { pageIndex: 0, pageSize: take, }, sorting, }, }); // Manage your own state const [state, setState] = React.useState(table.initialState); // Override the state managers for the table to your own table.setOptions((prev) => ({ ...prev, state, onStateChange: setState, debugTable: state.pagination.pageIndex > 2, })); return ( <>
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header, index) => { const name = header.column.id; const col = column.find((e: any) => e?.name === name); const isSort = typeof col?.sortable === "boolean" ? col.sortable : true; return ( ); })} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => { const ctx = cell.getContext(); const param = { row: row.original, name: ctx.column.id, cell, }; const head = column.find( (e: any) => e?.name === ctx.column.id ); const renderData = typeof head?.renderCell === "function" ? head.renderCell(param) : flexRender( cell.column.columnDef.cell, cell.getContext() ); return ( {renderData} ); })} ))}
{ if (isSort) { const sort = local?.sort?.[name]; const mode = sort === "desc" ? null : sort === "asc" ? "desc" : "asc"; local.sort = mode ? { [name]: mode, } : {}; local.render(); } }} className={cx( "flex flex-grow flex-row select-none items-center flex-row", isSort ? " cursor-pointer" : "" )} >
{header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )}
{local?.sort?.[name] === "asc" ? ( ) : local?.sort?.[name] === "desc" ? ( ) : ( <> )}
{headerGroup.headers.length !== index + 1 ? (
header.column.resetSize(), onMouseDown: header.getResizeHandler(), onTouchStart: header.getResizeHandler(), className: `resizer w-0.5 bg-gray-300 ${ table.options.columnResizeDirection } ${ header.column.getIsResizing() ? "isResizing" : "" }`, style: { transform: columnResizeMode === "onEnd" && header.column.getIsResizing() ? `translateX(${ (table.options .columnResizeDirection === "rtl" ? -1 : 1) * (table.getState().columnSizingInfo .deltaOffset ?? 0) }px)` : "", }, }} >
) : null}
); };