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, 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';
15 interface DataExplorerProps<T> {
17 columns: Array<DataColumn<T>>;
18 contextActions: ContextMenuActionGroup[];
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;
32 interface DataExplorerState<T> {
34 anchorEl?: HTMLElement;
39 class DataExplorer<T> extends React.Component<DataExplorerProps<T> & WithStyles<CssRules>, DataExplorerState<T>> {
40 state: DataExplorerState<T> = {
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 {this.props.items.length > 0 &&
53 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
54 <div className={this.props.classes.searchBox}>
56 value={this.props.searchValue}
57 onSearch={this.props.onSearch} />
60 columns={this.props.columns}
61 onColumnToggle={this.props.onColumnToggle} />
66 columns={this.props.columns}
67 items={this.props.items}
68 onRowClick={(_, item: T) => this.props.onRowClick(item)}
69 onRowContextMenu={this.openContextMenu}
70 onFiltersChange={this.props.onFiltersChange}
71 onSortToggle={this.props.onSortToggle} />
73 {this.props.items.length > 0 &&
74 <Grid container justify="flex-end">
76 count={this.props.items.length}
77 rowsPerPage={this.props.rowsPerPage}
78 page={this.props.page}
79 onChangePage={this.changePage}
80 onChangeRowsPerPage={this.changeRowsPerPage}
88 openContextMenu = (event: React.MouseEvent<HTMLElement>, item: T) => {
89 event.preventDefault();
92 anchorEl: mockAnchorFromMouseEvent(event),
98 closeContextMenu = () => {
99 this.setState({ contextMenu: {} });
102 callAction = (action: ContextMenuAction) => {
103 const { item } = this.state.contextMenu;
104 this.closeContextMenu();
106 this.props.onContextAction(action, item);
110 changePage = (event: React.MouseEvent<HTMLButtonElement> | null, page: number) => {
111 this.props.onChangePage(page);
114 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
115 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
120 type CssRules = "searchBox" | "toolbar";
122 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
124 paddingBottom: theme.spacing.unit * 2
127 paddingTop: theme.spacing.unit * 2
131 export default withStyles(styles)(DataExplorer);