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