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, IconButton } from '@material-ui/core';
7 import classnames from 'classnames';
8 import { DataColumn, SortDirection } from './data-column';
9 import { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
10 import { DataTableFilters } from '../data-table-filters/data-table-filters-tree';
11 import { DataTableFiltersPopover } from '../data-table-filters/data-table-filters-popover';
12 import { countNodes } from '~/models/tree';
13 import { SvgIconProps } from '@material-ui/core/SvgIcon';
14 import ArrowDownwardIcon from '@material-ui/icons/ArrowDownward';
16 export type DataColumns<T> = Array<DataColumn<T>>;
18 export enum DataTableFetchMode {
23 export interface DataTableDataProps<T> {
25 columns: DataColumns<T>;
26 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
27 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
28 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
29 onSortToggle: (column: DataColumn<T>) => void;
30 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
31 extractKey?: (item: T) => React.Key;
33 defaultView?: React.ReactNode;
34 currentItemUuid?: string;
35 currentRoute?: string;
38 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell' | 'arrow' | 'arrowButton' | 'tableCellWorkflows';
40 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
46 display: 'inline-block',
50 background: theme.palette.background.paper
54 padding: theme.spacing.unit
57 wordWrap: 'break-word'
60 '&:nth-last-child(2)': {
74 color: theme.palette.text.primary
78 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
80 export const DataTable = withStyles(styles)(
81 class Component<T> extends React.Component<DataTableProps<T>> {
83 const { items, classes } = this.props;
84 return <div className={classes.root}>
85 <div className={classes.content}>
89 {this.mapVisibleColumns(this.renderHeadCell)}
92 <TableBody className={classes.tableBody}>
93 {items.map(this.renderBodyRow)}
96 {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
101 renderNoItemsPlaceholder = () => {
102 return this.props.defaultView
103 ? this.props.defaultView
104 : <DataTableDefaultView />;
107 renderHeadCell = (column: DataColumn<T>, index: number) => {
108 const { name, key, renderHeader, filters, sortDirection } = column;
109 const { onSortToggle, onFiltersChange } = this.props;
110 return <TableCell key={key || index}>
113 countNodes(filters) > 0
114 ? <DataTableFiltersPopover
115 name={`${name} filters`}
116 mutuallyExclusive={column.mutuallyExclusiveFilters}
119 onFiltersChange(filters, column)}
122 </DataTableFiltersPopover>
125 active={sortDirection !== SortDirection.NONE}
126 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
127 IconComponent={this.ArrowIcon}
131 onSortToggle(column)}>
140 ArrowIcon = ({ className, ...props }: SvgIconProps) => (
141 <IconButton component='span' className={this.props.classes.arrowButton} tabIndex={-1}>
142 <ArrowDownwardIcon {...props} className={classnames(className, this.props.classes.arrow)} />
146 renderBodyRow = (item: any, index: number) => {
147 const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
150 key={extractKey ? extractKey(item) : index}
151 onClick={event => onRowClick && onRowClick(event, item)}
152 onContextMenu={this.handleRowContextMenu(item)}
153 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
154 selected={item === currentItemUuid}>
155 {this.mapVisibleColumns((column, index) => (
156 <TableCell key={column.key || index} className={currentRoute === '/workflows' ? classes.tableCellWorkflows : classes.tableCell}>
157 {column.render(item)}
163 mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
164 return this.props.columns.filter(column => column.selected).map(fn);
167 handleRowContextMenu = (item: T) =>
168 (event: React.MouseEvent<HTMLElement>) =>
169 this.props.onContextMenu(event, item)