X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0c8dea6be6e43f6331655cbf916946e5750b5aa9..876e5e2f31a218255845977489ea70aacc0211cd:/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 854b322052..34f8168a55 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,94 +3,21 @@
 // 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, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
-export interface DataItem {
-    key: React.Key;
-}
-export interface DataTableProps<T> {
+
+export interface DataTableDataProps<T> {
     items: T[];
     columns: DataColumns<T>;
     onRowClick: (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;
-}
-
-class DataTable<T extends DataItem> extends React.Component<DataTableProps<T> & WithStyles<CssRules>> {
-    render() {
-        const { items, classes } = this.props;
-        return <div
-            className={classes.tableContainer}>
-            <Table>
-                <TableHead>
-                    <TableRow>
-                        {this.mapVisibleColumns(this.renderHeadCell)}
-                    </TableRow>
-                </TableHead>
-                <TableBody className={classes.tableBody}>
-                    {items.map(this.renderBodyRow)}
-                </TableBody>
-            </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} style={{ width: column.width, minWidth: column.width }}>
-            {renderHeader ?
-                renderHeader() :
-                filters
-                    ? <DataTableFilters
-                        name={`${name} filters`}
-                        onChange={filters =>
-                            onFiltersChange &&
-                            onFiltersChange(filters, column)}
-                        filters={filters}>
-                        {name}
-                    </DataTableFilters>
-                    : sortDirection
-                        ? <TableSortLabel
-                            active={sortDirection !== SortDirection.None}
-                            direction={sortDirection !== SortDirection.None ? sortDirection : undefined}
-                            onClick={() =>
-                                onSortToggle &&
-                                onSortToggle(column)}>
-                            {name}
-                        </TableSortLabel>
-                        : <span>
-                            {name}
-                        </span>}
-        </TableCell>;
-    }
-
-    renderBodyRow = (item: T, index: number) => {
-        const { onRowClick, onContextMenu } = this.props;
-        return <TableRow
-            hover
-            key={item.key}
-            onClick={event => onRowClick && onRowClick(event, item)}
-            onContextMenu={this.handleRowContextMenu(item)}>
-            {this.mapVisibleColumns((column, index) => (
-                <TableCell key={column.key || index}>
-                    {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);
-    }
-
-    handleRowContextMenu = (item: T) =>
-        (event: React.MouseEvent<HTMLElement>) =>
-            this.props.onContextMenu(event, item)
-
+    extractKey?: (item: T) => React.Key;
 }
 
 type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
@@ -109,4 +36,80 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     }
 });
 
-export default withStyles(styles)(DataTable);
+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>
+                            {this.mapVisibleColumns(this.renderHeadCell)}
+                        </TableRow>
+                    </TableHead>
+                    <TableBody className={classes.tableBody}>
+                        {items.map(this.renderBodyRow)}
+                    </TableBody>
+                </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} style={{ width: column.width, minWidth: column.width }}>
+                {renderHeader ?
+                    renderHeader() :
+                    filters
+                        ? <DataTableFilters
+                            name={`${name} filters`}
+                            onChange={filters =>
+                                onFiltersChange &&
+                                onFiltersChange(filters, column)}
+                            filters={filters}>
+                            {name}
+                        </DataTableFilters>
+                        : sortDirection
+                            ? <TableSortLabel
+                                active={sortDirection !== SortDirection.NONE}
+                                direction={sortDirection !== SortDirection.NONE ? sortDirection : undefined}
+                                onClick={() =>
+                                    onSortToggle &&
+                                    onSortToggle(column)}>
+                                {name}
+                            </TableSortLabel>
+                            : <span>
+                                {name}
+                            </span>}
+            </TableCell>;
+        }
+
+        renderBodyRow = (item: T, index: number) => {
+            const { onRowClick, onRowDoubleClick, extractKey } = 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}>
+                        {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);
+        }
+
+        handleRowContextMenu = (item: T) =>
+            (event: React.MouseEvent<HTMLElement>) =>
+                this.props.onContextMenu(event, item)
+
+    }
+);
