import { ReactNode, useEffect, useRef } from 'react'; import dot from 'dot-object'; import { MdMoreHoriz } from 'react-icons/md'; import { Colors } from '../../hooks/theme'; import { Badge } from '../Badge'; import { Dropdown, DropdownContent, DropdownItem } from '../Dropdown'; import { Empty } from '../Empty'; import { Flex } from '../Flex'; import { IconButton } from '../IconButton'; import { LinkButton } from '../LinkButton'; import { Pagination } from '../Pagination'; import { Ellipse, Rectangle } from '../Shimmer'; import { HeaderCell } from './HeaderCell'; import { Container, BodyCell, ActionCell, FooterCell } from './styles'; type RenderType = { value: string | { [key: string]: string }; item: | { [key: string]: string } | { [key: string]: { [key: string]: string } }; }; type HeaderType = { label: string; dataIndex: string; sort?: string | null; visible?: boolean; render?: (data: RenderType) => ReactNode; align?: 'left' | 'right'; }; type ActionType = { label: string; type?: string; onClick: ( item: | { [key: string]: string } | { [key: string]: { [key: string]: string } }, ) => void; }; type SortType = { direction: string; dataIndex: string; }; interface TableProps { headers: HeaderType[]; data: { [key: string]: any }[] | { [key: string]: { [key: string]: any } }[]; footer?: { [key: string]: string }; actions?: ActionType[]; totalCount?: number; currentPage?: number; onPageChange?: (page: number) => void; perPage?: number; perPageChange?: (amount: number) => void; sort?: SortType; onSort?: (sort: SortType) => void; exports?: () => void; isLoading?: boolean; } export function Table({ headers = [], data = [], footer, actions = [], totalCount, currentPage, onPageChange, perPage = 20, perPageChange, sort, onSort, exports, isLoading, }: TableProps): JSX.Element { const containerRef = useRef(null); const hasPagination = !!data.length && currentPage !== undefined && totalCount !== undefined && onPageChange !== undefined; return ( item.visible !== false).length && actions.length ? headers.filter((item) => item.visible !== false).length + 1 : headers.filter((item) => item.visible !== false).length } haveActions={!!actions.length} >
{headers .filter((item) => item.visible !== false) .map((field) => ( {field.label} ))} {!!actions.length && Ações} {!isLoading && !!data.length && data.map((item) => ( <> {headers .filter((headerItem) => headerItem.visible !== false) .map((field) => { if (dot.pick(field.dataIndex, item) === null) { return ; } if (dot.pick(field.dataIndex, item) && field.render) { return ( {field.render({ value: dot.pick(field.dataIndex, item), item, })} ); } if ( typeof dot.pick(field.dataIndex, item) === 'number' && field.render ) { return ( {field.render({ value: dot.pick(field.dataIndex, item), item, })} ); } if ( typeof dot.pick(field.dataIndex, item) === 'boolean' && field.render ) { return ( {field.render({ value: dot.pick(field.dataIndex, item), item, })} ); } if (typeof dot.pick(field.dataIndex, item) === 'object') { const { type, value } = dot.pick( field.dataIndex, item, ) as { [key: string]: string; }; if (type === 'BADGE') { const { color } = dot.pick(field.dataIndex, item) as { [key: string]: string; }; return ( {value} ); } if (type === 'URL') { const { label } = dot.pick(field.dataIndex, item) as { [key: string]: string; }; return ( window.open(value)}> {label} ); } if (type === 'ICCID') { return (

{value.slice(0, 10)} {value.slice(10)}

); } if (type === 'IMEI') { return (

{value.slice(0, 9)} {value.slice(9)}

); } return ( {JSON.stringify(dot.pick(field.dataIndex, item))} ); } if (field.dataIndex) { return ( {dot.pick(field.dataIndex, item)} ); } return ; })} {!!actions.length && ( } /> {actions.map((action) => ( action.onClick(item)} type={action.type as any} > {action.label} ))} )} ))} {footer && headers.map( (header) => header.dataIndex && ( {footer[header.dataIndex]} ), )} {footer && actions.length && }
{isLoading && ( {new Array(5).fill('').map(() => ( ))} )} {!data.length && !isLoading && ( )} {(hasPagination || !!exports) && ( <> {!!exports && exports()} {hasPagination && ( { onPageChange(value); if (containerRef.current) { containerRef.current.scrollTo(0, 0); } }} perPage={perPage} perPageChange={(value) => { if (perPageChange) { perPageChange(value); } if (containerRef.current) { containerRef.current.scrollTo(0, 0); } }} /> )} )}
); }