import React, { HTMLAttributes, ReactNode, useEffect, useState } from 'react'; import styled from 'styled-components'; import './index.scss'; import Pagination from '../Pagination/Pagination '; export type TableData = { header: React.ReactNode[]; body: { [key: string]: React.ReactNode }[]; footer?: { title: string; result: string } | any; }; export interface TableProps extends HTMLAttributes { children?: React.ReactNode; data: TableData; pagination?: boolean; perPage?: number; moreTool?: boolean; goTool?: boolean; statistic?: boolean; } const TableWrap = styled.div` width: 100%; display: flex; flex-direction: column; gap: 40px; align-items: center; `; const TableStyled = styled.div` width: 100%; `; const Table: React.FC = (props) => { const { children, data, perPage, pagination, ...rest } = props; const [showData, setShowData] = useState<{ [key: string]: React.ReactNode }[]>([]); useEffect(() => { if (pagination) { setShowData(data.body.slice(0, perPage)); } else { setShowData(data.body); } }, [data.body]); const [page, setPage] = useState(1); const fn = (currentPage: number, start: number, end: number) => { setShowData(data.body.slice(start, end)); setPage(currentPage); }; const bodyRender = (arr: { [key: string]: React.ReactNode }) => { const vNode: ReactNode[] = []; // eslint-disable-next-line no-restricted-syntax for (const key in arr) { // eslint-disable-next-line no-prototype-builtins if (arr.hasOwnProperty(key)) vNode.push({arr[key]}); } return vNode; }; const render = () => { return ( {data.header.map((item) => { return ( ); })} {showData.map((item: { [key: string]: React.ReactNode }) => { return {bodyRender(item)}; })} {data.footer ? ( ) : ( '' )}
{item}
{data.footer.title} {data.footer.result}
); }; return ( {render()} {pagination ? ( ) : ( '' )} ); }; Table.defaultProps = { children: '', pagination: false, perPage: 10, moreTool: false, statistic: false, goTool: false }; export default Table;