1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
7 import { DataColumn, SortDirection } from './data-column';
8 import DataTableFilters, { DataTableFilterItem } from "../data-table-filters/data-table-filters";
10 export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
11 export interface DataItem {
14 export interface DataTableProps<T> {
16 columns: DataColumns<T>;
17 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
18 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
19 onRowContextMenu: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
20 onSortToggle: (column: DataColumn<T>) => void;
21 onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
24 class DataTable<T extends DataItem> extends React.Component<DataTableProps<T> & WithStyles<CssRules>> {
26 const { items, classes } = this.props;
27 return <div className={classes.tableContainer}>
31 {this.mapVisibleColumns(this.renderHeadCell)}
34 <TableBody className={classes.tableBody}>
35 {items.map(this.renderBodyRow)}
41 renderHeadCell = (column: DataColumn<T>, index: number) => {
42 const { name, key, renderHeader, filters, sortDirection } = column;
43 const { onSortToggle, onFiltersChange } = this.props;
44 return <TableCell key={key || index} style={{width: column.width, minWidth: column.width}}>
49 name={`${name} filters`}
52 onFiltersChange(filters, column)}
58 active={sortDirection !== SortDirection.None}
59 direction={sortDirection !== SortDirection.None ? sortDirection : undefined}
62 onSortToggle(column)}>
71 renderBodyRow = (item: T, index: number) => {
72 const { onRowClick, onRowDoubleClick, onRowContextMenu } = this.props;
76 onClick={event => onRowClick && onRowClick(event, item)}
77 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item) }
78 onContextMenu={event => onRowContextMenu && onRowContextMenu(event, item)}>
79 {this.mapVisibleColumns((column, index) => (
80 <TableCell key={column.key || index}>
87 mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
88 return this.props.columns.filter(column => column.selected).map(fn);
93 type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
95 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
101 background: theme.palette.background.paper
105 padding: theme.spacing.unit
109 export default withStyles(styles)(DataTable);