15012: Uses varargs version of joinFilters().
[arvados-workbench2.git] / src / components / data-table / data-table.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
15
16 export type DataColumns<T> = Array<DataColumn<T>>;
17
18 export enum DataTableFetchMode {
19     PAGINATED,
20     INFINITE
21 }
22
23 export interface DataTableDataProps<T> {
24     items: 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;
32     working?: boolean;
33     defaultView?: React.ReactNode;
34     currentItemUuid?: string;
35     currentRoute?: string;
36 }
37
38 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell' | 'arrow' | 'arrowButton' | 'tableCellWorkflows';
39
40 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
41     root: {
42         overflowX: 'auto',
43         overflowY: 'hidden'
44     },
45     content: {
46         display: 'inline-block',
47         width: '100%'
48     },
49     tableBody: {
50         background: theme.palette.background.paper
51     },
52     noItemsInfo: {
53         textAlign: "center",
54         padding: theme.spacing.unit
55     },
56     tableCell: {
57         wordWrap: 'break-word'
58     },
59     tableCellWorkflows: {
60         '&:nth-last-child(2)': {
61             padding: '0px',
62             maxWidth: '48px'
63         },
64         '&:last-child': {
65             padding: '0px',
66             paddingRight: '24px',
67             width: '48px'
68         }
69     },
70     arrow: {
71         margin: 0
72     },
73     arrowButton: {
74         color: theme.palette.text.primary
75     }
76 });
77
78 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
79
80 export const DataTable = withStyles(styles)(
81     class Component<T> extends React.Component<DataTableProps<T>> {
82         render() {
83             const { items, classes } = this.props;
84             return <div className={classes.root}>
85                 <div className={classes.content}>
86                     <Table>
87                         <TableHead>
88                             <TableRow>
89                                 {this.mapVisibleColumns(this.renderHeadCell)}
90                             </TableRow>
91                         </TableHead>
92                         <TableBody className={classes.tableBody}>
93                             {items.map(this.renderBodyRow)}
94                         </TableBody>
95                     </Table>
96                     {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
97                 </div>
98             </div>;
99         }
100
101         renderNoItemsPlaceholder = () => {
102             return this.props.defaultView
103                 ? this.props.defaultView
104                 : <DataTableDefaultView />;
105         }
106
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}>
111                 {renderHeader ?
112                     renderHeader() :
113                     countNodes(filters) > 0
114                         ? <DataTableFiltersPopover
115                             name={`${name} filters`}
116                             mutuallyExclusive={column.mutuallyExclusiveFilters}
117                             onChange={filters =>
118                                 onFiltersChange &&
119                                 onFiltersChange(filters, column)}
120                             filters={filters}>
121                             {name}
122                         </DataTableFiltersPopover>
123                         : sortDirection
124                             ? <TableSortLabel
125                                 active={sortDirection !== SortDirection.NONE}
126                                 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
127                                 IconComponent={this.ArrowIcon}
128                                 hideSortIcon
129                                 onClick={() =>
130                                     onSortToggle &&
131                                     onSortToggle(column)}>
132                                 {name}
133                             </TableSortLabel>
134                             : <span>
135                                 {name}
136                             </span>}
137             </TableCell>;
138         }
139
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)} />
143             </IconButton>
144         )
145
146         renderBodyRow = (item: any, index: number) => {
147             const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
148             return <TableRow
149                 hover
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)}
158                     </TableCell>
159                 ))}
160             </TableRow>;
161         }
162
163         mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
164             return this.props.columns.filter(column => column.selected).map(fn);
165         }
166
167         handleRowContextMenu = (item: T) =>
168             (event: React.MouseEvent<HTMLElement>) =>
169                 this.props.onContextMenu(event, item)
170
171     }
172 );