21720:
[arvados.git] / services / workbench2 / 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 { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { IconButton, Paper, List, Checkbox, ListItemText, ListItem, Tooltip } from '@mui/material';
8 import { WithStyles } from '@mui/styles';
9 import withStyles from '@mui/styles/withStyles';
10 import MenuIcon from "@mui/icons-material/Menu";
11 import { DataColumn } from '../data-table/data-column';
12 import { Popover } from "../popover/popover";
13 import { IconButtonProps } from '@mui/material/IconButton';
14 import { DataColumns } from '../data-table/data-table';
15 import { ArvadosTheme } from "common/custom-theme";
16
17 interface ColumnSelectorDataProps {
18     columns: DataColumns<any, any>;
19     onColumnToggle: (column: DataColumn<any, any>) => void;
20     className?: string;
21 }
22
23 type CssRules = "checkbox" | "listItem" | "listItemText";
24
25 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
26     checkbox: {
27         width: 24,
28         height: 24
29     },
30     listItem: {
31         padding: 0
32     },
33     listItemText: {
34         paddingTop: '0.2rem'
35     }
36 });
37
38 export type ColumnSelectorProps = ColumnSelectorDataProps & WithStyles<CssRules>;
39
40 export const ColumnSelector = withStyles(styles)(
41     ({ columns, onColumnToggle, classes }: ColumnSelectorProps) =>
42         <Popover triggerComponent={ColumnSelectorTrigger}>
43             <Paper>
44                 <List dense>
45                     {columns
46                         .filter(column => column.configurable)
47                         .map((column, index) =>
48                             <ListItem
49                                 button
50                                 key={index}
51                                 className={classes.listItem}
52                                 onClick={() => onColumnToggle(column)}>
53                                 <Checkbox
54                                     disableRipple
55                                     color="primary"
56                                     checked={column.selected}
57                                     className={classes.checkbox} />
58                                 <ListItemText
59                                     className={classes.listItemText}>
60                                     {column.name}
61                                 </ListItemText>
62                             </ListItem>
63                         )}
64                 </List>
65             </Paper>
66         </Popover>
67 );
68
69 export const ColumnSelectorTrigger = (props: IconButtonProps) =>
70     <Tooltip disableFocusListener title="Select columns">
71         <IconButton {...props} size="large">
72             <MenuIcon aria-label="Select columns" />
73         </IconButton>
74     </Tooltip>;