X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/fd79729916cf6ddf27063b1865d39a36abb9e038..2f14e8e6ff2dabed511301338a917fbf4226ce12:/src/components/data-table-filters/data-table-filters.tsx?ds=inline

diff --git a/src/components/data-table-filters/data-table-filters.tsx b/src/components/data-table-filters/data-table-filters.tsx
index cf0260b1..11607e4b 100644
--- a/src/components/data-table-filters/data-table-filters.tsx
+++ b/src/components/data-table-filters/data-table-filters.tsx
@@ -18,139 +18,12 @@ import {
     Card,
     CardActions,
     Typography,
-    CardContent
+    CardContent,
+    Tooltip
 } from "@material-ui/core";
 import * as classnames from "classnames";
 import { DefaultTransformOrigin } from "../popover/helpers";
 
-export interface DataTableFilterItem {
-    name: string;
-    selected: boolean;
-}
-
-export interface DataTableFilterProps {
-    name: string;
-    filters: DataTableFilterItem[];
-    onChange?: (filters: DataTableFilterItem[]) => void;
-}
-
-interface DataTableFilterState {
-    anchorEl?: HTMLElement;
-    filters: DataTableFilterItem[];
-    prevFilters: DataTableFilterItem[];
-}
-
-class DataTableFilter extends React.Component<DataTableFilterProps & WithStyles<CssRules>, DataTableFilterState> {
-    state: DataTableFilterState = {
-        anchorEl: undefined,
-        filters: [],
-        prevFilters: []
-    };
-    icon = React.createRef<HTMLElement>();
-
-    render() {
-        const { name, classes, children } = this.props;
-        return <>
-            <ButtonBase
-                className={classnames([
-                    classes.root,
-                    { [classes.active]: this.state.filters.filter(({ selected }) => !selected).length > 0 }])}
-                component="span"
-                onClick={this.open}
-                disableRipple>
-                {children}
-                <i className={classnames(["fas fa-filter", classes.icon])}
-                    data-fa-transform="shrink-3"
-                    ref={this.icon} />
-            </ButtonBase>
-            <Popover
-                anchorEl={this.state.anchorEl}
-                open={!!this.state.anchorEl}
-                anchorOrigin={DefaultTransformOrigin}
-                transformOrigin={DefaultTransformOrigin}
-                onClose={this.cancel}>
-                <Card>
-                    <CardContent>
-                        <Typography variant="caption">
-                            {name}
-                        </Typography>
-                    </CardContent>
-                    <List dense>
-                        {this.state.filters.map((filter, index) =>
-                            <ListItem
-                                button
-                                key={index}
-                                onClick={this.toggleFilter(filter)}>
-                                <Checkbox
-                                    disableRipple
-                                    color="primary"
-                                    checked={filter.selected}
-                                    className={classes.checkbox} />
-                                <ListItemText>
-                                    {filter.name}
-                                </ListItemText>
-                            </ListItem>
-                        )}
-                    </List>
-                    <CardActions>
-                        <Button
-                            color="primary"
-                            variant="raised"
-                            size="small"
-                            onClick={this.submit}>
-                            Ok
-                        </Button>
-                        <Button
-                            color="primary"
-                            variant="outlined"
-                            size="small"
-                            onClick={this.cancel}>
-                            Cancel
-                        </Button>
-                    </CardActions >
-                </Card>
-            </Popover>
-        </>;
-    }
-
-    static getDerivedStateFromProps(props: DataTableFilterProps, state: DataTableFilterState): DataTableFilterState {
-        return props.filters !== state.prevFilters
-            ? { ...state, filters: props.filters, prevFilters: props.filters }
-            : state;
-    }
-
-    open = () => {
-        this.setState({ anchorEl: this.icon.current || undefined });
-    }
-
-    submit = () => {
-        const { onChange } = this.props;
-        if (onChange) {
-            onChange(this.state.filters);
-        }
-        this.setState({ anchorEl: undefined });
-    }
-
-    cancel = () => {
-        this.setState(prev => ({
-            ...prev,
-            filters: prev.prevFilters,
-            anchorEl: undefined
-        }));
-    }
-
-    toggleFilter = (toggledFilter: DataTableFilterItem) => () => {
-        this.setState(prev => ({
-            ...prev,
-            filters: prev.filters.map(filter =>
-                filter === toggledFilter
-                    ? { ...filter, selected: !filter.selected }
-                    : filter)
-        }));
-    }
-}
-
-
 export type CssRules = "root" | "icon" | "active" | "checkbox";
 
 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
