Add pagination to 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, Theme, WithStyles, TablePagination, Table } from '@material-ui/core';
7 import ContextMenu, { ContextMenuActionGroup, ContextMenuAction } from "../../components/context-menu/context-menu";
8 import ColumnSelector from "../../components/column-selector/column-selector";
9 import DataTable from "../../components/data-table/data-table";
10 import { mockAnchorFromMouseEvent } from "../../components/popover/helpers";
11 import { DataColumn, toggleSortDirection } from "../../components/data-table/data-column";
12 import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
13 import SearchInput from '../search-input/search-input';
14
15 interface DataExplorerProps<T> {
16     items: T[];
17     columns: Array<DataColumn<T>>;
18     contextActions: ContextMenuActionGroup[];
19     searchValue: string;
20     rowsPerPage: number;
21     page: number;
22     onSearch: (value: string) => void;
23     onRowClick: (item: T) => void;
24     onColumnToggle: (column: DataColumn<T>) => void;
25     onContextAction: (action: ContextMenuAction, item: T) => void;
26     onSortToggle: (column: DataColumn<T>) => void;
27     onFiltersChange: (filters: DataTableFilterItem[], column: DataColumn<T>) => void;
28     onChangePage: (page: number) => void;
29     onChangeRowsPerPage: (rowsPerPage: number) => void;
30 }
31
32 interface DataExplorerState<T> {
33     contextMenu: {
34         anchorEl?: HTMLElement;
35         item?: T;
36     };
37 }
38
39 class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>, DataExplorerState<T>> {
40     state: DataExplorerState<T> = {
41         contextMenu: {}
42     };
43
44     render() {
45         return <Paper>
46             <ContextMenu
47                 anchorEl={this.state.contextMenu.anchorEl}
48                 actions={this.props.contextActions}
49                 onActionClick={this.callAction}
50                 onClose={this.closeContextMenu} />
51             <Toolbar className={this.props.classes.toolbar}>
52                 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
53                     <div className={this.props.classes.searchBox}>
54                         <SearchInput
55                             value={this.props.searchValue}
56                             onSearch={this.props.onSearch} />
57                     </div>
58                     <ColumnSelector
59                         columns={this.props.columns}
60                         onColumnToggle={this.props.onColumnToggle} />
61                 </Grid>
62             </Toolbar>
63             <DataTable
64                 columns={this.props.columns}
65                 items={this.props.items}
66                 onRowClick={(_, item: T) => this.props.onRowClick(item)}
67                 onRowContextMenu={this.openContextMenu}
68                 onFiltersChange={this.props.onFiltersChange}
69                 onSortToggle={this.props.onSortToggle} />
70             <Toolbar>
71                 {this.props.items.length > 0 &&
72                     <Grid container justify="flex-end">
73                         <TablePagination
74                             count={this.props.items.length}
75                             rowsPerPage={this.props.rowsPerPage}
76                             page={this.props.page}
77                             onChangePage={this.changePage}
78                             onChangeRowsPerPage={this.changeRowsPerPage}
79                             component="div"
80                         />
81                     </Grid>}
82             </Toolbar>
83         </Paper>;
84     }
85
86     openContextMenu = (event: React.MouseEvent<HTMLElement>, item: T) => {
87         event.preventDefault();
88         this.setState({
89             contextMenu: {
90                 anchorEl: mockAnchorFromMouseEvent(event),
91                 item
92             }
93         });
94     }
95
96     closeContextMenu = () => {
97         this.setState({ contextMenu: {} });
98     }
99
100     callAction = (action: ContextMenuAction) => {
101         const { item } = this.state.contextMenu;
102         this.closeContextMenu();
103         if (item) {
104             this.props.onContextAction(action, item);
105         }
106     }
107
108     changePage = (event: React.MouseEvent<HTMLButtonElement> | null, page: number) => {
109         this.props.onChangePage(page);
110     }
111
112     changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
113         this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
114     }
115
116 }
117
118 type CssRules = "searchBox" | "toolbar";
119
120 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
121     searchBox: {
122         paddingBottom: theme.spacing.unit * 2
123     },
124     toolbar: {
125         paddingTop: theme.spacing.unit * 2
126     }
127 });
128
129 export default withStyles(styles)(DataExplorer);