import React from 'react' import {Typography, Paper, TableContainer, IconButton, Grid} from "@mui/material" import {MdKeyboardArrowDown, MdKeyboardArrowUp} from "react-icons/md" import { StyledTable, StyledTableBody, StyledTableRow, StyledTableCell, StyledTableHead, StyledTableFooter } from './styles' import Progress from '../Progress'; interface Cols{ columnKey ?: string, columnLabel ?: string | React.ReactNode, minWidth?: number, style?: React.CSSProperties, render ?: (row : any,index:any) => React.ReactNode } interface TableProps{ cols ?: Array, data ?: Array, isExpandable ?: boolean, expandCols ?: Array, expandLabelKey?: string, preloading ?: boolean, className ?: string, onExpand ?: (row : object, id : string) => void, sx?: any, isPagination?: boolean, page?: number, rowsPerPage?: number, tableContainerStyles?:any, childTableStyles?:any, childLoading?:boolean } interface TableHeaders{ cols : Array, isExpandable : boolean } interface TableBodyRow{ row ?: {[key : string] : any}, cols ?: Array, isExpandable ?: boolean, expandCols ?: Array, expandLabelKey?:string, index?:number, onExpand ?: (row : object, id : string) => void, childTableStyles:any, childLoading?:boolean } const getTableHeaders = ({cols, isExpandable} : TableHeaders) => { return {isExpandable && } {cols?.map((header, index) => {header?.columnLabel} )} } const TableBodyRow = ({row = {}, cols = [], isExpandable, expandCols, childTableStyles,childLoading,expandLabelKey,index,onExpand = () => {/* */}} : TableBodyRow) => { const [isExpand, setIsExpand] = React.useState(false); const [loader, setLoader] = React.useState(false); const onExpandClick = async (id : string) => { let interval: NodeJS.Timer; let timer: NodeJS.Timeout; setIsExpand(!isExpand) if(!isExpand && expandLabelKey){ setLoader(true); await onExpand(row, id); timer = setTimeout(() => { interval = setInterval(() => { if(expandLabelKey && row?.[expandLabelKey]?.length > 0){ setLoader(false) clearInterval(interval); clearTimeout(timer); } }, 500) setLoader(false); clearTimeout(timer); }, 1500); } } return ( *': { borderBottom: 'unset' } }}> {isExpandable && onExpandClick(row?.definitionId)} > {isExpand ? : } } { cols.map((col,key) => { return( {col.render!(row,index)} ) }) } {(isExpandable && isExpand) && (loader ?
: )} ); } const Table = (tableProps : TableProps) => { const {cols = [], data = [], childTableStyles,isExpandable = false, tableContainerStyles,expandCols, expandLabelKey, onExpand, preloading, childLoading, sx, isPagination, page=0, rowsPerPage=0} = tableProps; return ( {getTableHeaders({cols, isExpandable})} {preloading ? : <> { data.length > 0 ? <> {data.map((row,index) => ( ))} : No Records Found } } ) } export default Table