1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
14 interface DataExplorerProps<T> {
16 itemsAvailable: number;
17 columns: DataColumns<T>;
20 rowsPerPageOptions?: 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;
34 class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>> {
38 <Toolbar className={this.props.classes.toolbar}>
39 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
40 <div className={this.props.classes.searchBox}>
42 value={this.props.searchValue}
43 onSearch={this.props.onSearch} />
46 columns={this.props.columns}
47 onColumnToggle={this.props.onColumnToggle} />
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} />
60 {this.props.items.length > 0 &&
61 <Grid container justify="flex-end">
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}
76 changePage = (event: React.MouseEvent<HTMLButtonElement> | null, page: number) => {
77 this.props.onChangePage(page);
80 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
81 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
84 renderContextMenuTrigger = (item: T) =>
85 <Grid container justify="flex-end">
86 <IconButton onClick={event => this.props.onContextMenu(event, item)}>
94 key: "context-actions",
95 renderHeader: () => null,
96 render: this.renderContextMenuTrigger,
102 type CssRules = "searchBox" | "toolbar";
104 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
106 paddingBottom: theme.spacing.unit * 2
109 paddingTop: theme.spacing.unit * 2
113 export default withStyles(styles)(DataExplorer);