import React, { useEffect, useState } from 'react'; import { TableProps } from '.'; import getModuleAnimation from '../Card/Animations/animations'; import Loading from '../Loading/Loading'; import { Typography } from '../Typography'; import { paginate } from './Helper'; import { Divider, DivSpinnerLoaderParent, DivTableCell, NoData, Pagination, PaginationTag, PaginationText, TableGrid, TableGridContainer, TableParent, } from './Table.styles'; const Table: React.FC = ({ columnsConfig, header, data, pageSize, maxPages, noPagination, customPageNumber, onPageNumberChanged, customNoDataComponent, customNoDataText = 'No Data', isLoading = false, customLoadingContent, alignCellItems = 'start', justifyCellItems = 'start', ...props }) => { const [pageNum, setPageNum] = useState( customPageNumber ? customPageNumber : 0, ); useEffect(() => { if (typeof onPageNumberChanged != 'undefined') { onPageNumberChanged(pageNum); } }, [pageNum]); useEffect(() => { handleSetPageNumber(customPageNumber ? customPageNumber : 0); }, [customPageNumber]); const handleSetPageNumber = (state: number): void => { if (typeof customPageNumber == 'number') { setPageNum(customPageNumber); } else { setPageNum(state); } }; const computeCurrentData = (): (string | React.ReactNode)[][] => { if (noPagination) { return data; } const from = pageNum * pageSize; const to = from + pageSize; return data?.slice(from, to); }; const handlePrev = (): void => { if (pageNum != 0) { handleSetPageNumber(pageNum - 1); } }; const handleNext = (): void => { if (pageNum + 1 < Math.ceil(data?.length / pageSize)) { handleSetPageNumber(pageNum + 1); } }; const RenderTableHeader = (): JSX.Element => { return ( <> {header.map((col, key) => ( {col} ))} ); }; const RenderNoData = (): JSX.Element => { if (customNoDataComponent) { return {customNoDataComponent}; } return (
{getModuleAnimation(undefined)}

{customNoDataText}

); }; const RenderTable = (): JSX.Element => { if (computeCurrentData().length == 0) { return ; } return ( <> {computeCurrentData().map( (row: (string | React.ReactNode)[], rowKey, arr) => ( {row.map( ( item: string | React.ReactNode, colKey: number, rowData, ) => ( {item} ), )} {rowKey != arr.length - 1 && ( )} ), )} ); }; const RenderPagination = (): JSX.Element => { if (noPagination || data?.length == 0) { return <>; } return (
Prev {paginate(data?.length, pageNum, pageSize, maxPages).map( (key) => ( handleSetPageNumber(key - 1)} role="pagination-item" data-testid={`pagination_${key - 1 == pageNum}`} > {key} ), )} Next
); }; const Loader = () => ( {customLoadingContent ? ( customLoadingContent ) : ( <> Loading Content )} ); return ( {isLoading ? : } ); }; export default Table;