import * as React from "react"; import * as _ from "underscore"; import { ClassHelpers } from "../../"; import { Dialog } from "../display/dialog"; import { TableFiltersDialog } from "./tableFilterDialog"; import { TableFilters } from "./tableFilters"; import { TableHeading } from "./tableHeader"; import { TableItem } from "./tableItem"; import { TableItemDropdown } from "./tableItemDropdown"; import { TableOptions } from "./tableOptions"; import { IPaginateButtonProps, TablePagination } from "./tablePagingation"; import { TableTitle } from "./tableTitle"; import { IDataTableOptions, IFilter, IFilterParameters, TFilterAction, TSortDirection, } from "./tableTypes"; export interface ITableProps { /** (React.ReactNode) Specify the formatting of the individual column, you have access to both the */ columnFormatter?: { [P in keyof T]?: (value: T[P], row?: T) => React.ReactNode; }; /** (T[]) The data to display */ data?: T[]; /** (IFilter[]) The active filter list */ filters?: IFilter[]; /** The filterable list */ filterList?: Array>; /** (React.ReactNode) Specify the formatting of the header column */ headerFormatter: { [P in keyof T]?: (name: keyof T) => React.ReactNode; }; /** (number) The number of pages of data */ numberOfPages?: number; /** ((rows:number) => void) Event to fire when user changes the max number of items per page */ onChangeRowsPerPage?: (rows: number) => void; /** ((pageNumber:number) => void) Event to fire when user changes the page number */ onChangePage?: (pageNumber: number) => void; /** () => void Event to fire when user downloads the table */ onDownload?: () => void; /** () => void Event to fire when user prints the table */ onPrint?: (ref: HTMLTableElement) => void; /** ((key:keyof T, direction: TSortDirection) => void) Event to fire when user sorts the columns */ onSortBy?: (key: keyof T, direction: TSortDirection) => void; /** () => void Event to fire when user filters the data by a column */ onUpdateFilter?: ( action: TFilterAction, key?: any, value?: React.ReactNode, ) => void; /** (options:IDataTableOptions) Options for the data table */ options?: IDataTableOptions; /** (component) Pagination Element */ paginationElement?: React.FC; /** (string) The sub or secondary title of the table */ subTitle?: string; /** (string) The title of the table */ title?: string; } export function Table({ data, filters, filterList, columnFormatter, headerFormatter, numberOfPages, onChangeRowsPerPage, onChangePage, onDownload, onPrint, onSortBy, onUpdateFilter, options, paginationElement: PaginationElement, subTitle, title, }: React.PropsWithChildren>) { const ref = React.useRef(null); const [currentPage, setCurrentPage] = React.useState(1); const [filterDialogState, setFilterDialogState] = React.useState( false, ); const columnKeys = React.useMemo(() => { return Object.keys(headerFormatter) as Array; }, [headerFormatter]); return (
{options && (
onUpdateFilter("remove", key, value) } /> onPrint(ref.current)} onFilter={() => { setFilterDialogState(true); }} />
)} {options && options.hideHeaders ? null : ( {headerFormatter && columnKeys.map((header: keyof T, idx: number) => ( key={idx} name={header} cell={headerFormatter[header]} sortBy={(onSortBy && onSortBy) || null} initSort={ options && options.sort && options.sort.initialSortBy && options.sort.initialSortBy.key === header ? options.sort.initialSortBy.direction : null } /> ))} )} {data.map((rows: T, idx: number) => { return ( ); })}
{!data || (data.length === 0 && "No Data")} {onChangePage && numberOfPages && PaginationElement && (
{ return ( { onChangePage(r.index); setCurrentPage(r.index); }} direction={(r.direction && r.direction) || null} /> ); }} />
)} setFilterDialogState(false)} > {filterList && ( onUpdateFilter("clear")} /> )}
); }