X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/5fd5db805554d3eddd46cc911a5108fbb74b3cfc..a0a42fd73533f2f17844cd9343519c34043fda71:/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 e86113ef..1d208d75 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,26 +3,50 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
+import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core';
 import { DataColumn, SortDirection } from './data-column';
-import DataTableFilters, { DataTableFilterItem } from "../data-table-filters/data-table-filters";
+import { DataTableFilters,  DataTableFilterItem } from "../data-table-filters/data-table-filters";
 
-export type DataColumns<T> = Array<DataColumn<T>>;
+export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
 
-export interface DataTableProps<T> {
+export interface DataTableDataProps<T> {
     items: T[];
     columns: DataColumns<T>;
     onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
-    onRowContextMenu: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
+    onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
+    onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: T) => void;
     onSortToggle: (column: DataColumn<T>) => void;
     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
+    extractKey?: (item: T) => React.Key;
 }
 
-class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRules>> {
-    render() {
-        const { items, classes } = this.props;
-        return <div className={classes.tableContainer}>
-            {items.length > 0 ?
+type CssRules = "tableBody" | "tableContainer" | "noItemsInfo" | 'tableCell';
+
+const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+    tableContainer: {
+        overflowX: 'auto',
+        overflowY: 'hidden'
+    },
+    tableBody: {
+        background: theme.palette.background.paper
+    },
+    noItemsInfo: {
+        textAlign: "center",
+        padding: theme.spacing.unit
+    },
+    tableCell: {
+        wordWrap: 'break-word'
+    }
+});
+
+type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
+
+export const DataTable = withStyles(styles)(
+    class Component<T> extends React.Component<DataTableProps<T>> {
+        render() {
+            const { items, classes } = this.props;
+            return <div
+                className={classes.tableContainer}>
                 <Table>
                     <TableHead>
                         <TableRow>
@@ -32,79 +56,63 @@ class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRul
                     <TableBody className={classes.tableBody}>
                         {items.map(this.renderBodyRow)}
                     </TableBody>
-                </Table> : <Typography
-                    className={classes.noItemsInfo}
-                    variant="body2"
-                    gutterBottom>
-                    No items
-                </Typography>}
-        </div>;
-    }
+                </Table>
+            </div>;
+        }
 
-    renderHeadCell = (column: DataColumn<T>, index: number) => {
-        const { name, key, renderHeader, filters, sortDirection } = column;
-        const { onSortToggle, onFiltersChange } = this.props;
-        return <TableCell key={key || index}>
-            {renderHeader ?
-                renderHeader() :
-                filters
-                    ? <DataTableFilters
-                        name={`${name} filters`}
-                        onChange={filters =>
-                            onFiltersChange &&
-                            onFiltersChange(filters, column)}
-                        filters={filters}>
-                        {name}
-                    </DataTableFilters>
-                    : sortDirection
-                        ? <TableSortLabel
-                            active={sortDirection !== "none"}
-                            direction={sortDirection !== "none" ? sortDirection : undefined}
-                            onClick={() =>
-                                onSortToggle &&
-                                onSortToggle(column)}>
-                            {name}
-                        </TableSortLabel>
-                        : <span>
+        renderHeadCell = (column: DataColumn<T>, index: number) => {
+            const { name, key, renderHeader, filters, sortDirection } = column;
+            const { onSortToggle, onFiltersChange } = this.props;
+            return <TableCell key={key || index}>
+                {renderHeader ?
+                    renderHeader() :
+                    filters.length > 0
+                        ? <DataTableFilters
+                            name={`${name} filters`}
+                            onChange={filters =>
+                                onFiltersChange &&
+                                onFiltersChange(filters, column)}
+                            filters={filters}>
                             {name}
-                        </span>}
-        </TableCell>;
-    }
-
-    renderBodyRow = (item: T, index: number) => {
-        const { columns, onRowClick, onRowContextMenu } = this.props;
-        return <TableRow
-            hover
-            key={index}
-            onClick={event => onRowClick && onRowClick(event, item)}
-            onContextMenu={event => onRowContextMenu && onRowContextMenu(event, item)}>
-            {this.mapVisibleColumns((column, index) => (
-                <TableCell key={column.key || index}>
-                    {column.render(item)}
-                </TableCell>
-            ))}
-        </TableRow>;
-    }
+                        </DataTableFilters>
+                        : sortDirection
+                            ? <TableSortLabel
+                                active={sortDirection !== SortDirection.NONE}
+                                direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
+                                onClick={() =>
+                                    onSortToggle &&
+                                    onSortToggle(column)}>
+                                {name}
+                            </TableSortLabel>
+                            : <span>
+                                {name}
+                            </span>}
+            </TableCell>;
+        }
 
-    mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
-        return this.props.columns.filter(column => column.selected).map(fn);
-    }
+        renderBodyRow = (item: T, index: number) => {
+            const { onRowClick, onRowDoubleClick, extractKey, classes } = this.props;
+            return <TableRow
+                hover
+                key={extractKey ? extractKey(item) : index}
+                onClick={event => onRowClick && onRowClick(event, item)}
+                onContextMenu={this.handleRowContextMenu(item)}
+                onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item) }>
+                {this.mapVisibleColumns((column, index) => (
+                    <TableCell key={column.key || index} className={classes.tableCell}>
+                        {column.render(item)}
+                    </TableCell>
+                ))}
+            </TableRow>;
+        }
 
-}
+        mapVisibleColumns = (fn: (column: DataColumn<T>, index: number) => React.ReactElement<any>) => {
+            return this.props.columns.filter(column => column.selected).map(fn);
+        }
 
-type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
+        handleRowContextMenu = (item: T) =>
+            (event: React.MouseEvent<HTMLElement>) =>
+                this.props.onContextMenu(event, item)
 
-const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
-    tableContainer: {
-        overflowX: 'auto'
-    },
-    tableBody: {
-        background: theme.palette.background.paper
-    },
-    noItemsInfo: {
-        textAlign: "center",
-        padding: theme.spacing.unit
     }
-});
-
-export default withStyles(styles)(DataTable);
+);
