import { Checkbox, LabelDisplayedRowsArgs, Table, TableBody, TableBodyProps, TablePagination, TablePaginationProps, TableProps } from '@mui/material' import * as locales from '@mui/material/locale' import { ThemeProvider, createTheme, useTheme } from '@mui/material/styles' import { CommonUtils, StrictOmit } from 'fwork-jsts-common' import React, { ReactNode, useEffect, useMemo, useState } from 'react' import { TablePaginationActions } from './actions' import { Checkable, StyledTableCell, StyledTableRow, StyledTableRowProps, SupportedLocales, TableComponentSetCurrPageProps } from './common' export interface TableComponentProps { downloadCsv?: boolean, bodyList: T[], header?: ReactNode, footer?: ReactNode, wrapperProps?: React.DetailedHTMLProps, HTMLDivElement> tableShadowContainerProps?: React.DetailedHTMLProps, HTMLDivElement>, tableScrollContainerProps?: React.DetailedHTMLProps, HTMLDivElement>, tableProps?: TableProps, bodyProps?: TableBodyProps, paginationProps?: StrictOmit, bodyRowBuilder: (item: T, idx: number) => React.ReactNode, onPageChange?: (page: number) => void, onRowsPerPageChange?: (rowsPerPage: number) => void, // EXPORTED METHOD... setCurrPageRef?: React.MutableRefObject // ...EXPORTED METHOD /** * If you set rowProps, you don't need to return a `TableRow` in the `bodyRowBuilder`; `rowProps` already wraps the elements in a `TableRow`. */ rowProps?: { onClick?: (event: React.MouseEvent, item: T) => void onChangeChecked?: (e: React.ChangeEvent, item: T, checked: boolean) => void } & StrictOmit } export const TableComponent: React.FC> = (props: TableComponentProps) => { const [page, setPage] = useState(0) const [rowsPerPage, setRowsPerPage] = useState(10) // EXPORTED METHOD... useEffect(() => { if (props.setCurrPageRef) props.setCurrPageRef.current = setCurrPage; }, []); const setCurrPage = (args: { page: number }) => { setPage(args.page) }; // ...EXPORTED METHOD React.useEffect(() => { if (props.onPageChange) props.onPageChange(page) }, [page]) React.useEffect(() => { if (props.onRowsPerPageChange) props.onRowsPerPageChange(rowsPerPage) }, [rowsPerPage]) const handleChangePage = (_: unknown, newPage: number) => setPage(newPage) const handleChangeRowsPerPage = (event: React.ChangeEvent) => { setRowsPerPage(+event.target.value) setPage(0) } //TablePagination const [locale] = useState('ptBR') const theme = useTheme() const themeWithLocale = useMemo( () => createTheme(theme, locales[locale]), [locale, theme], ) const handleLabelDisplayedRows = (paginationInfo: LabelDisplayedRowsArgs) => { // return defaultPageCount > 1 ? `Página ${page + 1} ${paginationInfo.from}-${paginationInfo.to} de ${paginationInfo.count}` : ''; return defaultPageCount > 1 ? `${page + 1} ${paginationInfo.from}-${paginationInfo.to} de ${paginationInfo.count}` : ''; }; const items = props.bodyList?.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) // let pageCount = props.bodyList.length / 10 // if (pageCount % 1 > 0) { // pageCount = pageCount | 0 // pageCount++ // } // const defaultPageCount = Math.ceil(props.bodyList.length / rowsPerPage) const defaultPageCount = Math.ceil(props.bodyList.length / 10) // wrapperProps -> tableShadowContainerProps -> tableScrollContainerProps -> tableProps return <>
{items.length ? props.header : <>} {items.map((item: T, idx: number) => { return props.rowProps ? { props.rowProps?.onClick?.(event, item) }} > {props.rowProps.onChangeChecked ? <> )._checked} onChange={(e, checked) => { props.rowProps?.onChangeChecked?.(e, item, checked) }} onClick={(e) => e.stopPropagation()} slotProps={{ input: { 'aria-label': 'controlled' }, }} /> {props.bodyRowBuilder(item, idx)} : props.bodyRowBuilder(item, idx)} : props.bodyRowBuilder(item, idx) })} {items.length ? props.footer : <>}
{defaultPageCount > 1 || props.downloadCsv ?
1 ? undefined : '')} rowsPerPageOptions={props.paginationProps?.rowsPerPageOptions ?? (defaultPageCount > 1 ? undefined : [])} labelDisplayedRows={(paginationInfo: LabelDisplayedRowsArgs) => { return props.paginationProps?.labelDisplayedRows ? props.paginationProps.labelDisplayedRows(paginationInfo) : handleLabelDisplayedRows(paginationInfo) }} ActionsComponent={(subprops) => { const CustomActions = props.paginationProps?.ActionsComponent; return ( <> {CustomActions && } CommonUtils.exportToCSV(props.bodyList)} /> ); }} onPageChange={(e, newPage) => { handleChangePage(e, newPage) if (props.paginationProps?.onPageChange) props.paginationProps.onPageChange(e, newPage) }} onRowsPerPageChange={(e) => { handleChangeRowsPerPage(e) if (props.paginationProps?.onRowsPerPageChange) props.paginationProps.onRowsPerPageChange(e) }} count={props.bodyList.length} page={page} rowsPerPage={rowsPerPage} />
: <>}
}