From a287df5e0cc590759cd49d184cf077c93114065d Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 1 Oct 2021 23:43:49 +0200 Subject: [PATCH] added table component --- src/components/Table.tsx | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/components/Table.tsx diff --git a/src/components/Table.tsx b/src/components/Table.tsx new file mode 100644 index 0000000..f954116 --- /dev/null +++ b/src/components/Table.tsx @@ -0,0 +1,59 @@ +import React from "react" + +export interface Header { + name: string +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export interface Row { + cells: Cell[] + data?: T, + // TODO: maybe there is some TS magic to apply here so that rowData has the same type as data without using generics + onClick?: (e: React.MouseEvent, rowData: T ) => void + onContextMenu?: (e: React.MouseEvent, data: T)=>void +} + +export interface Cell { + name: string + component?: React.ReactNode +} + +export interface Data { + headers: Header[] + body: Row[] +} + +interface Props { + data: Data +} + +const Table: React.FC = ({data:{headers,body}}) => { + const numCol = headers.length + return ( + + + {headers.map((e,i)=>)} + + + {body.map((row,rowIndex)=> + row.onClick?.(e,row.data)} + onContextMenu={e=>row.onContextMenu?.(e,row.data)} + > + {(row.cells.length >= numCol? + row.cells: + row.cells.concat(Array(numCol - row.cells.length).fill({name:""}))) // Pad array to fit numCol + .map((cell,cellIndex)=> + + )} + )} + +
{e.name}
+ {cell.component || cell.name} +
+ ) +} + +export default Table