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 } from '@material-ui/core';
7 import { DataColumn, SortDirection } from './data-column';
8 import { DataTableFilters, DataTableFilterItem } from "../data-table-filters/data-table-filters";
9 import { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
11 export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
13 export interface DataTableDataProps<T> {
15 columns: DataColumns<T>;
16 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
17 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
18 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
19 onSortToggle: (column: DataColumn<T>) => void;
20 onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
21 extractKey?: (item: T) => React.Key;
23 defaultView?: React.ReactNode;
26 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell';
28 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
34 display: 'inline-block',
38 background: theme.palette.background.paper
42 padding: theme.spacing.unit
45 wordWrap: 'break-word'
49 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
51 export const DataTable = withStyles(styles)(
52 class Component<T> extends React.Component<DataTableProps<T>> {
54 const { items, classes } = this.props;
55 return <div className={classes.root}>
56 <div className={classes.content}>
60 {this.mapVisibleColumns(this.renderHeadCell)}
63 <TableBody className={classes.tableBody}>
64 {items.map(this.renderBodyRow)}
67 {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
72 renderNoItemsPlaceholder = () => {
73 return this.props.defaultView
74 ? this.props.defaultView
75 : <DataTableDefaultView/>;
78 renderHeadCell = (column: DataColumn<T>, index: number) => {
79 const { name, key, renderHeader, filters, sortDirection } = column;
80 const { onSortToggle, onFiltersChange } = this.props;
81 return <TableCell key={key || index}>
86 name={`${name} filters`}
89 onFiltersChange(filters, column)}
95 active={sortDirection !== SortDirection.NONE}
96 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
99 onSortToggle(column)}>
108 renderBodyRow = (item: T, index: number) => {
109 const { onRowClick, onRowDoubleClick, extractKey, classes } = this.props;
112 key={extractKey ? extractKey(item) : index}
113 onClick={event => onRowClick && onRowClick(event, item)}
114 onContextMenu={this.handleRowContextMenu(item)}
115 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}>
116 {this.mapVisibleColumns((column, index) => (
117 <TableCell key={column.key || index} className={classes.tableCell}>
118 {column.render(item)}
124 mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
125 return this.props.columns.filter(column => column.selected).map(fn);
128 handleRowContextMenu = (item: T) =>
129 (event: React.MouseEvent<HTMLElement>) =>
130 this.props.onContextMenu(event, item)