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 (
Showing {p.offset + 1} to {end} of{" "} {p.total} item{p.total === 1 ? "" : "s"}
{end >= p.total ? ({col.title} {sortBy === col.name ? ( ) : null} | ); })} {hasActions &&} |
---|---|
{col.formatter ? getFormatter(col.formatter)(row[col.name], row) : row[col.name]} | ); })} {hasActions && (
|
)}