add new option for data-explorer - hide search input
[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, Tooltip, Button } from '@material-ui/core';
7 import { ColumnSelector } from "~/components/column-selector/column-selector";
8 import { DataTable, DataColumns } from "~/components/data-table/data-table";
9 import { DataColumn, SortDirection } from "~/components/data-table/data-column";
10 import { SearchInput } from '~/components/search-input/search-input';
11 import { ArvadosTheme } from "~/common/custom-theme";
12 import { createTree } from '~/models/tree';
13 import { DataTableFilters } from '~/components/data-table-filters/data-table-filters-tree';
14 import { MoreOptionsIcon } from '~/components/icon/icon';
15 import { PaperProps } from '@material-ui/core/Paper';
16
17 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
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     footer: {
27         overflow: 'auto'
28     },
29     root: {
30         height: '100%'
31     },
32     moreOptionsButton: {
33         padding: 0
34     }
35 });
36
37 interface DataExplorerDataProps<T> {
38     items: T[];
39     itemsAvailable: number;
40     columns: DataColumns<T>;
41     searchValue: string;
42     rowsPerPage: number;
43     rowsPerPageOptions: number[];
44     page: number;
45     contextMenuColumn: boolean;
46     dataTableDefaultView?: React.ReactNode;
47     working?: boolean;
48     hideColumnSelector?: boolean;
49     paperProps?: PaperProps;
50     actions?: React.ReactNode;
51     hideSearchInput?: boolean;
52 }
53
54 interface DataExplorerActionProps<T> {
55     onSetColumns: (columns: DataColumns<T>) => void;
56     onSearch: (value: string) => void;
57     onRowClick: (item: T) => void;
58     onRowDoubleClick: (item: T) => void;
59     onColumnToggle: (column: DataColumn<T>) => void;
60     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
61     onSortToggle: (column: DataColumn<T>) => void;
62     onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
63     onChangePage: (page: number) => void;
64     onChangeRowsPerPage: (rowsPerPage: number) => void;
65     extractKey?: (item: T) => React.Key;
66 }
67
68 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
69
70 export const DataExplorer = withStyles(styles)(
71     class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
72         componentDidMount() {
73             if (this.props.onSetColumns) {
74                 this.props.onSetColumns(this.props.columns);
75             }
76         }
77         render() {
78             const {
79                 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
80                 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
81                 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
82                 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput
83             } = this.props;
84             return <Paper className={classes.root} {...paperProps}>
85                 <Toolbar className={classes.toolbar}>
86                     <Grid container justify="space-between" wrap="nowrap" alignItems="center">
87                         {!hideSearchInput && <div className={classes.searchBox}>
88                             <SearchInput
89                                 value={searchValue}
90                                 onSearch={onSearch} />
91                         </div>}
92                         {actions}
93                         {!hideColumnSelector && <ColumnSelector
94                             columns={columns}
95                             onColumnToggle={onColumnToggle} />}
96                     </Grid>
97                 </Toolbar>
98                 <DataTable
99                     columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
100                     items={items}
101                     onRowClick={(_, item: T) => onRowClick(item)}
102                     onContextMenu={onContextMenu}
103                     onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
104                     onFiltersChange={onFiltersChange}
105                     onSortToggle={onSortToggle}
106                     extractKey={extractKey}
107                     working={working}
108                     defaultView={dataTableDefaultView}
109                 />
110                 <Toolbar className={classes.footer}>
111                     <Grid container justify="flex-end">
112                         <TablePagination
113                             count={itemsAvailable}
114                             rowsPerPage={rowsPerPage}
115                             rowsPerPageOptions={rowsPerPageOptions}
116                             page={this.props.page}
117                             onChangePage={this.changePage}
118                             onChangeRowsPerPage={this.changeRowsPerPage}
119                             component="div" />
120                     </Grid>
121                 </Toolbar>
122             </Paper>;
123         }
124
125         changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
126             this.props.onChangePage(page);
127         }
128
129         changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
130             this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
131         }
132
133         renderContextMenuTrigger = (item: T) =>
134             <Grid container justify="center">
135                 <Tooltip title="More options" disableFocusListener>
136                     <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
137                         <MoreOptionsIcon />
138                     </IconButton>
139                 </Tooltip>
140             </Grid>
141
142         contextMenuColumn: DataColumn<any> = {
143             name: "Actions",
144             selected: true,
145             configurable: false,
146             sortDirection: SortDirection.NONE,
147             filters: createTree(),
148             key: "context-actions",
149             render: this.renderContextMenuTrigger
150         };
151     }
152 );