18549: Layout fixed, tests updated
[arvados-workbench2.git] / src / components / column-selector / column-selector.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { WithStyles, StyleRulesCallback, withStyles, IconButton, Paper, List, Checkbox, ListItemText, ListItem, Tooltip } from '@material-ui/core';
7 import MenuIcon from "@material-ui/icons/Menu";
8 import { DataColumn } from '../data-table/data-column';
9 import { Popover } from "../popover/popover";
10 import { IconButtonProps } from '@material-ui/core/IconButton';
11 import { DataColumns } from '../data-table/data-table';
12 import { ArvadosTheme } from "common/custom-theme";
13
14 interface ColumnSelectorDataProps {
15     columns: DataColumns<any>;
16     onColumnToggle: (column: DataColumn<any>) => void;
17     className?: string;
18 }
19
20 type CssRules = "checkbox";
21
22 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
23     checkbox: {
24         width: 24,
25         height: 24
26     }
27 });
28
29 export type ColumnSelectorProps = ColumnSelectorDataProps & WithStyles<CssRules>;
30
31 export const ColumnSelector = withStyles(styles)(
32     ({ columns, onColumnToggle, classes }: ColumnSelectorProps) =>
33         <Popover triggerComponent={ColumnSelectorTrigger}>
34             <Paper>
35                 <List dense>
36                     {columns
37                         .filter(column => column.configurable)
38                         .map((column, index) =>
39                             <ListItem
40                                 button
41                                 key={index}
42                                 onClick={() => onColumnToggle(column)}>
43                                 <Checkbox
44                                     disableRipple
45                                     color="primary"
46                                     checked={column.selected}
47                                     className={classes.checkbox} />
48                                 <ListItemText>
49                                     {column.name}
50                                 </ListItemText>
51                             </ListItem>
52                         )}
53                 </List>
54             </Paper>
55         </Popover>
56 );
57
58 export const ColumnSelectorTrigger = (props: IconButtonProps) =>
59     <Tooltip disableFocusListener title="Select columns">
60         <IconButton {...props}>
61             <MenuIcon aria-label="Select columns" />
62         </IconButton>
63     </Tooltip>;