Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14229-chips-field
[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 } from '@material-ui/core';
7 import { DataColumn, SortDirection } from './data-column';
8 import { DataTableFilters, DataTableFilterItem } from "../data-table-filters/data-table-filters";
9 import { DataTableDefaultView } from '../data-table-default-view/data-table-default-view';
10
11 export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
12
13 export interface DataTableDataProps<T> {
14     items: T[];
15     columns: DataColumns<T>;
16     onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
17     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
18     onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
19     onSortToggle: (column: DataColumn<T>) => void;
20     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
21     extractKey?: (item: T) => React.Key;
22     working?: boolean;
23     defaultView?: React.ReactNode;
24 }
25
26 type CssRules = "tableBody" | "root" | "content" | "noItemsInfo" | 'tableCell';
27
28 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
29     root: {
30         overflowX: 'auto',
31         overflowY: 'hidden'
32     },
33     content: {
34         display: 'inline-block',
35         width: '100%'
36     },
37     tableBody: {
38         background: theme.palette.background.paper
39     },
40     noItemsInfo: {
41         textAlign: "center",
42         padding: theme.spacing.unit
43     },
44     tableCell: {
45         wordWrap: 'break-word'
46     }
47 });
48
49 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
50
51 export const DataTable = withStyles(styles)(
52     class Component<T> extends React.Component<DataTableProps<T>> {
53         render() {
54             const { items, classes } = this.props;
55             return <div className={classes.root}>
56                 <div className={classes.content}>
57                     <Table>
58                         <TableHead>
59                             <TableRow>
60                                 {this.mapVisibleColumns(this.renderHeadCell)}
61                             </TableRow>
62                         </TableHead>
63                         <TableBody className={classes.tableBody}>
64                             {items.map(this.renderBodyRow)}
65                         </TableBody>
66                     </Table>
67                     {items.length === 0 && this.props.working !== undefined && !this.props.working && this.renderNoItemsPlaceholder()}
68                 </div>
69             </div>;
70         }
71
72         renderNoItemsPlaceholder = () => {
73             return this.props.defaultView
74                 ? this.props.defaultView
75                 : <DataTableDefaultView/>;
76         }
77
78         renderHeadCell = (column: DataColumn<T>, index: number) => {
79             const { name, key, renderHeader, filters, sortDirection } = column;
80             const { onSortToggle, onFiltersChange } = this.props;
81             return <TableCell key={key || index}>
82                 {renderHeader ?
83                     renderHeader() :
84                     filters.length > 0
85                         ? <DataTableFilters
86                             name={`${name} filters`}
87                             onChange={filters =>
88                                 onFiltersChange &&
89                                 onFiltersChange(filters, column)}
90                             filters={filters}>
91                             {name}
92                         </DataTableFilters>
93                         : sortDirection
94                             ? <TableSortLabel
95                                 active={sortDirection !== SortDirection.NONE}
96                                 direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
97                                 onClick={() =>
98                                     onSortToggle &&
99                                     onSortToggle(column)}>
100                                 {name}
101                             </TableSortLabel>
102                             : <span>
103                                 {name}
104                             </span>}
105             </TableCell>;
106         }
107
108         renderBodyRow = (item: T, index: number) => {
109             const { onRowClick, onRowDoubleClick, extractKey, classes } = this.props;
110             return <TableRow
111                 hover
112                 key={extractKey ? extractKey(item) : index}
113                 onClick={event => onRowClick && onRowClick(event, item)}
114                 onContextMenu={this.handleRowContextMenu(item)}
115                 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}>
116                 {this.mapVisibleColumns((column, index) => (
117                     <TableCell key={column.key || index} className={classes.tableCell}>
118                         {column.render(item)}
119                     </TableCell>
120                 ))}
121             </TableRow>;
122         }
123
124         mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
125             return this.props.columns.filter(column => column.selected).map(fn);
126         }
127
128         handleRowContextMenu = (item: T) =>
129             (event: React.MouseEvent<HTMLElement>) =>
130                 this.props.onContextMenu(event, item)
131
132     }
133 );