import { ActionIcon, Center, Container, Divider, Flex, Group, LoadingOverlay, Table as MantineTable, Pagination, Select, Text, Tooltip, TransitionProps, } from '@mantine/core' import { IconEdit, IconEye, IconTrashX } from '@tabler/icons-react' import { ColumnDef, ColumnOrderState, flexRender, getCoreRowModel, useReactTable, } from '@tanstack/react-table' import Link from 'next/link' import { useMemo, useState } from 'react' import EmptyRecord from '../../Empty/EmptyRecord' import { openSelectModal } from '../../MyModal' import { IReactTable } from './IReactTable' import MiscReactTableMenu from './partials/MiscReactTableMenu' export const optPageSize = [ { label: '5', value: '5' }, { label: '10', value: '10' }, { label: '20', value: '20' }, { label: '30', value: '30' }, { label: '50', value: '50' }, // { label: '100', value: '100' }, // { label: '200', value: '200' }, // { label: '500', value: '500' }, // { label: '1000', value: '1000' }, ] export default function MyReactTable(props: IReactTable) { const { query, columns, baseURL, selectedMutation, multiSelectedMutation, showModalDetail, isEdit = true, isDeleted = true, isShowDetail = true, isMiscAction = true, ...otherProps } = props const [page, setPage] = useState(1) const [pageSize, setPageSize] = useState(10) const { data, total, isFetching } = query const transition: Partial = { transition: 'slide-up', duration: 500, } const defaultColumns: ColumnDef[] = [ ...columns, { accessorKey: 'actions', id: 'actions', header: 'Actions', enablePinning: true, cell: ({ row }) => ( {isMiscAction ? ( ) : ( <> {isShowDetail && ( showModalDetail(row.original)} > )} {isEdit && ( )} {isDeleted && ( openSelectModal({ // @ts-ignore id: row?.original?.id as string, mutation: selectedMutation, query, }) } > )} )} ), }, ] const newColumns = useMemo(() => { if (!isEdit && !isDeleted && !isShowDetail) { return [...columns] } return defaultColumns }, [isEdit, isDeleted, isShowDetail]) const columnOrder = useMemo( () => newColumns.map((column) => column.id as string), [newColumns] ) const table = useReactTable({ data: data ?? [], columns: [ { accessorKey: 'index', id: 'index', enablePinning: true, header: '#', cell: ({ row }) => (
{row.index + 1 + ((page ?? 1) - 1) * (pageSize ?? 10)}
), }, ...newColumns, ], state: { columnOrder: ['select', 'index', ...columnOrder], }, // onColumnOrderChange: setColumnOrder, getCoreRowModel: getCoreRowModel(), debugTable: true, debugHeaders: true, debugColumns: true, }) return ( {/* Header */} {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { let widthStyle: any = 'fit-content' if (header.id.match(/actions/i)) { widthStyle = 90 } else if (['index', 'select'].includes(header.id)) { widthStyle = 100 } return ( {header.isPlaceholder ? 'null' : flexRender( header.column.columnDef.header, header.getContext() )} ) })} ))} {/* Body */} {data?.length === 0 && ( )} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} ))}