import React, { ReactNode } from "react"; import cn from "classnames"; import { Badge } from "components"; import { TableActionsMenu } from "components"; export interface TableColumn { /** * Column Name, should match the dataset keys */ name: string; /** * Column Title */ title: string; /** * Function to perform when rendering this field */ formatter?: any; /** * Additional classes */ className?: string; } export interface TablePagination { limit: number; offset: number; total: number; onSetOffset?: any; } export interface TableProps { /** * Columns */ columns: TableColumn[]; /** * data to render */ data: any; /** * Pagination */ pagination?: TablePagination; /** * Name of column to show sorted by */ sortBy?: string; /** * Should we render actions column at the end */ hasActions?: boolean; } export const Table = ({ columns, data, pagination, sortBy, hasActions, }: TableProps) => { const getFormatter = (given: any) => { if (typeof given === "function") { return given; } if (typeof given === "string") { switch (given) { // Simple ID column has text-muted case "id": return (val: number) => { return {val}; }; /* case "setup": return (val: boolean) => { return ( {val ? intl.formatMessage({ id: "ready", defaultMessage: "Ready", }) : intl.formatMessage({ id: "setup-required", defaultMessage: "Setup Required", })} ); }; */ case "bool": return (val: boolean) => { return ( {val ? "true" : "false"} ); }; default: return () => { return <>formatter-not-found: {given}; }; } } }; const getPagination = (p: TablePagination) => { const totalPages = Math.ceil(p.total / p.limit); const currentPage = Math.floor(p.offset / p.limit) + 1; const end = p.total < p.limit ? p.total : p.offset + p.limit; const getPageList = () => { const list = []; for (let x = 0; x < totalPages; x++) { list.push(
  • , ); } return list; }; return (

    Showing {p.offset + 1} to {end} of{" "} {p.total} item{p.total === 1 ? "" : "s"}

    {end >= p.total ? ( ) : null}
    ); }; return ( <>
    {columns.map((col, idx) => { return ( ); })} {hasActions && {data.map((row: any, idx: number) => { return ( {columns.map((col, idx2) => { return ( ); })} {hasActions && ( )} ); })}
    {col.title} {sortBy === col.name ? ( ) : null} }
    {col.formatter ? getFormatter(col.formatter)(row[col.name], row) : row[col.name]}
    {pagination ? getPagination(pagination) : null} ); };