@@ -186,4 +59,130 @@ const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
     }
 });
 
-export default withStyles(styles)(DataTableFilter);
+export interface DataTableFilterItem {
+    name: string;
+    selected: boolean;
+}
+
+export interface DataTableFilterProps {
+    name: string;
+    filters: DataTableFilterItem[];
+    onChange?: (filters: DataTableFilterItem[]) => void;
+}
+
+interface DataTableFilterState {
+    anchorEl?: HTMLElement;
+    filters: DataTableFilterItem[];
+    prevFilters: DataTableFilterItem[];
+}
+
+export const DataTableFilters = withStyles(styles)(
+    class extends React.Component<DataTableFilterProps & WithStyles<CssRules>, DataTableFilterState> {
+        state: DataTableFilterState = {
+            anchorEl: undefined,
+            filters: [],
+            prevFilters: []
+        };
+        icon = React.createRef<HTMLElement>();
+
+        render() {
+            const { name, classes, children } = this.props;
+            const isActive = this.state.filters.some(f => f.selected);
+            return <>
+                <Tooltip title='Filters'>
+                    <ButtonBase
+                        className={classnames([classes.root, { [classes.active]: isActive }])}
+                        component="span"
+                        onClick={this.open}
+                        disableRipple>
+                        {children}
+                        <i className={classnames(["fas fa-filter", classes.icon])}
+                            data-fa-transform="shrink-3"
+                            ref={this.icon} />
+                    </ButtonBase>
+                </Tooltip>
+                <Popover
+                    anchorEl={this.state.anchorEl}
+                    open={!!this.state.anchorEl}
+                    anchorOrigin={DefaultTransformOrigin}
+                    transformOrigin={DefaultTransformOrigin}
+                    onClose={this.cancel}>
+                    <Card>
+                        <CardContent>
+                            <Typography variant="caption">
+                                {name}
+                            </Typography>
+                        </CardContent>
+                        <List dense>
+                            {this.state.filters.map((filter, index) =>
+                                <ListItem
+                                    key={index}>
+                                    <Checkbox
+                                        onClick={this.toggleFilter(filter)}
+                                        color="primary"
+                                        checked={filter.selected}
+                                        className={classes.checkbox} />
+                                    <ListItemText>
+                                        {filter.name}
+                                    </ListItemText>
+                                </ListItem>
+                            )}
+                        </List>
+                        <CardActions>
+                            <Button
+                                color="primary"
+                                variant="raised"
+                                size="small"
+                                onClick={this.submit}>
+                                Ok
+                            </Button>
+                            <Button
+                                color="primary"
+                                variant="outlined"
+                                size="small"
+                                onClick={this.cancel}>
+                                Cancel
+                            </Button>
+                        </CardActions >
+                    </Card>
+                </Popover>
+            </>;
+        }
+
+        static getDerivedStateFromProps(props: DataTableFilterProps, state: DataTableFilterState): DataTableFilterState {
+            return props.filters !== state.prevFilters
+                ? { ...state, filters: props.filters, prevFilters: props.filters }
+                : state;
+        }
+
+        open = () => {
+            this.setState({ anchorEl: this.icon.current || undefined });
+        }
+
+        submit = () => {
+            const { onChange } = this.props;
+            if (onChange) {
+                onChange(this.state.filters);
+            }
+            this.setState({ anchorEl: undefined });
+        }
+
+        cancel = () => {
+            this.setState(prev => ({
+                ...prev,
+                filters: prev.prevFilters,
+                anchorEl: undefined
+            }));
+        }
+
+        toggleFilter = (toggledFilter: DataTableFilterItem) => () => {
+            this.setState(prev => ({
+                ...prev,
+                filters: prev.filters.map(filter =>
+                    filter === toggledFilter
+                        ? { ...filter, selected: !filter.selected }
+                        : filter)
+            }));
+        }
+    }
+);
