X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/fbd2012fa56f6de44dfc4008d70e5a21cfcaf459..47e0dc87fa82bac593c53518e556ba7c55410288:/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 0367dfa9c7..829bc84ebe 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,63 +3,113 @@
 // 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 { DataColumn } from './data-column';
+import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
+import { DataColumn, SortDirection } from './data-column';
+import { DataTableFilters,  DataTableFilterItem } from "../data-table-filters/data-table-filters";
 
-export interface DataTableProps<T> {
+export type DataColumns<T, F extends DataTableFilterItem = DataTableFilterItem> = Array<DataColumn<T, F>>;
+
+export interface DataTableDataProps<T> {
     items: T[];
-    columns: Array<DataColumn<T>>;
-    onItemClick?: (item: T) => void;
+    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;
+    extractKey?: (item: T) => React.Key;
 }
 
-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 ?
+type CssRules = "tableBody" | "tableContainer" | "noItemsInfo";
+
+const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+    tableContainer: {
+        overflowX: 'auto',
+        overflowY: 'hidden'
+    },
+    tableBody: {
+        background: theme.palette.background.paper
+    },
+    noItemsInfo: {
+        textAlign: "center",
+        padding: theme.spacing.unit
+    }
+});
+
+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>
-                            {columns
-                                .filter(column => column.selected)
-                                .map(({ name, renderHeader, key }, index) =>
-                                    <TableCell key={key || index}>
-                                        {renderHeader ? renderHeader() : name}
-                                    </TableCell>
-                                )}
+                            {this.mapVisibleColumns(this.renderHeadCell)}
                         </TableRow>
                     </TableHead>
                     <TableBody className={classes.tableBody}>
-                        {items
-                            .map((item, index) =>
-                                <TableRow
-                                    hover
-                                    key={index}
-                                    onClick={() => onItemClick && onItemClick(item)}>
-                                    {columns
-                                        .filter(column => column.selected)
-                                        .map((column, index) => (
-                                            <TableCell key={column.key || index}>
-                                                {column.render(item)}
-                                            </TableCell>
-                                        ))}
-                                </TableRow>
-                            )}
+                        {items.map(this.renderBodyRow)}
                     </TableBody>
-                </Table> : <Typography>No items</Typography>}
-        </div>;
-    }
-}
+                </Table>
+            </div>;
+        }
 
-type CssRules = "tableBody" | "tableContainer";
+        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>;
+        }
 
-const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
-    tableContainer: {
-        overflowX: 'auto'
-    },
-    tableBody: {
-        background: theme.palette.background.paper
-    }
-});
+        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>;
+        }
 
-export default withStyles(styles)(DataTable);
+        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)
+
+    }
+);
