Merge branch 'master'
[arvados-workbench2.git] / src / components / data-explorer / data-explorer.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 { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, Theme, WithStyles, TablePagination, IconButton } from '@material-ui/core';
7 import MoreVertIcon from "@material-ui/icons/MoreVert";
8 import ColumnSelector from "../column-selector/column-selector";
9 import DataTable, { DataColumns } from "../data-table/data-table";
10 import { DataColumn } from "../data-table/data-column";
11 import { DataTableFilterItem } from '../data-table-filters/data-table-filters';
12 import SearchInput from '../search-input/search-input';
13
14 interface DataExplorerProps<T> {
15     items: T[];
16     itemsAvailable: number;
17     columns: DataColumns<T>;
18     searchValue: string;
19     rowsPerPage: number;
20     rowsPerPageOptions?: number[];
21     page: number;
22     onSearch: (value: string) => void;
23     onRowClick: (item: T) => void;
24     onRowDoubleClick: (item: T) => void;
25     onColumnToggle: (column: DataColumn<T>) => void;
26     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
27     onSortToggle: (column: DataColumn<T>) => void;
28     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
29     onChangePage: (page: number) => void;
30     onChangeRowsPerPage: (rowsPerPage: number) => void;
31     extractKey?: (item: T) => React.Key;
32 }
33
34 class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>> {
35
36     render() {
37         return <Paper>
38             <Toolbar className={this.props.classes.toolbar}>
39                 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
40                     <div className={this.props.classes.searchBox}>
41                         <SearchInput
42                             value={this.props.searchValue}
43                             onSearch={this.props.onSearch} />
44                     </div>
45                     <ColumnSelector
46                         columns={this.props.columns}
47                         onColumnToggle={this.props.onColumnToggle} />
48                 </Grid>
49             </Toolbar>
50             <DataTable
51                 columns={[...this.props.columns, this.contextMenuColumn]}
52                 items={this.props.items}
53                 onRowClick={(_, item: T) => this.props.onRowClick(item)}
54                 onContextMenu={this.props.onContextMenu}
55                 onRowDoubleClick={(_, item: T) => this.props.onRowDoubleClick(item)}
56                 onFiltersChange={this.props.onFiltersChange}
57                 onSortToggle={this.props.onSortToggle}
58                 extractKey={this.props.extractKey} />
59             <Toolbar>
60                 {this.props.items.length > 0 &&
61                     <Grid container justify="flex-end">
62                         <TablePagination
63                             count={this.props.itemsAvailable}
64                             rowsPerPage={this.props.rowsPerPage}
65                             rowsPerPageOptions={this.props.rowsPerPageOptions}
66                             page={this.props.page}
67                             onChangePage={this.changePage}
68                             onChangeRowsPerPage={this.changeRowsPerPage}
69                             component="div"
70                         />
71                     </Grid>}
72             </Toolbar>
73         </Paper>;
74     }
75
76     changePage = (event: React.MouseEvent<HTMLButtonElement> | null, page: number) => {
77         this.props.onChangePage(page);
78     }
79
80     changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
81         this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
82     }
83
84     renderContextMenuTrigger = (item: T) =>
85         <Grid container justify="flex-end">
86             <IconButton onClick={event => this.props.onContextMenu(event, item)}>
87                 <MoreVertIcon />
88             </IconButton>
89         </Grid>
90
91     contextMenuColumn = {
92         name: "Actions",
93         selected: true,
94         key: "context-actions",
95         renderHeader: () => null,
96         render: this.renderContextMenuTrigger,
97         width: "auto"
98     };
99
100 }
101
102 type CssRules = "searchBox" | "toolbar";
103
104 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
105     searchBox: {
106         paddingBottom: theme.spacing.unit * 2
107     },
108     toolbar: {
109         paddingTop: theme.spacing.unit * 2
110     }
111 });
112
113 export default withStyles(styles)(DataExplorer);