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 { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
9 import { DataTableFilters } from '../data-table-filters/data-table-filters-tree';
10 import { DataTableFiltersPopover } from '../data-table-filters/data-table-filters-popover';
11 import { countNodes } from '~/models/tree';
13 export type DataColumns<T> = Array<DataColumn<T>>;
15 export interface DataTableDataProps<T> {
17 columns: DataColumns<T>;
18 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
19 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
20 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
21 onSortToggle: (column: DataColumn<T>) => void;
22 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
23 extractKey?: (item: T) => React.Key;
25 defaultView?: React.ReactNode;
28 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell';
30 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
36 display: 'inline-block',
40 background: theme.palette.background.paper
44 padding: theme.spacing.unit
47 wordWrap: 'break-word'
51 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
53 export const DataTable = withStyles(styles)(
54 class Component<T> extends React.Component<DataTableProps<T>> {
56 const { items, classes } = this.props;
57 return <div className={classes.root}>
58 <div className={classes.content}>
62 {this.mapVisibleColumns(this.renderHeadCell)}
65 <TableBody className={classes.tableBody}>
66 {items.map(this.renderBodyRow)}
69 {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
74 renderNoItemsPlaceholder = () => {
75 return this.props.defaultView
76 ? this.props.defaultView
77 : <DataTableDefaultView/>;
80 renderHeadCell = (column: DataColumn<T>, index: number) => {
81 const { name, key, renderHeader, filters, sortDirection } = column;
82 const { onSortToggle, onFiltersChange } = this.props;
83 return <TableCell key={key || index}>
86 countNodes(filters) > 0
87 ? <DataTableFiltersPopover
88 name={`${name} filters`}
91 onFiltersChange(filters, column)}
94 </DataTableFiltersPopover>
97 active={sortDirection !== SortDirection.NONE}
98 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
101 onSortToggle(column)}>
110 renderBodyRow = (item: T, index: number) => {
111 const { onRowClick, onRowDoubleClick, extractKey, classes } = this.props;
114 key={extractKey ? extractKey(item) : index}
115 onClick={event => onRowClick && onRowClick(event, item)}
116 onContextMenu={this.handleRowContextMenu(item)}
117 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}>
118 {this.mapVisibleColumns((column, index) => (
119 <TableCell key={column.key || index} className={classes.tableCell}>
120 {column.render(item)}
126 mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
127 return this.props.columns.filter(column => column.selected).map(fn);
130 handleRowContextMenu = (item: T) =>
131 (event: React.MouseEvent<HTMLElement>) =>
132 this.props.onContextMenu(event, item)