X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/7cea5abf194fe6709ec25dbbd819e4c4be2faba0..1a9eb2261e6030ba78078e2a206bad27653f2475:/src/components/data-table/data-table.tsx

diff --git a/src/components/data-table/data-table.tsx b/src/components/data-table/data-table.tsx
index 5a372c41..43efdd0a 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,66 +3,71 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { Table, TableBody, TableRow, TableCell, TableHead, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
-import { Column } from './column';
+import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
+import { DataColumn } from './data-column';
+
+export type DataColumns<T> = Array<DataColumn<T>>;
 
 export interface DataTableProps<T> {
     items: T[];
-    columns: Array<Column<T>>;
-    onItemClick?: (item: T) => void;
+    columns: DataColumns<T>;
+    onRowClick?: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
+    onRowContextMenu?: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
 }
 
 class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRules>> {
     render() {
-        const { items, columns, classes, onItemClick } = this.props;
-        return (
-            <div className={classes.tableContainer}>
-                {
-                    items.length > 0 ? (
-                        <Table>
-                            <TableHead>
-                                <TableRow>
-                                    {
-                                        columns
-                                            .filter(column => column.selected)
-                                            .map(({ header, renderHeader, key }, index) => (
-                                                <TableCell key={key || index}>
-                                                    {renderHeader ? renderHeader() : header}
-                                                </TableCell>
-                                            ))
-                                    }
+        const { items, columns, classes, onRowClick, onRowContextMenu } = this.props;
+        return <div className={classes.tableContainer}>
+            {items.length > 0 ?
+                <Table>
+                    <TableHead>
+                        <TableRow>
+                            {columns
+                                .filter(column => column.selected)
+                                .map(({ name, renderHeader, key, sortDirection, onSortToggle }, index) =>
+                                    <TableCell key={key || index}>
+                                        {renderHeader ?
+                                            renderHeader() :
+                                            <TableSortLabel
+                                                active={!!sortDirection}
+                                                direction={sortDirection}
+                                                onClick={() => onSortToggle && onSortToggle()}>
+                                                {name}
+                                            </TableSortLabel>}
+                                    </TableCell>
+                                )}
+                        </TableRow>
+                    </TableHead>
+                    <TableBody className={classes.tableBody}>
+                        {items
+                            .map((item, index) =>
+                                <TableRow
+                                    hover
+                                    key={index}
+                                    onClick={event => onRowClick && onRowClick(event, item)}
+                                    onContextMenu={event => onRowContextMenu && onRowContextMenu(event, item)}>
+                                    {columns
+                                        .filter(column => column.selected)
+                                        .map((column, index) => (
+                                            <TableCell key={column.key || index}>
+                                                {column.render(item)}
+                                            </TableCell>
+                                        ))}
                                 </TableRow>
-                            </TableHead>
-                            <TableBody className={classes.tableBody}>
-                                {
-                                    items
-                                        .map((item, index) => (
-                                            <TableRow key={index} hover onClick={() => onItemClick && onItemClick(item)}>
-                                                {
-                                                    columns
-                                                        .filter(column => column.selected)
-                                                        .map((column, index) => (
-                                                            <TableCell key={column.key || index}>
-                                                                {column.render(item)}
-                                                            </TableCell>
-                                                        ))
-                                                }
-                                            </TableRow>
-                                        ))
-                                }
-                            </TableBody>
-                        </Table>
-                    ) : (
-                            <Typography>No items</Typography>
-                        )
-                }
-
-            </div>
-        );
+                            )}
+                    </TableBody>
+                </Table> : <Typography
+                    className={classes.noItemsInfo}
+                    variant="body2"
+                    gutterBottom>
+                    No items
+                </Typography>}
+        </div>;
     }
 }
 
-type CssRules = "tableBody" | "tableContainer";
+type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
 
 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     tableContainer: {
@@ -70,6 +75,10 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     },
     tableBody: {
         background: theme.palette.background.paper
+    },
+    noItemsInfo: {
+        textAlign: "center",
+        padding: theme.spacing.unit
     }
 });
 
