refs # Update packages to the newest versions (including TS 3.0.1)
[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, Typography } from '@material-ui/core';
7 import { DataColumn, SortDirection } from './data-column';
8 import { DataTableFilters,  DataTableFilterItem } from "../data-table-filters/data-table-filters";
9
10 export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
11
12 export interface DataTableDataProps<T> {
13     items: T[];
14     columns: DataColumns<T>;
15     onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
16     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
17     onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
18     onSortToggle: (column: DataColumn<T>) => void;
19     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
20     extractKey?: (item: T) => React.Key;
21 }
22
23 type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
24
25 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
26     tableContainer: {
27         overflowX: 'auto',
28         overflowY: 'hidden'
29     },
30     tableBody: {
31         background: theme.palette.background.paper
32     },
33     noItemsInfo: {
34         textAlign: "center",
35         padding: theme.spacing.unit
36     }
37 });
38
39 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
40
41 export const DataTable = withStyles(styles)(
42     class Component<T> extends React.Component<DataTableProps<T>> {
43         render() {
44             const { items, classes } = this.props;
45             return <div
46                 className={classes.tableContainer}>
47                 <Table>
48                     <TableHead>
49                         <TableRow>
50                             {this.mapVisibleColumns(this.renderHeadCell)}
51                         </TableRow>
52                     </TableHead>
53                     <TableBody className={classes.tableBody}>
54                         {items.map(this.renderBodyRow)}
55                     </TableBody>
56                 </Table>
57             </div>;
58         }
59
60         renderHeadCell = (column: DataColumn<T>, index: number) => {
61             const { name, key, renderHeader, filters, sortDirection } = column;
62             const { onSortToggle, onFiltersChange } = this.props;
63             return <TableCell key={key || index} style={{ width: column.width, minWidth: column.width }}>
64                 {renderHeader ?
65                     renderHeader() :
66                     filters
67                         ? <DataTableFilters
68                             name={`${name} filters`}
69                             onChange={filters =>
70                                 onFiltersChange &&
71                                 onFiltersChange(filters, column)}
72                             filters={filters}>
73                             {name}
74                         </DataTableFilters>
75                         : sortDirection
76                             ? <TableSortLabel
77                                 active={sortDirection !== SortDirection.NONE}
78                                 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
79                                 onClick={() =>
80                                     onSortToggle &&
81                                     onSortToggle(column)}>
82                                 {name}
83                             </TableSortLabel>
84                             : <span>
85                                 {name}
86                             </span>}
87             </TableCell>;
88         }
89
90         renderBodyRow = (item: T, index: number) => {
91             const { onRowClick, onRowDoubleClick, extractKey } = this.props;
92             return <TableRow
93                 hover
94                 key={extractKey ? extractKey(item) : index}
95                 onClick={event => onRowClick && onRowClick(event, item)}
96                 onContextMenu={this.handleRowContextMenu(item)}
97                 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item) }>
98                 {this.mapVisibleColumns((column, index) => (
99                     <TableCell key={column.key || index}>
100                         {column.render(item)}
101                     </TableCell>
102                 ))}
103             </TableRow>;
104         }
105
106         mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
107             return this.props.columns.filter(column => column.selected).map(fn);
108         }
109
110         handleRowContextMenu = (item: T) =>
111             (event: React.MouseEvent<HTMLElement>) =>
112                 this.props.onContextMenu(event, item)
113
114     }
115 );