fix test for data-explorer
[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, 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 import { ArvadosTheme } from "../../common/custom-theme";
14 import { DefaultView } from '../default-view/default-view';
15 import { IconType } from '../icon/icon';
16
17 type CssRules = 'searchBox' | "toolbar" | 'defaultRoot' | 'defaultMessage' | 'defaultIcon';
18
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20     searchBox: {
21         paddingBottom: theme.spacing.unit * 2
22     },
23     toolbar: {
24         paddingTop: theme.spacing.unit * 2
25     },
26     defaultRoot: {
27         position: 'absolute',
28         width: '80%',
29         left: '50%',
30         top: '50%',
31         transform: 'translate(-50%, -50%)'
32     },
33     defaultMessage: {
34         fontSize: '1.75rem',
35     },
36     defaultIcon: {
37         fontSize: '6rem'
38     }
39 });
40
41 interface DataExplorerDataProps<T> {
42     items: T[];
43     itemsAvailable: number;
44     columns: DataColumns<T>;
45     searchValue: string;
46     rowsPerPage: number;
47     rowsPerPageOptions: number[];
48     page: number;
49     defaultIcon: IconType;
50     defaultMessages: string[];
51 }
52
53 interface DataExplorerActionProps<T> {
54     onSearch: (value: string) => void;
55     onRowClick: (item: T) => void;
56     onRowDoubleClick: (item: T) => void;
57     onColumnToggle: (column: DataColumn<T>) => void;
58     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
59     onSortToggle: (column: DataColumn<T>) => void;
60     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
61     onChangePage: (page: number) => void;
62     onChangeRowsPerPage: (rowsPerPage: number) => void;
63     extractKey?: (item: T) => React.Key;
64 }
65
66 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
67
68 export const DataExplorer = withStyles(styles)(
69     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
70         render() {
71             const { 
72                 columns, onContextMenu, onFiltersChange, onSortToggle, extractKey, 
73                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch, 
74                 items, itemsAvailable, onRowClick, onRowDoubleClick, defaultIcon, defaultMessages, classes 
75             } = this.props;
76             return <div>
77                 { items.length > 0 ? (
78                     <Paper>
79                         <Toolbar className={classes.toolbar}>
80                             <Grid container justify="space-between" wrap="nowrap" alignItems="center">
81                                 <div className={classes.searchBox}>
82                                     <SearchInput
83                                         value={searchValue}
84                                         onSearch={onSearch}/>
85                                 </div>
86                                 <ColumnSelector
87                                     columns={columns}
88                                     onColumnToggle={onColumnToggle}/>
89                             </Grid>
90                         </Toolbar>
91                         <DataTable
92                             columns={[...columns, this.contextMenuColumn]}
93                             items={items}
94                             onRowClick={(_, item: T) => onRowClick(item)}
95                             onContextMenu={onContextMenu}
96                             onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
97                             onFiltersChange={onFiltersChange}
98                             onSortToggle={onSortToggle}
99                             extractKey={extractKey}/>
100                         <Toolbar>
101                             <Grid container justify="flex-end">
102                                 <TablePagination
103                                     count={itemsAvailable}
104                                     rowsPerPage={rowsPerPage}
105                                     rowsPerPageOptions={rowsPerPageOptions}
106                                     page={this.props.page}
107                                     onChangePage={this.changePage}
108                                     onChangeRowsPerPage={this.changeRowsPerPage}
109                                     component="div" />
110                             </Grid>
111                         </Toolbar>
112                     </Paper>
113                 ) : (
114                     <DefaultView 
115                         classRoot={classes.defaultRoot}
116                         icon={defaultIcon}
117                         classIcon={classes.defaultIcon}
118                         messages={defaultMessages}
119                         classMessage={classes.defaultMessage} />
120                 )}
121             </div>;
122         }
123
124         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
125             this.props.onChangePage(page);
126         }
127
128         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
129             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
130         }
131
132         renderContextMenuTrigger = (item: T) =>
133             <Grid container justify="flex-end">
134                 <IconButton onClick={event => this.props.onContextMenu(event, item)}>
135                     <MoreVertIcon/>
136                 </IconButton>
137             </Grid>
138
139         contextMenuColumn = {
140             name: "Actions",
141             selected: true,
142             configurable: false,
143             key: "context-actions",
144             render: this.renderContextMenuTrigger,
145             width: "auto"
146         };
147     }
148 );